コード例 #1
0
        public async Task HandleAsync(AccountDeletedIntegrationEvent integrationEvent, CancellationToken cancellationToken = default)
        {
            try
            {
                var user = await _userRepository.GetByIdAsync(integrationEvent.AccountId);

                user.AddDeletedEvent(integrationEvent.CorrelationId);
                await _communicationBus.DispatchDomainEventsAsync(user, cancellationToken);

                await _userRepository.DeleteAsync(user);

                var userDeletedIntegrationEvent = new UserDeletedIntegrationEvent(integrationEvent.CorrelationId, user.Id);
                await _integrationEventBus.PublishIntegrationEventAsync(userDeletedIntegrationEvent);
            }
            catch (Exception e)
            {
                _logger.LogIntegrationEventError(ServiceComponentEnumeration.RivaUsers, integrationEvent,
                                                 "userId={userId}, message={message}, stackTrace={stackTrace}", integrationEvent.AccountId,
                                                 e.Message, e.StackTrace);
                var userDeletionCompletedIntegrationEventFailure = new UserDeletionCompletedIntegrationEventFailure(
                    integrationEvent.CorrelationId, IntegrationEventErrorCodeEnumeration.UnexpectedError.DisplayName,
                    IntegrationEventErrorMessage.UnexpectedError, integrationEvent.AccountId);
                await _integrationEventBus.PublishIntegrationEventAsync(userDeletionCompletedIntegrationEventFailure);
            }
        }
コード例 #2
0
        public async Task HandleAsync(DeleteRoomForRentAnnouncementPreferenceCommand command, CancellationToken cancellationToken = default)
        {
            var getUserResult = await _userGetterService.GetByIdAsync(command.UserId);

            if (!getUserResult.Success)
            {
                throw new ResourceNotFoundException(getUserResult.Errors);
            }

            var getRoomForRentAnnouncementPreferenceResult =
                _roomForRentAnnouncementPreferenceGetterService.GetByByUserAndId(
                    getUserResult.Value, command.RoomForRentAnnouncementPreferenceId);

            if (!getRoomForRentAnnouncementPreferenceResult.Success)
            {
                throw new ResourceNotFoundException(getRoomForRentAnnouncementPreferenceResult.Errors);
            }

            getUserResult.Value.DeleteRoomForRentAnnouncementPreference(getRoomForRentAnnouncementPreferenceResult.Value, command.CorrelationId);
            await _communicationBus.DispatchDomainEventsAsync(getUserResult.Value, cancellationToken);

            await _userRepository.UpdateAsync(getUserResult.Value);

            var userAnnouncementPreferenceDeletedIntegrationEvent =
                new UserAnnouncementPreferenceDeletedIntegrationEvent(command.CorrelationId, getUserResult.Value.Id,
                                                                      getRoomForRentAnnouncementPreferenceResult.Value.Id,
                                                                      AnnouncementPreferenceType.RoomForRentAnnouncementPreference);
            await _integrationEventBus.PublishIntegrationEventAsync(userAnnouncementPreferenceDeletedIntegrationEvent);
        }
コード例 #3
0
        public async Task HandleAsync(RequestPasswordResetTokenCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByEmailAsync(command.Email);

            if (!getAccountResult.Success)
            {
                throw new ValidationException(getAccountResult.Errors);
            }

            var accountIsConfirmedVerificationResult = _accountVerificationService.VerifyAccountIsConfirmed(getAccountResult.Value.Confirmed);

            if (!accountIsConfirmedVerificationResult.Success)
            {
                throw new ValidationException(accountIsConfirmedVerificationResult.Errors);
            }

            var passwordIsSetVerificationResult = _accountVerificationService.VerifyPasswordIsSet(getAccountResult.Value.PasswordHash);

            if (!passwordIsSetVerificationResult.Success)
            {
                throw new ValidationException(passwordIsSetVerificationResult.Errors);
            }

            var correlationId = Guid.NewGuid();
            var token         = getAccountResult.Value.GenerateToken(TokenTypeEnumeration.PasswordReset, correlationId);
            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountRepository.UpdateAsync(getAccountResult.Value);

            await _passwordResetTokenRequestService.PublishPasswordResetRequestedIntegrationEventAsync(getAccountResult.Value.Email, token.Value, correlationId);
        }
