updated code for create user api

This commit is contained in:
rajesh
2024-09-26 17:31:44 +05:30
parent 5f6efca3ff
commit 40e5ab9e91
7 changed files with 86 additions and 29 deletions

View File

@@ -4,6 +4,7 @@ import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.RegionEntity;
import net.gepafin.tendermanagement.entities.RoleEntity;
import net.gepafin.tendermanagement.enums.RoleStatusEnum;
import net.gepafin.tendermanagement.model.request.RoleReq;
import net.gepafin.tendermanagement.model.response.RegionResponseBean;
import net.gepafin.tendermanagement.model.response.RoleResponseBean;
@@ -119,4 +120,8 @@ public class RoleDao {
log.info("Total roles found: {}", roles.size());
return roles;
}
public RoleEntity getRoleByType(RoleStatusEnum roleStatus) {
return roleRepository.findByRoleType(roleStatus.getValue());
}
}

View File

@@ -6,6 +6,7 @@ import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.RoleEntity;
import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.enums.RoleStatusEnum;
import net.gepafin.tendermanagement.enums.UserStatusEnum;
import net.gepafin.tendermanagement.model.request.*;
import net.gepafin.tendermanagement.model.response.RoleResponseBean;
@@ -28,6 +29,8 @@ import org.springframework.stereotype.Repository;
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
import java.util.regex.Pattern;
@Repository
public class UserDao {
@@ -46,32 +49,57 @@ public class UserDao {
private RoleDao roleDao;
public JWTToken createUser(HttpServletRequest request, String tempToken, UserReq userReq) {
log.info("Creating user with email: {}", userReq.getEmail());
if (userRepository.existsByEmailIgnoreCase(userReq.getEmail())) {
log.error("User creation failed: Email {} already exists", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.EMAIL_ALREADY_EXISTS));
}
if (Boolean.FALSE.equals(StringUtils.isEmpty(userReq.getCodiceFiscale())) && userRepository.existsByCodiceFiscale(userReq.getCodiceFiscale())) {
log.error("User creation failed: CodiceFiscale {} already exists", userReq.getCodiceFiscale());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.CODICE_FISCALE_EXISTS));
}
if(tempToken == null && (StringUtils.isEmpty(userReq.getPassword()) || StringUtils.isEmpty(userReq.getConfPassword()))) {
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.INVALID_REQUEST));
}
if (tempToken == null && !userReq.getPassword().equals(userReq.getConfPassword())) {
log.error("User creation failed: Passwords do not match for email {}", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_DOESNT_MATCH));
}
if (tempToken == null && userReq.getPassword().length() < 8) {
log.error("User creation failed: Password length is less than 8 characters for email {}", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_MIN_LEN));
}
if (Boolean.FALSE.equals(isValidEmail(userReq.getEmail()))) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.VALIDATE_EMAIL));
}
log.info("Creating user with email: {}", userReq.getEmail());
if (userRepository.existsByEmailIgnoreCase(userReq.getEmail())) {
log.error("User creation failed: Email {} already exists", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.EMAIL_ALREADY_EXISTS));
}
if (Boolean.FALSE.equals(StringUtils.isEmpty(userReq.getCodiceFiscale()))
&& userRepository.existsByCodiceFiscale(userReq.getCodiceFiscale())) {
log.error("User creation failed: CodiceFiscale {} already exists", userReq.getCodiceFiscale());
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.CODICE_FISCALE_EXISTS));
}
if (tempToken == null && userReq.getRoleId() == null) {
throw new ResourceNotFoundException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.ROLE_ID_MANDATORY));
}
if(tempToken != null) {
userReq.setRoleId(null);
}
validatePassword(userReq.getPassword(), userReq.getConfPassword(), tempToken);
UserEntity userEntity = convertUserRequestToUserEntity(userReq);
userEntity = userRepository.save(userEntity);
log.info("User created with ID: {}", userEntity.getId());
return authService.getJWTTokenBean(userEntity, Boolean.TRUE);
}
private void validatePassword(String password, String confirmPassword, String tempToken) {
if (StringUtils.isEmpty(password) || StringUtils.isEmpty(confirmPassword)) {
if(tempToken == null) {
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.VALIDATE_PASSWORD));
}else if(Boolean.FALSE.equals(StringUtils.isEmpty(password) && StringUtils.isEmpty(confirmPassword))){
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.VALIDATE_PASSWORD));
}
}
if (password != null && !password.equals(confirmPassword)) {
log.error("User creation failed: Passwords do not match");
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_DOESNT_MATCH));
}
if (password != null && password.length() < 8) {
log.error("User creation failed: Password length is less than 8 characters");
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_MIN_LEN));
}
}
public UserResponseBean updateUser(Long userId, UpdateUserReq userReq) {
log.info("Updating user with ID: {}", userId);
@@ -108,13 +136,21 @@ public class UserDao {
userEntity.setOrganization(userReq.getOrganization());
userEntity.setAddress(userReq.getAddress());
userEntity.setPhoneNumber(userReq.getPhoneNumber());
userEntity.setRoleEntity(roleDao.validateRole(userReq.getRoleId()));
userEntity.setRoleEntity(getRoleEntity(userReq.getRoleId()));
userEntity.setCodiceFiscale(userReq.getCodiceFiscale());
userEntity.setDateOfBirth(userReq.getDateOfBirth());
return userEntity;
}
private UserResponseBean convertUserEntityToUserResponse(UserEntity userEntity) {
private RoleEntity getRoleEntity(Long roleId) {
if(roleId != null) {
return roleDao.validateRole(roleId);
} else {
return roleDao.getRoleByType(RoleStatusEnum.ROLE_BENEFICIARY);
}
}
private UserResponseBean convertUserEntityToUserResponse(UserEntity userEntity) {
UserResponseBean userResponseBean = new UserResponseBean();
userResponseBean.setId(userEntity.getId());
userResponseBean.setCreatedDate(userEntity.getCreatedDate());
@@ -225,6 +261,16 @@ public class UserDao {
authService.logout(request, response);
log.info("User successfully logged out.");
}
public static Boolean isValidEmail(String email) {
String EMAIL_REGEX = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
if (email == null || email.isEmpty()) {
return false;
}
Pattern pattern = Pattern.compile(EMAIL_REGEX);
return pattern.matcher(email).matches();
}
public UserResponseBean updateUserStatus(Long userId, UserStatusEnum statusReq) {
log.info("Updating status for user with ID: {}", userId);