Skip to content

Commit

Permalink
Added code comments and fixme todo as per code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kanishq-egov committed Jun 3, 2024
1 parent 4912482 commit 7a885fa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,26 +342,48 @@ public Long getPosition() {
* @return
*/
public EmployeeResponse update(EmployeeRequest employeeRequest) {
// Extracting request information from the employee request
RequestInfo requestInfo = employeeRequest.getRequestInfo();

// 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())) {
tenantId = employeeRequest.getEmployees().get(0).getTenantId();
}

List <String> uuidList= new ArrayList<>();
for(Employee employee: employeeRequest.getEmployees()) {
// 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).tenantId(tenantId).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 @@ -395,9 +417,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()
.get();

// 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
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
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 7a885fa

Please sign in to comment.