Created a new endpoint to update user

This commit is contained in:
rajesh
2025-02-06 20:33:40 +05:30
parent eeb01ab8fa
commit da677ca039
6 changed files with 116 additions and 2 deletions

View File

@@ -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);
}
}

View File

@@ -0,0 +1,18 @@
package net.gepafin.tendermanagement.model.request;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class UpdateUserReqForBeneficiary {
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String organization;
private String address;
private String city;
private String country;
private LocalDateTime dateOfBirth;
}

View File

@@ -47,4 +47,6 @@ public interface UserService {
public UserEntity getUserEntityById(Long userId);
List<UserResponseBean> getAllUsers(HttpServletRequest request, List<Long> roleIds);
UserResponseBean updateUserDetails(HttpServletRequest request, Long userId, UpdateUserReqForBeneficiary userReq);
}

View File

@@ -130,4 +130,10 @@ public class UserServiceImpl implements UserService {
UserEntity user=validator.validateUser(request);
return userDao.getAllUsers(user, roleIds);
}
@Override
@Transactional(rollbackFor = Exception.class)
public UserResponseBean updateUserDetails(HttpServletRequest request, Long userId, UpdateUserReqForBeneficiary userReq) {
return userDao.updateUserDetails(request , userId, userReq);
}
}

View File

@@ -47,7 +47,7 @@ public interface UserApi {
return new ResponseEntity<Response<JWTToken>>(HttpStatus.NOT_IMPLEMENTED);
}
@Operation(summary = "Api to update user",
@Operation(summary = "Api to update user (Only for super admin)",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@@ -243,6 +243,24 @@ public interface UserApi {
@RequestMapping("favicon.ico")
@ResponseBody
void returnNoFavicon();
@Operation(summary = "Api to update user",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE)})),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE)})),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE)}))})
@RequestMapping(value = "/{userId}/update-details",
produces = {"application/json"},
method = RequestMethod.PUT)
default ResponseEntity<Response<UserResponseBean>> updateUserDetails(HttpServletRequest request,
@Parameter(description = "The user id", required = true) @PathVariable("userId") Long userId,
@Parameter(description = "User request object", required = true) @Valid @RequestBody UpdateUserReqForBeneficiary userReq) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}

View File

@@ -247,5 +247,21 @@ public class UserApiController implements UserApi {
public void returnNoFavicon() {
// Do nothing
}
@Override
public ResponseEntity<Response<UserResponseBean>> updateUserDetails(HttpServletRequest request,
@PathVariable("userId") Long userId,
@Valid @RequestBody UpdateUserReqForBeneficiary userReq) {
log.info("Update User for Beneficiary- User ID: {}, Request Body: {}", userId, userReq);
/** This code is responsible for "Updating user details by beneficiary" operation. **/
loggingUtil.logUserAction(UserActionRequest.builder().request(request).actionType(UserActionLogsEnum.UPDATE)
.actionContext(UserActionContextEnum.UPDATE_USER_DETAILS).build());
UserResponseBean updatedUser = userService.updateUserDetails(request, userId, userReq);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(updatedUser, Status.SUCCESS, Translator.toLocale(GepafinConstant.USER_UPDATED_SUCCESS_MSG)));
}
}