Created beneficiary and associated with user
This commit is contained in:
@@ -4,6 +4,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.BeneficiaryEntity;
|
||||
import net.gepafin.tendermanagement.entities.RoleEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.enums.RoleStatusEnum;
|
||||
@@ -13,6 +14,7 @@ import net.gepafin.tendermanagement.model.response.RoleResponseBean;
|
||||
import net.gepafin.tendermanagement.model.response.UserSamlResponse;
|
||||
import net.gepafin.tendermanagement.model.response.UserResponseBean;
|
||||
import net.gepafin.tendermanagement.model.util.JWTToken;
|
||||
import net.gepafin.tendermanagement.repositories.BeneficiaryRepository;
|
||||
import net.gepafin.tendermanagement.repositories.UserRepository;
|
||||
import net.gepafin.tendermanagement.service.impl.AuthenticationService;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
@@ -47,9 +49,41 @@ public class UserDao {
|
||||
|
||||
@Autowired
|
||||
private RoleDao roleDao;
|
||||
|
||||
@Autowired
|
||||
private BeneficiaryRepository beneficiaryRepository;
|
||||
|
||||
|
||||
public JWTToken createUser(HttpServletRequest request, String tempToken, UserReq userReq) {
|
||||
|
||||
validateUserRequest(tempToken, userReq);
|
||||
validatePassword(userReq.getPassword(), userReq.getConfPassword(), tempToken);
|
||||
|
||||
RoleEntity roleEntity = getRoleEntity(userReq.getRoleId());
|
||||
BeneficiaryEntity beneficiary = createBeneficiary(roleEntity, userReq);
|
||||
UserEntity userEntity = convertUserRequestToUserEntity(beneficiary, roleEntity, userReq);
|
||||
log.info("User created with ID: {}", userEntity.getId());
|
||||
return authService.getJWTTokenBean(userEntity, Boolean.TRUE);
|
||||
}
|
||||
|
||||
private BeneficiaryEntity createBeneficiary(RoleEntity roleEntity, UserReq userReq) {
|
||||
BeneficiaryEntity beneficiaryEntity = null;
|
||||
if (RoleStatusEnum.ROLE_BENEFICIARY.getValue().equals(roleEntity.getRoleType())) {
|
||||
beneficiaryEntity = new BeneficiaryEntity();
|
||||
beneficiaryEntity.setAddress(userReq.getAddress());
|
||||
beneficiaryEntity.setCity(userReq.getCity());
|
||||
beneficiaryEntity.setCodiceFiscale(userReq.getCodiceFiscale());
|
||||
beneficiaryEntity.setCountry(userReq.getCountry());
|
||||
beneficiaryEntity.setDateOfBirth(userReq.getDateOfBirth());
|
||||
beneficiaryEntity.setEmail(userReq.getEmail());
|
||||
beneficiaryEntity.setFirstName(userReq.getFirstName());
|
||||
beneficiaryEntity.setLastName(userReq.getLastName());
|
||||
beneficiaryEntity.setOrganization(userReq.getOrganization());
|
||||
beneficiaryEntity.setPhoneNumber(userReq.getPhoneNumber());
|
||||
}
|
||||
return beneficiaryRepository.save(beneficiaryEntity);
|
||||
}
|
||||
|
||||
private void validateUserRequest(String tempToken, UserReq userReq) {
|
||||
if (Boolean.FALSE.equals(isValidEmail(userReq.getEmail()))) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.VALIDATE_EMAIL));
|
||||
@@ -61,27 +95,21 @@ public class UserDao {
|
||||
Translator.toLocale(GepafinConstant.EMAIL_ALREADY_EXISTS));
|
||||
}
|
||||
if (Boolean.FALSE.equals(StringUtils.isEmpty(userReq.getCodiceFiscale()))
|
||||
&& userRepository.existsByCodiceFiscale(userReq.getCodiceFiscale())) {
|
||||
&& userRepository.existsByBeneficiaryCodiceFiscale(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));
|
||||
throw new ResourceNotFoundException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.ROLE_ID_MANDATORY));
|
||||
}
|
||||
if(tempToken != null) {
|
||||
userReq.setRoleId(null);
|
||||
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) {
|
||||
}
|
||||
|
||||
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));
|
||||
@@ -124,22 +152,24 @@ public class UserDao {
|
||||
return convertUserEntityToUserResponse(userEntity);
|
||||
}
|
||||
|
||||
private UserEntity convertUserRequestToUserEntity(UserReq userReq) {
|
||||
private UserEntity convertUserRequestToUserEntity(BeneficiaryEntity beneficiary, RoleEntity roleEntity, UserReq userReq) {
|
||||
UserEntity userEntity = new UserEntity();
|
||||
if(Boolean.FALSE.equals(StringUtils.isEmpty(userReq.getPassword()))) {
|
||||
userEntity.setPassword(passwordEncoder.encode(userReq.getPassword()));
|
||||
}
|
||||
userEntity.setRoleEntity(roleEntity);
|
||||
userEntity.setEmail(userReq.getEmail());
|
||||
userEntity.setFirstName(userReq.getFirstName());
|
||||
userEntity.setStatus(UserStatusEnum.ACTIVE.getValue());
|
||||
userEntity.setLastName(userReq.getLastName());
|
||||
userEntity.setOrganization(userReq.getOrganization());
|
||||
userEntity.setAddress(userReq.getAddress());
|
||||
userEntity.setPhoneNumber(userReq.getPhoneNumber());
|
||||
userEntity.setRoleEntity(getRoleEntity(userReq.getRoleId()));
|
||||
userEntity.setCodiceFiscale(userReq.getCodiceFiscale());
|
||||
userEntity.setDateOfBirth(userReq.getDateOfBirth());
|
||||
return userEntity;
|
||||
userEntity.setBeneficiary(beneficiary);
|
||||
if (Boolean.FALSE.equals(RoleStatusEnum.ROLE_BENEFICIARY.getValue().equals(roleEntity.getRoleType()))) {
|
||||
userEntity.setFirstName(userReq.getFirstName());
|
||||
userEntity.setLastName(userReq.getLastName());
|
||||
userEntity.setOrganization(userReq.getOrganization());
|
||||
userEntity.setAddress(userReq.getAddress());
|
||||
userEntity.setPhoneNumber(userReq.getPhoneNumber());
|
||||
userEntity.setDateOfBirth(userReq.getDateOfBirth());
|
||||
}
|
||||
return userRepository.save(userEntity);
|
||||
}
|
||||
|
||||
private RoleEntity getRoleEntity(Long roleId) {
|
||||
@@ -151,37 +181,48 @@ public class UserDao {
|
||||
}
|
||||
|
||||
private UserResponseBean convertUserEntityToUserResponse(UserEntity userEntity) {
|
||||
UserResponseBean userResponseBean = new UserResponseBean();
|
||||
userResponseBean.setId(userEntity.getId());
|
||||
userResponseBean.setCreatedDate(userEntity.getCreatedDate());
|
||||
userResponseBean.setUpdatedDate(userEntity.getUpdatedDate());
|
||||
userResponseBean.setEmail(userEntity.getEmail());
|
||||
userResponseBean.setFirstName(userEntity.getFirstName());
|
||||
userResponseBean.setLastName(userEntity.getLastName());
|
||||
userResponseBean.setPhoneNumber(userEntity.getPhoneNumber());
|
||||
userResponseBean.setOrganization(userEntity.getOrganization());
|
||||
userResponseBean.setAddress(userEntity.getAddress());
|
||||
userResponseBean.setCity(userEntity.getCity());
|
||||
userResponseBean.setCountry(userEntity.getCountry());
|
||||
userResponseBean.setStatus(UserStatusEnum.valueOf(userEntity.getStatus()));
|
||||
RoleResponseBean roleResponseBean = roleDao.convertRoleEntityToRoleResponse(userEntity.getRoleEntity());
|
||||
userResponseBean.setRole(roleResponseBean);
|
||||
userResponseBean.setLastLogin(userEntity.getLastLogin());
|
||||
userResponseBean.setCodiceFiscale(userEntity.getCodiceFiscale());
|
||||
userResponseBean.setDateOfBirth(userEntity.getDateOfBirth());
|
||||
return userResponseBean;
|
||||
}
|
||||
UserResponseBean userResponseBean = new UserResponseBean();
|
||||
userResponseBean.setId(userEntity.getId());
|
||||
userResponseBean.setCreatedDate(userEntity.getCreatedDate());
|
||||
userResponseBean.setUpdatedDate(userEntity.getUpdatedDate());
|
||||
userResponseBean.setEmail(userEntity.getEmail());
|
||||
userResponseBean.setStatus(UserStatusEnum.valueOf(userEntity.getStatus()));
|
||||
RoleResponseBean roleResponseBean = roleDao.convertRoleEntityToRoleResponse(userEntity.getRoleEntity());
|
||||
userResponseBean.setRole(roleResponseBean);
|
||||
userResponseBean.setLastLogin(userEntity.getLastLogin());
|
||||
if (userEntity.getBeneficiary() == null) {
|
||||
userResponseBean.setFirstName(userEntity.getFirstName());
|
||||
userResponseBean.setLastName(userEntity.getLastName());
|
||||
userResponseBean.setPhoneNumber(userEntity.getPhoneNumber());
|
||||
userResponseBean.setOrganization(userEntity.getOrganization());
|
||||
userResponseBean.setAddress(userEntity.getAddress());
|
||||
userResponseBean.setCity(userEntity.getCity());
|
||||
userResponseBean.setCountry(userEntity.getCountry());
|
||||
userResponseBean.setDateOfBirth(userEntity.getDateOfBirth());
|
||||
} else {
|
||||
userResponseBean.setFirstName(userEntity.getBeneficiary().getFirstName());
|
||||
userResponseBean.setLastName(userEntity.getBeneficiary().getLastName());
|
||||
userResponseBean.setPhoneNumber(userEntity.getBeneficiary().getPhoneNumber());
|
||||
userResponseBean.setOrganization(userEntity.getBeneficiary().getOrganization());
|
||||
userResponseBean.setAddress(userEntity.getBeneficiary().getAddress());
|
||||
userResponseBean.setCity(userEntity.getBeneficiary().getCity());
|
||||
userResponseBean.setCountry(userEntity.getBeneficiary().getCountry());
|
||||
userResponseBean.setCodiceFiscale(userEntity.getBeneficiary().getCodiceFiscale());
|
||||
userResponseBean.setDateOfBirth(userEntity.getBeneficiary().getDateOfBirth());
|
||||
}
|
||||
return userResponseBean;
|
||||
}
|
||||
|
||||
public UserResponseBean getUserById(Long id) {
|
||||
log.info("Fetching user with ID: {}", id);
|
||||
UserEntity userEntity=validateUser(id);
|
||||
public UserResponseBean getUserById(Long id) {
|
||||
log.info("Fetching user with ID: {}", id);
|
||||
UserEntity userEntity = validateUser(id);
|
||||
// if (!UserStatusEnum.ACTIVE.getValue().equals(userEntity.getStatus())) {
|
||||
// log.info("User with ID: {} is not active", id);
|
||||
// throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG));
|
||||
// }
|
||||
log.info("User found: {}", userEntity);
|
||||
return convertUserEntityToUserResponse(userEntity);
|
||||
}
|
||||
log.info("User found: {}", userEntity);
|
||||
return convertUserEntityToUserResponse(userEntity);
|
||||
}
|
||||
|
||||
public void deleteUser(Long id) {
|
||||
log.info("Deleting user with ID: {}", id);
|
||||
|
||||
Reference in New Issue
Block a user