Wasi/BS-1344

added the exception handling for the email
Wasi/BS-1344
Wasiullah Khan 3 days ago
parent da14c7c846
commit c57aa7e4b8

@ -4,6 +4,7 @@ import com.mfsys.common.configuration.exception.ErrorMessage;
public enum ERRCode implements ErrorMessage {
USER_ALREADY_EXISTS("ERR_001", "User already exists"),
EMAIL_ALREADY_USED("ERR_002", "Email already used with username {0}"),
;
private String code;

@ -30,7 +30,7 @@ public class ApplicationExceptionMapper {
@ExceptionHandler(value = {ApplicationException.class})
protected ResponseEntity<APIError> handleConflict(ApplicationException ex, WebRequest request) {
logExceptionStackTrace(ex);
return new ResponseEntity<APIError>(new APIError(ex.getErrorCode(), ex.getArguments(), ex.getLocalizedMessage(),
return new ResponseEntity<>(new APIError(ex.getErrorCode(), ex.getArguments(), ex.getErrorDescription(),
MDC.get("request_id")), HttpStatus.BAD_REQUEST);
}

@ -0,0 +1,15 @@
package com.mfsys.common.configuration.exception;
import com.mfsys.common.configuration.constant.ERRCode;
public class EmailAlreadyUsedException extends ApplicationException {
public EmailAlreadyUsedException(String porOrgacode, String existingUsername) {
super(porOrgacode, ERRCode.EMAIL_ALREADY_USED, existingUsername);
}
public EmailAlreadyUsedException(String porOrgacode, String existingUsername, Throwable cause) {
super(porOrgacode, ERRCode.EMAIL_ALREADY_USED, existingUsername);
initCause(cause);
}
}

@ -2,25 +2,17 @@ package com.mfsys.common.configuration.exception;
import com.mfsys.common.configuration.constant.ERRCode;
public class UserAlreadyExistsException extends ApplicationException {
private static final String POR_ORGA_CODE = "DEFAULT";
public UserAlreadyExistsException() {
super(POR_ORGA_CODE,ERRCode.USER_ALREADY_EXISTS);
}
public UserAlreadyExistsException(Throwable cause) {
super(POR_ORGA_CODE,ERRCode.USER_ALREADY_EXISTS, cause);
public UserAlreadyExistsException(String porOrgacode) {
super(porOrgacode, ERRCode.USER_ALREADY_EXISTS);
}
public UserAlreadyExistsException(String message) {
super(POR_ORGA_CODE,ERRCode.USER_ALREADY_EXISTS, message);
public UserAlreadyExistsException(String porOrgacode, Throwable cause) {
super(porOrgacode, ERRCode.USER_ALREADY_EXISTS, cause);
}
public UserAlreadyExistsException(String message, Throwable cause) {
super(POR_ORGA_CODE,ERRCode.USER_ALREADY_EXISTS, message, cause);
public UserAlreadyExistsException(String porOrgacode, Object... arguments) {
super(porOrgacode, ERRCode.USER_ALREADY_EXISTS, arguments);
}
}

@ -12,4 +12,5 @@ public class SignupRequest {
private String password;
private String email;
private String fullName;
private String porOrgacode;
}

@ -2,6 +2,7 @@ package com.mfsys.aconnect.security.service;
import com.mfsys.aconnect.security.dto.SignupRequest;
import com.mfsys.aconnect.security.dto.SignupResponse;
import com.mfsys.common.configuration.exception.EmailAlreadyUsedException;
import com.mfsys.common.configuration.exception.UserAlreadyExistsException;
import com.mfsys.common.configuration.service.JwtService;
import com.mfsys.common.configuration.service.PasswordEncryptionService;
@ -51,12 +52,16 @@ public class AuthenticationService {
@Transactional
public SignupResponse signup(SignupRequest request){
SignupResponse response = new SignupResponse();
String porOrgacode = request.getPorOrgacode();
if(userAlreadyExists(request.getUsername())){
throw new UserAlreadyExistsException();
throw new UserAlreadyExistsException(request.getPorOrgacode());
}
userRepository.findByEmailAndIsActiveTrue(request.getEmail())
.ifPresent(existingUser -> {
throw new EmailAlreadyUsedException(porOrgacode, existingUser.getUserId());
});
User user = new User();
user.setUserId(request.getUsername());
user.setPassword(passwordEncryptionService.hashPassword(request.getPassword()));

@ -10,4 +10,5 @@ import java.util.Optional;
public interface UserRepository extends JpaRepository<User, String> {
Optional<User> findByUserIdAndIsActiveTrue(String userId);
Optional<User> findByEmail(String email);
Optional<User> findByEmailAndIsActiveTrue(String email);
}

Loading…
Cancel
Save