コード例 #4
0
        public async Task HandleAsync(AccountCreatedIntegrationEvent integrationEvent, CancellationToken cancellationToken = default)
        {
            try
            {
                var user = User.Builder()
                           .SetId(integrationEvent.AccountId)
                           .SetEmail(integrationEvent.Email)
                           .SetServiceActive(DefaultUserSettings.ServiceActive)
                           .SetAnnouncementPreferenceLimit(DefaultUserSettings.AnnouncementPreferenceLimit)
                           .SetAnnouncementSendingFrequency(DefaultUserSettings.AnnouncementSendingFrequency)
                           .SetPicture(integrationEvent.Picture)
                           .Build();

                user.AddCreatedEvent(integrationEvent.CorrelationId);

                await _communicationBus.DispatchDomainEventsAsync(user, cancellationToken);

                await _userRepository.AddAsync(user);

                var userCreationCompletedIntegrationEvent = new UserCreationCompletedIntegrationEvent(integrationEvent.CorrelationId, user.Id);
                await _integrationEventBus.PublishIntegrationEventAsync(userCreationCompletedIntegrationEvent);
            }
            catch (Exception e)
            {
                _logger.LogIntegrationEventError(ServiceComponentEnumeration.RivaUsers, integrationEvent,
                                                 "userId={userId}, message={message}, stackTrace={stackTrace}", integrationEvent.AccountId, e.Message, e.StackTrace);
                var userCreationCompletedIntegrationEventFailure = new UserCreationCompletedIntegrationEventFailure(
                    integrationEvent.CorrelationId, IntegrationEventErrorCodeEnumeration.UnexpectedError.DisplayName,
                    IntegrationEventErrorMessage.UnexpectedError, integrationEvent.AccountId);
                await _integrationEventBus.PublishIntegrationEventAsync(userCreationCompletedIntegrationEventFailure);
            }
        }
コード例 #5
0
        public async Task HandleAsync(ChangePasswordCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByIdAsync(command.AccountId);

            if (!getAccountResult.Success)
            {
                throw new ResourceNotFoundException(getAccountResult.Errors);
            }

            var passwordIsSetVerificationResult = _accountVerificationService.VerifyPasswordIsSet(getAccountResult.Value.PasswordHash);

            if (!passwordIsSetVerificationResult.Success)
            {
                throw new ValidationException(passwordIsSetVerificationResult.Errors);
            }

            var passwordIsCorrectVerificationResult = _accountVerificationService.VerifyPassword(getAccountResult.Value.PasswordHash, command.OldPassword);

            if (!passwordIsCorrectVerificationResult.Success)
            {
                throw new ValidationException(passwordIsCorrectVerificationResult.Errors);
            }

            getAccountResult.Value.ChangePassword(_passwordService.HashPassword(command.NewPassword), Guid.NewGuid());
            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountRepository.UpdateAsync(getAccountResult.Value);
        }
コード例 #6
0
        public async Task HandleAsync(ConfirmAccountCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByEmailAsync(command.Email);

            if (!getAccountResult.Success)
            {
                throw new ValidationException(getAccountResult.Errors);
            }

            var accountIsNotConfirmedVerificationResult = _accountVerificationService.VerifyAccountIsNotConfirmed(getAccountResult.Value.Confirmed);

            if (!accountIsNotConfirmedVerificationResult.Success)
            {
                throw new ValidationException(accountIsNotConfirmedVerificationResult.Errors);
            }

            var accountConfirmationToken           = getAccountResult.Value.Tokens.SingleOrDefault(x => Equals(x.Type, TokenTypeEnumeration.AccountConfirmation));
            var confirmationCodeVerificationResult = _accountVerificationService.VerifyConfirmationCode(accountConfirmationToken, command.Code);

            if (!confirmationCodeVerificationResult.Success)
            {
                throw new ValidationException(confirmationCodeVerificationResult.Errors);
            }

            getAccountResult.Value.Confirm(Guid.NewGuid());
            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountRepository.UpdateAsync(getAccountResult.Value);
        }
