Skip to content

Commit

Permalink
HLM Health-HRMS bug fix, user was set to null in hrms update as the t… (
Browse files Browse the repository at this point in the history
#761)

* HLM Health-HRMS bug fix, user was set to null in hrms update as the tenantid was not provided during internal search

* HLM updated hrms user type

* updated type from individual create

* Addressed code review comments

* Added code comments and fixme todo as per code review comments

* HLM updated flyway migration docker version

* Revert "HLM updated flyway migration docker version"

This reverts commit 1f1167e.

* Updated code comments on EmployeeService update method

* Update EmployeeService.java

* Updated code comments on EmployeeService, added changes for NPE handling

---------

Co-authored-by: kavi_elrey@1993 <25226238+kavi-egov@users.noreply.github.com>
  • Loading branch information
kanishq-egov and kavi-egov committed Jun 4, 2024
1 parent a4c94e3 commit 564c7d8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,27 +334,58 @@ public Long getPosition() {
}

/**
* Service method to update user. Performs the following:
* 1. Enriches the employee object with required parameters.
* 2. Updates user by making call to the user service.
*
* Updates the details of employees provided in the EmployeeRequest.
* TODO FIXME
* This method searches and updates the USER, INDIVIDUAL manually
* instead of cascading the update call directly to other services,
* the flow has to be relooked
*
* @param employeeRequest
* @return
*/
public EmployeeResponse update(EmployeeRequest employeeRequest) {
// Extracting request information from the employee request
RequestInfo requestInfo = employeeRequest.getRequestInfo();
List <String> uuidList= new ArrayList<>();
for(Employee employee: employeeRequest.getEmployees()) {

// Initialize tenantId to null
String tenantId = null;

// If employeeRequest is not null and contains employees, extract the tenantId from the first employee
if (employeeRequest != null && !CollectionUtils.isEmpty(employeeRequest.getEmployees()) && !employeeRequest.getEmployees().isEmpty()) {
tenantId = employeeRequest.getEmployees().get(0).getTenantId();
}

// List to store the UUIDs of the employees to be updated
List<String> uuidList = new ArrayList<>();

// Iterate over the employees in the request and collect their UUIDs
for (Employee employee : employeeRequest.getEmployees()) {
uuidList.add(employee.getUuid());
}
EmployeeResponse existingEmployeeResponse = search(EmployeeSearchCriteria.builder().uuids(uuidList).build(),requestInfo);
List <Employee> existingEmployees = existingEmployeeResponse.getEmployees();

// Search for existing employees based on the collected UUIDs and tenantId
EmployeeResponse existingEmployeeResponse = search(
EmployeeSearchCriteria.builder().uuids(uuidList).tenantId(tenantId).build(), requestInfo
);

// Extract the list of existing employees from the search response
List<Employee> existingEmployees = existingEmployeeResponse.getEmployees();

// Iterate over each employee in the request
employeeRequest.getEmployees().stream().forEach(employee -> {
// Enrich the update request with additional information using the existing employee details
enrichUpdateRequest(employee, requestInfo, existingEmployees);
// Update the user information for the employee
updateUser(employee, requestInfo);
});

// Push the updated employee request to the HRMS topic for further processing
hrmsProducer.push(propertiesManager.getUpdateTopic(), employeeRequest);
//notificationService.sendReactivationNotification(employeeRequest);

// (Optional) Send reactivation notifications if needed
// notificationService.sendReactivationNotification(employeeRequest);

// Generate and return the response containing the updated employee information
return generateResponse(employeeRequest);
}

Expand Down Expand Up @@ -388,9 +419,16 @@ private void enrichUpdateRequest(Employee employee, RequestInfo requestInfo, Lis
.createdBy(requestInfo.getUserInfo().getUserName())
.createdDate(new Date().getTime())
.build();
Employee existingEmpData = existingEmployeesData.stream().filter(existingEmployee -> existingEmployee.getUuid().equals(employee.getUuid())).findFirst().get();
// Find the existing employee data matching the current employee's UUID
Employee existingEmpData = existingEmployeesData.stream()
.filter(existingEmployee -> existingEmployee.getUuid().equals(employee.getUuid()))
.findFirst()
.orElseThrow(() -> new CustomException("EMPLOYEE_NOT_FOUND", "Employee not found with UUID: " + employee.getUuid()));

// Set the user's username to the employee's code
employee.getUser().setUserName(employee.getCode());

// Set the user's active status based on the employee's isActive status
if(!employee.getIsActive())
employee.getUser().setActive(false);
else
Expand Down Expand Up @@ -577,4 +615,4 @@ public Map<String,Object> getEmployeeCountResponse(RequestInfo requestInfo, Stri
return response;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,64 @@ public UserResponse createUser(UserRequest userRequest) {
return userResponse;
}

/**
* Updates a user by searching for the corresponding individual and updating their details.
*
* Steps:
* 1. Map UserRequest to IndividualSearchRequest.
* 2. Fetch individual data using tenant ID and search request.
* 3. Return null if no individual is found.
* 4. Prepare an IndividualRequest for update.
* 5. Construct the update endpoint URI.
* 6. Perform REST call to update individual.
* 7. Map response to UserResponse if successful.
* 8. Return UserResponse.
* TODO FIXME
* @param userRequest The request object containing user details to be updated.
* @return UserResponse containing updated user information, or null if no individual was found.
*/
@Override
public UserResponse updateUser(UserRequest userRequest) {
// Map the UserRequest to an IndividualSearchRequest
IndividualSearchRequest individualSearchRequest = mapToIndividualSearchRequest(userRequest);

// Fetch the individual response from the individual service
IndividualBulkResponse individualSearchResponse =
getIndividualResponse(userRequest.getUser().getTenantId(), individualSearchRequest);

UserResponse userResponse = null;
if (individualSearchResponse == null || individualSearchResponse.getIndividual() == null || individualSearchResponse.getIndividual().size() == 0) {
return userResponse;

// Check if the individual search response is null or contains no individuals
if (individualSearchResponse == null || individualSearchResponse.getIndividual() == null || individualSearchResponse.getIndividual().isEmpty()) {
return userResponse; // Return null if no individual is found
}

// Get the first individual from the search response
Individual individual = individualSearchResponse.getIndividual().get(0);

// Map the found individual and the user request to an IndividualRequest for update
IndividualRequest updateRequest = mapToIndividualUpdateRequest(individual, userRequest);

// Build the URI for the update endpoint
StringBuilder uri = new StringBuilder();
uri.append(propertiesManager.getIndividualHost());
uri.append(propertiesManager.getIndividualUpdateEndpoint());

// Make a REST call to update the individual
IndividualResponse response = restCallRepository
.fetchResult(uri, updateRequest, IndividualResponse.class);

// If the response is not null and contains an updated individual, map it to UserResponse
if (response != null && response.getIndividual() != null) {
log.info("response received from individual service");
log.info("Response received from individual service");
userResponse = mapToUserResponse(response);
}

// Return the UserResponse
return userResponse;
}


private IndividualRequest mapToIndividualUpdateRequest(Individual individual, UserRequest userRequest) {
Individual updatedIndividual = Individual.builder()
.id(individual.getId())
Expand Down Expand Up @@ -129,7 +164,7 @@ private IndividualRequest mapToIndividualUpdateRequest(Individual individual, Us
.tenantId(userRequest.getUser().getTenantId())
.description(role.getDescription())
.build()).collect(Collectors.toList()))
.userType(UserType.fromValue(userRequest.getUser().getType()))
.userType(individual.getUserDetails().getUserType())
.build())
.isDeleted(Boolean.FALSE)
.clientAuditDetails(AuditDetails.builder()
Expand Down Expand Up @@ -310,6 +345,7 @@ private static User getUser(Individual individual) {
.userServiceUuid(individual.getUserUuid())
.active(individual.getIsSystemUserActive())
.gender(individual.getGender() != null ? individual.getGender().name() : null)
.type(individual.getUserDetails().getUserType().toString())
.userName(individual.getUserDetails().getUsername())
.emailId(individual.getEmail())
.correspondenceAddress(individual.getAddress() != null && !individual.getAddress().isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public ResponseEntity<?> create(@RequestBody @Valid EmployeeRequest employeeRequ
* EmployeeResponse type or ErrorResponse type
*
* @param employeeRequest
* @param bindingResult
* @return ResponseEntity<?>
*/
@PostMapping(value = "/_update")
Expand Down

0 comments on commit 564c7d8

Please sign in to comment.