Merge pull request #203 from Kitzanos/feature/GEPAFINBE-163
GEPAFINBE-163(Create new endpoint for user update)
This commit is contained in:
@@ -20,6 +20,7 @@ import net.gepafin.tendermanagement.util.LoggingUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.util.Validator;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ForbiddenAccessException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
|
||||
@@ -597,5 +598,58 @@ public class UserDao {
|
||||
return userResponseBeans;
|
||||
}
|
||||
|
||||
public UserResponseBean updateUserDetails(HttpServletRequest request , Long userId, UpdateUserReqForBeneficiary userReq){
|
||||
log.info("Updating user by beneficiary with ID: {}", userId);
|
||||
UserEntity userEntity = validator.validateUserId(request, userId);
|
||||
|
||||
UserEntity oldUserEntity = Utils.getClonedEntityForData(userEntity);
|
||||
log.info("Current user details: {}", userEntity);
|
||||
log.info("New user details: {}", userReq);
|
||||
|
||||
HubEntity hubEntity = hubService.valdateHub(userEntity.getHub().getId());
|
||||
String beneficiaryRoleType = RoleStatusEnum.ROLE_BENEFICIARY.getValue();
|
||||
|
||||
if (validator.checkIsBeneficiary()) {
|
||||
// Validate if the new email already exists for another beneficiary in the same hub
|
||||
boolean emailExistsForBeneficiary = userRepository.existsByEmailIgnoreCaseForBeneficiaries(
|
||||
userReq.getEmail(), hubEntity.getUniqueUuid(), beneficiaryRoleType);
|
||||
|
||||
if (emailExistsForBeneficiary) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.EMAIL_ALREADY_EXISTS));
|
||||
}
|
||||
|
||||
setIfUpdated(userEntity::getEmail,userEntity::setEmail,userReq.getEmail()); // Only update email if role is beneficiary
|
||||
}
|
||||
|
||||
BeneficiaryEntity oldBeneficiaryEntity = null;
|
||||
|
||||
if(userEntity.getBeneficiary()!=null) {
|
||||
oldBeneficiaryEntity = Utils.getClonedEntityForData(userEntity.getBeneficiary());
|
||||
setIfUpdated(userEntity.getBeneficiary()::getFirstName, userEntity.getBeneficiary()::setFirstName, userReq.getFirstName());
|
||||
setIfUpdated(userEntity.getBeneficiary()::getLastName, userEntity.getBeneficiary()::setLastName, userReq.getLastName());
|
||||
setIfUpdated(userEntity.getBeneficiary()::getOrganization, userEntity.getBeneficiary()::setOrganization, userReq.getOrganization());
|
||||
setIfUpdated(userEntity.getBeneficiary()::getAddress, userEntity.getBeneficiary()::setAddress, userReq.getAddress());
|
||||
setIfUpdated(userEntity.getBeneficiary()::getPhoneNumber, userEntity.getBeneficiary()::setPhoneNumber, userReq.getPhoneNumber());
|
||||
setIfUpdated(userEntity.getBeneficiary()::getDateOfBirth, userEntity.getBeneficiary()::setDateOfBirth, userReq.getDateOfBirth());
|
||||
setIfUpdated(userEntity.getBeneficiary()::getCity, userEntity.getBeneficiary()::setCity, userReq.getCity());
|
||||
setIfUpdated(userEntity.getBeneficiary()::getCountry, userEntity.getBeneficiary()::setCountry, userReq.getCountry());
|
||||
|
||||
/** This code is responsible for adding a version history log for the "Update beneficiary details " operation **/
|
||||
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(oldBeneficiaryEntity).newData(userEntity.getBeneficiary()).build());
|
||||
}
|
||||
|
||||
userEntity = userRepository.save(userEntity);
|
||||
|
||||
/** This code is responsible for adding a version history log for the "Update user details by beneficiary" operation **/
|
||||
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(oldUserEntity).newData(userEntity).build());
|
||||
|
||||
return convertUserEntityToUserResponse(userEntity);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user