diff --git a/src/main/java/leonardo/labutilities/qualitylabpro/services/users/UserService.java b/src/main/java/leonardo/labutilities/qualitylabpro/services/users/UserService.java index 13b74e8..41c780f 100644 --- a/src/main/java/leonardo/labutilities/qualitylabpro/services/users/UserService.java +++ b/src/main/java/leonardo/labutilities/qualitylabpro/services/users/UserService.java @@ -40,7 +40,8 @@ private void sendRecoveryEmail(RecoveryEmailDTO recoveryEmailDTO) { Best regards, Your Team""", recoveryEmailDTO.temporaryPassword()); log.info("Sending recovery identifier to: {}", recoveryEmailDTO.email()); - this.emailService.sendPlainTextEmail(new EmailDTO(recoveryEmailDTO.email(), subject, message)); + this.emailService + .sendPlainTextEmail(new EmailDTO(recoveryEmailDTO.email(), subject, message)); } public void recoverPassword(String username, String email) { @@ -61,7 +62,8 @@ public void changePassword(String email, String temporaryPassword, String newPas if (!this.passwordRecoveryTokenManager.isRecoveryTokenValid(temporaryPassword, email)) { throw new CustomGlobalErrorHandling.RecoveryTokenInvalidException(); } - this.userRepository.setPasswordWhereByEmail(email, BCryptEncoderComponent.encrypt(newPassword)); + this.userRepository.setPasswordWhereByEmail(email, + BCryptEncoderComponent.encrypt(newPassword)); } private TokenJwtDTO authenticateAndGenerateToken(User credential, String password) { @@ -94,19 +96,23 @@ public User signUp(String username, String email, String password) { var user = new User(username, BCryptEncoderComponent.encrypt(password), email, UserRoles.USER); + var savedUser = this.userRepository.save(user); try { - this.emailService.notifyUserSignup(user.getUsername(), user.getEmail(), LocalDateTime.now()); + this.emailService.notifyUserSignup(savedUser.getUsername(), savedUser.getEmail(), + LocalDateTime.now()); } catch (Exception e) { - log.error("Failed signup for user: {}. Exception: ", user.getEmail(), e); + log.error("Failed to send signup notification for user: {}. Exception: ", + savedUser.getEmail(), e); } - return this.userRepository.save(user); + return savedUser; } public TokenJwtDTO signIn(String identifier, String password) { try { - final var credential = this.userRepository.findOneByUsernameOrEmail(identifier, identifier); + final var credential = + this.userRepository.findOneByUsernameOrEmail(identifier, identifier); if (credential == null) { throw new CustomGlobalErrorHandling.UserNotFoundException(); diff --git a/src/main/java/leonardo/labutilities/qualitylabpro/utils/exception/CustomGlobalErrorHandling.java b/src/main/java/leonardo/labutilities/qualitylabpro/utils/exception/CustomGlobalErrorHandling.java index bfd3b7d..6cd36ca 100644 --- a/src/main/java/leonardo/labutilities/qualitylabpro/utils/exception/CustomGlobalErrorHandling.java +++ b/src/main/java/leonardo/labutilities/qualitylabpro/utils/exception/CustomGlobalErrorHandling.java @@ -89,12 +89,12 @@ public ResponseEntity handleAccessDenied(HttpServletRequest request) { @ResponseStatus(HttpStatus.CONFLICT) public ResponseEntity handleUserAlreadyExist(UserAlreadyExistException ex, HttpServletRequest request) { - ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, - "Username or identifier already exists", request.getRequestURI()); + ApiError apiError = new ApiError(HttpStatus.CONFLICT, + "A user with this username or email already exists", request.getRequestURI()); - log.error("Username or identifier already exists at {}: {}", request.getRequestURI(), - ex.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(apiError); + log.error("A user with this username or email already exists at {}: {}", + request.getRequestURI(), ex.getMessage()); + return ResponseEntity.status(HttpStatus.CONFLICT).body(apiError); } @ExceptionHandler(DataIntegrityViolationException.class)