コード例 #7
0
        public async Task HandleAsync(UpdateUserCommand command, CancellationToken cancellationToken = default)
        {
            var getUserResult = await _userGetterService.GetByIdAsync(command.UserId);

            if (!getUserResult.Success)
            {
                throw new ResourceNotFoundException(getUserResult.Errors);
            }

            UpdateAnnouncementPreferenceLimit(getUserResult.Value, command.AnnouncementPreferenceLimit, command.CorrelationId);
            UpdateAnnouncementSendingFrequency(getUserResult.Value, command.AnnouncementSendingFrequency, command.CorrelationId);
            getUserResult.Value.ChangeServiceActive(command.ServiceActive, command.CorrelationId);

            if (command.Picture != null && command.Picture.Data.Any())
            {
                var pictureUrl = await _blobContainerService.UploadFileAsync(command.Picture.Data,
                                                                             $"image-{getUserResult.Value.Id.ToString().ToLower()}", command.Picture.ContentType);

                getUserResult.Value.ChangePicture(pictureUrl, command.CorrelationId);
            }

            await _communicationBus.DispatchDomainEventsAsync(getUserResult.Value, cancellationToken);

            await _userRepository.UpdateAsync(getUserResult.Value);

            var userUpdatedIntegrationEvent = new UserUpdatedIntegrationEvent(command.CorrelationId,
                                                                              getUserResult.Value.Id, getUserResult.Value.ServiceActive,
                                                                              getUserResult.Value.AnnouncementSendingFrequency.ConvertToEnum());
            await _integrationEventBus.PublishIntegrationEventAsync(userUpdatedIntegrationEvent);
        }
コード例 #8
0
        public async Task HandleAsync(CreateFlatForRentAnnouncementPreferenceCommand command, CancellationToken cancellationToken = default)
        {
            var getUserResult = await _userGetterService.GetByIdAsync(command.UserId);

            if (!getUserResult.Success)
            {
                throw new ResourceNotFoundException(getUserResult.Errors);
            }

            var cityAndCityDistrictsVerificationResult = await _cityVerificationService.VerifyCityAndCityDistrictsAsync(command.CityId, command.CityDistricts);

            if (!cityAndCityDistrictsVerificationResult.Success)
            {
                throw new ValidationException(cityAndCityDistrictsVerificationResult.Errors);
            }

            var announcementPreferenceLimitIsNotExceededVerificationResult =
                _userVerificationService.VerifyAnnouncementPreferenceLimitIsNotExceeded(
                    getUserResult.Value.AnnouncementPreferenceLimit,
                    getUserResult.Value.FlatForRentAnnouncementPreferences.Count +
                    getUserResult.Value.RoomForRentAnnouncementPreferences.Count);

            if (!announcementPreferenceLimitIsNotExceededVerificationResult.Success)
            {
                throw new ValidationException(announcementPreferenceLimitIsNotExceededVerificationResult.Errors);
            }

            var flatForRentAnnouncementPreference =
                _mapper.Map <CreateFlatForRentAnnouncementPreferenceCommand, FlatForRentAnnouncementPreference>(command);
            var flatForRentAnnouncementPreferences = getUserResult.Value.FlatForRentAnnouncementPreferences.ToList();

            flatForRentAnnouncementPreferences.Add(flatForRentAnnouncementPreference);
            var flatForRentAnnouncementPreferencesVerificationResult =
                _flatForRentAnnouncementPreferenceVerificationService.VerifyFlatForRentAnnouncementPreferences(flatForRentAnnouncementPreferences);

            if (!flatForRentAnnouncementPreferencesVerificationResult.Success)
            {
                throw new ValidationException(flatForRentAnnouncementPreferencesVerificationResult.Errors);
            }

            getUserResult.Value.AddFlatForRentAnnouncementPreference(flatForRentAnnouncementPreference, command.CorrelationId);
            await _communicationBus.DispatchDomainEventsAsync(getUserResult.Value, cancellationToken);

            await _userRepository.UpdateAsync(getUserResult.Value);

            var userFlatForRentAnnouncementPreferenceCreatedIntegrationEvent =
                new UserFlatForRentAnnouncementPreferenceCreatedIntegrationEvent(command.CorrelationId,
                                                                                 getUserResult.Value.Id,
                                                                                 flatForRentAnnouncementPreference.Id,
                                                                                 getUserResult.Value.Email,
                                                                                 flatForRentAnnouncementPreference.CityId,
                                                                                 getUserResult.Value.ServiceActive,
                                                                                 getUserResult.Value.AnnouncementSendingFrequency.ConvertToEnum(),
                                                                                 flatForRentAnnouncementPreference.PriceMin,
                                                                                 flatForRentAnnouncementPreference.PriceMax,
                                                                                 flatForRentAnnouncementPreference.RoomNumbersMin,
                                                                                 flatForRentAnnouncementPreference.RoomNumbersMax,
                                                                                 flatForRentAnnouncementPreference.CityDistricts);
            await _integrationEventBus.PublishIntegrationEventAsync(userFlatForRentAnnouncementPreferenceCreatedIntegrationEvent);
        }
