public async Task ExecuteAsync(InviteUser command)
        {
            var inviter = await _userRepository.GetUserAsync(command.UserId);

            var invitedUser = await _userRepository.FindByEmailAsync(command.EmailAddress);

            if (invitedUser != null)
            {
                var member = new ApplicationTeamMember(command.ApplicationId, invitedUser.AccountId)
                {
                    AddedByName = inviter.UserName,
                    Roles       = new[] { ApplicationRole.Member }
                };

                await _applicationRepository.CreateAsync(member);

                await _eventBus.PublishAsync(new UserAddedToApplication(command.ApplicationId, member.AccountId));

                return;
            }

            var invitedMember = new ApplicationTeamMember(command.ApplicationId, command.EmailAddress)
            {
                AddedByName = inviter.UserName,
                Roles       = new[] { ApplicationRole.Member }
            };
            await _applicationRepository.CreateAsync(invitedMember);

            var invitation = await _invitationRepository.FindByEmailAsync(command.EmailAddress);

            if (invitation == null)
            {
                invitation = new Invitation(command.EmailAddress, inviter.UserName);
                await _invitationRepository.CreateAsync(invitation);
                await SendInvitationEmailAsync(invitation, command.Text);
            }

            invitation.Add(command.ApplicationId, inviter.UserName);
            await _invitationRepository.UpdateAsync(invitation);

            var app = await _applicationRepository.GetByIdAsync(command.ApplicationId);

            var evt = new UserInvitedToApplication(
                invitation.InvitationKey,
                command.ApplicationId,
                app.Name,
                command.EmailAddress,
                inviter.UserName);

            await _eventBus.PublishAsync(evt);
        }
        /// <inheritdoc />
        public async Task ExecuteAsync(InviteUser command)
        {
            var inviter = await _userRepository.GetUserAsync(command.UserId);

            if (!ClaimsPrincipal.Current.IsSysAdmin() &&
                !ClaimsPrincipal.Current.IsApplicationAdmin(command.ApplicationId))
            {
                _logger.Warn($"User {command.UserId} attempted to do an invite for an application: {command.ApplicationId}.");
                throw new SecurityException("You are not an admin of that application.");
            }

            var invitedUser = await _userRepository.FindByEmailAsync(command.EmailAddress);

            if (invitedUser != null)
            {
                //correction of issue #21, verify that the person isn't already a member.
                var members = await _applicationRepository.GetTeamMembersAsync(command.ApplicationId);

                if (members.Any(x => x.AccountId == invitedUser.AccountId))
                {
                    _logger.Warn("User " + invitedUser.AccountId + " is already a member.");
                    return;
                }

                var member = new ApplicationTeamMember(command.ApplicationId, invitedUser.AccountId)
                {
                    AddedByName = inviter.UserName,
                    Roles       = new[] { ApplicationRole.Member }
                };

                await _applicationRepository.CreateAsync(member);

                await _eventBus.PublishAsync(new UserAddedToApplication(command.ApplicationId, member.AccountId));

                return;
            }
            else
            {
                //correction of issue #21, verify that the person isn't already a member.
                var members = await _applicationRepository.GetTeamMembersAsync(command.ApplicationId);

                if (members.Any(x => x.EmailAddress == command.EmailAddress))
                {
                    _logger.Warn("User " + command.EmailAddress + " is already invited.");
                    return;
                }
            }

            var invitedMember = new ApplicationTeamMember(command.ApplicationId, command.EmailAddress)
            {
                AddedByName = inviter.UserName,
                Roles       = new[] { ApplicationRole.Member }
            };
            await _applicationRepository.CreateAsync(invitedMember);

            var invitation = await _invitationRepository.FindByEmailAsync(command.EmailAddress);

            if (invitation == null)
            {
                invitation = new Invitation(command.EmailAddress, inviter.UserName);
                await _invitationRepository.CreateAsync(invitation);
                await SendInvitationEmailAsync(invitation, command.Text);
            }

            invitation.Add(command.ApplicationId, inviter.UserName);
            await _invitationRepository.UpdateAsync(invitation);

            var app = await _applicationRepository.GetByIdAsync(command.ApplicationId);

            var evt = new UserInvitedToApplication(
                invitation.InvitationKey,
                command.ApplicationId,
                app.Name,
                command.EmailAddress,
                inviter.UserName);

            await _eventBus.PublishAsync(evt);
        }