コード例 #9
0
        public async Task <Account> ProvideAccountForExternalLoginAsync(string email, Guid correlationId)
        {
            var getAccountResult = await _accountGetterService.GetByEmailAsync(email);

            Account account;

            if (!getAccountResult.Success)
            {
                account = await _accountCreatorService.CreateAsync(Guid.NewGuid(), email, string.Empty, correlationId);
            }
            else
            {
                account = getAccountResult.Value;
                if (account.Confirmed)
                {
                    return(account);
                }

                account.Confirm(correlationId);
                await _communicationBus.DispatchDomainEventsAsync(account);

                await _accountRepository.UpdateAsync(account);
            }

            return(account);
        }
コード例 #10
0
        public async Task HandleAsync(UpdateAccountRolesCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByIdAsync(command.AccountId);

            if (!getAccountResult.Success)
            {
                throw new ResourceNotFoundException(getAccountResult.Errors);
            }

            var rolesToRemove = getAccountResult.Value.Roles.Except(command.Roles).ToList();
            var rolesToAdd    = command.Roles.Except(getAccountResult.Value.Roles).ToList();

            var userRole = await _roleRepository.GetByNameAsync(DefaultRoleEnumeration.User.DisplayName);

            if (!command.Roles.Contains(userRole.Id))
            {
                var errors = new Collection <IError>
                {
                    new Error(AccountErrorCodeEnumeration.UserRoleIsNotRemovable, AccountErrorMessage.UserRoleIsNotRemovable)
                };
                throw new ValidationException(errors);
            }

            var rolesToCheck = await _roleRepository.FindByIdsAsync(command.Roles);

            var incorrectRoles = command.Roles.Except(rolesToCheck.Select(x => x.Id));

            if (incorrectRoles.Any())
            {
                var errors = new Collection <IError>
                {
                    new Error(RoleErrorCodeEnumeration.RolesNotFound, RoleErrorMessage.RolesNotFound)
                };
                throw new ValidationException(errors);
            }

            var correlationId = Guid.NewGuid();

            foreach (var role in rolesToRemove)
            {
                getAccountResult.Value.RemoveRole(role, correlationId);
            }

            foreach (var role in rolesToAdd)
            {
                getAccountResult.Value.AddRole(role, correlationId);
            }

            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountRepository.UpdateAsync(getAccountResult.Value);
        }
コード例 #11
0
        public async Task HandleAsync(UpdateFlatForRentAnnouncementPreferenceCommand command, CancellationToken cancellationToken = default)
        {
            var getUserResult = await _userGetterService.GetByIdAsync(command.UserId);

            if (!getUserResult.Success)
            {
                throw new ResourceNotFoundException(getUserResult.Errors);
            }

            var getFlatForRentAnnouncementPreferenceResult =
                _flatForRentAnnouncementPreferenceGetterService.GetByByUserAndId(
                    getUserResult.Value, command.FlatForRentAnnouncementPreferenceId);

            if (!getFlatForRentAnnouncementPreferenceResult.Success)
            {
                throw new ResourceNotFoundException(getFlatForRentAnnouncementPreferenceResult.Errors);
            }

            await UpdateCityAndCityDistrictsAsync(getFlatForRentAnnouncementPreferenceResult.Value, command.CityId, command.CityDistricts.ToList());

            getFlatForRentAnnouncementPreferenceResult.Value.ChangePriceMin(command.PriceMin);
            getFlatForRentAnnouncementPreferenceResult.Value.ChangePriceMax(command.PriceMax);
            getFlatForRentAnnouncementPreferenceResult.Value.ChangeRoomNumbersMin(command.RoomNumbersMin);
            getFlatForRentAnnouncementPreferenceResult.Value.ChangeRoomNumbersMax(command.RoomNumbersMax);

            var flatForRentAnnouncementPreferencesVerificationResult =
                _flatForRentAnnouncementPreferenceVerificationService.VerifyFlatForRentAnnouncementPreferences(getUserResult.Value.FlatForRentAnnouncementPreferences);

            if (!flatForRentAnnouncementPreferencesVerificationResult.Success)
            {
                throw new ValidationException(flatForRentAnnouncementPreferencesVerificationResult.Errors);
            }

            getUserResult.Value.AddFlatForRentAnnouncementPreferenceChangedEvent(getFlatForRentAnnouncementPreferenceResult.Value, command.CorrelationId);
            await _communicationBus.DispatchDomainEventsAsync(getUserResult.Value, cancellationToken);

            await _userRepository.UpdateAsync(getUserResult.Value);

            var userFlatForRentAnnouncementPreferenceUpdatedIntegrationEvent =
                new UserFlatForRentAnnouncementPreferenceUpdatedIntegrationEvent(command.CorrelationId,
                                                                                 getUserResult.Value.Id,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.Id,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.CityId,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.PriceMin,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.PriceMax,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.RoomNumbersMin,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.RoomNumbersMax,
                                                                                 getFlatForRentAnnouncementPreferenceResult.Value.CityDistricts);
            await _integrationEventBus.PublishIntegrationEventAsync(userFlatForRentAnnouncementPreferenceUpdatedIntegrationEvent);
        }
コード例 #12
0
        public async Task HandleAsync(DeleteUserCommand command, CancellationToken cancellationToken = default)
        {
            var getUserResult = await _userGetterService.GetByIdAsync(command.UserId);

            if (!getUserResult.Success)
            {
                throw new ResourceNotFoundException(getUserResult.Errors);
            }

            getUserResult.Value.AddDeletedEvent(Guid.NewGuid());
            await _communicationBus.DispatchDomainEventsAsync(getUserResult.Value, cancellationToken);

            await _userRepository.DeleteAsync(getUserResult.Value);
        }
コード例 #13
0
        public async Task PersistAsync(Event evt)
        {
            if (evt is TokenIssuedSuccessEvent @event)
            {
                var getAccountResult = await _accountGetterService.GetByIdAsync(new Guid(@event.SubjectId));

                if (!getAccountResult.Success)
                {
                    return;
                }

                getAccountResult.Value.Login(Guid.NewGuid());
                await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value);

                await _accountRepository.UpdateAsync(getAccountResult.Value);
            }
        }
コード例 #14
0
        public async Task HandleAsync(DeleteAccountCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByIdAsync(command.AccountId);

            if (!getAccountResult.Success)
            {
                throw new ResourceNotFoundException(getAccountResult.Errors);
            }

            getAccountResult.Value.AddDeletedEvent(command.CorrelationId);
            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountDataConsistencyService.DeleteAccountWithRelatedPersistedGrants(getAccountResult.Value);

            var accountDeletedIntegrationEvent = new AccountDeletedIntegrationEvent(command.CorrelationId, command.AccountId);
            await _integrationEventBus.PublishIntegrationEventAsync(accountDeletedIntegrationEvent);
        }
コード例 #15
0
        public async Task HandleAsync(CreateUserCommand command, CancellationToken cancellationToken = default)
        {
            var userDoesNotExistsVerificationResult = await _userVerificationService.VerifyUserDoesNotExistAsync(command.UserId);

            if (!userDoesNotExistsVerificationResult.Success)
            {
                throw new ConflictException(userDoesNotExistsVerificationResult.Errors);
            }

            var accountExistsVerificationResult = await _accountVerificationService.VerifyAccountExistsAsync(command.UserId, command.Email);

            if (!accountExistsVerificationResult.Success)
            {
                throw new ValidationException(accountExistsVerificationResult.Errors);
            }

            var user = _mapper.Map <CreateUserCommand, User>(command);

            user.AddCreatedEvent(Guid.NewGuid());

            await _communicationBus.DispatchDomainEventsAsync(user, cancellationToken);

            await _userRepository.AddAsync(user);
        }
コード例 #16
0
        public async Task <Account> CreateAsync(Guid id, string email, string password, Guid correlationId)
        {
            var passwordHash = string.Empty;
            var confirmed    = true;

            if (!string.IsNullOrWhiteSpace(password))
            {
                passwordHash = _passwordService.HashPassword(password);
                confirmed    = false;
            }
            var account = Account.Builder()
                          .SetId(id)
                          .SetEmail(email)
                          .SetConfirmed(confirmed)
                          .SetPasswordHash(passwordHash)
                          .SetSecurityStamp(Guid.NewGuid())
                          .SetCreated(DateTimeOffset.UtcNow)
                          .Build();

            account.AddCreatedEvent(correlationId);

            if (!confirmed)
            {
                account.GenerateToken(TokenTypeEnumeration.AccountConfirmation, correlationId);
            }

            var role = await _roleRepository.GetByNameAsync(DefaultRoleEnumeration.User.DisplayName);

            account.AddRole(role.Id, correlationId);

            await _communicationBus.DispatchDomainEventsAsync(account);

            await _accountRepository.AddAsync(account);

            return(account);
        }