-
Notifications
You must be signed in to change notification settings - Fork 45
AMM-1141 | Allocate API for allocating grievance records to agents #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a7a9518
e1b5318
eddabe3
6e626bb
6cf8550
2303d60
68d0483
b7718c0
f9feef6
019d315
507f830
21be52e
0a90728
ea9ff9e
8825473
2d639c4
b0e039b
9831500
5cb6886
9e8e1f2
67b2f86
7b3b990
d834aa9
734551a
47ceb56
f26b8ff
d3534d9
6b58965
8d97517
b5798d2
abe5bd3
3c8fbb9
ef1021e
3451ad5
fa02a49
822ec19
c8e85ab
6557199
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,11 @@ | |
|
||
|
||
import com.iemr.common.service.grievance.GrievanceDataSync; | ||
import com.iemr.common.service.grievance.GrievanceHandlingService; | ||
import com.iemr.common.utils.exception.IEMRException; | ||
import com.iemr.common.utils.response.OutputResponse; | ||
|
||
import io.lettuce.core.dynamic.annotation.Param; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
|
||
|
||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.json.JSONException; | ||
|
@@ -16,6 +15,7 @@ | |
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
|
@@ -24,11 +24,15 @@ public class GrievanceController { | |
|
||
|
||
private GrievanceDataSync grievanceDataSync; | ||
|
||
private final GrievanceHandlingService grievanceHandlingService; | ||
|
||
@Autowired | ||
public GrievanceController(GrievanceDataSync grievanceDataSync) { | ||
public GrievanceController(GrievanceHandlingService grievanceHandlingService, GrievanceDataSync grievanceDataSync) { | ||
this.grievanceDataSync = grievanceDataSync; | ||
this.grievanceHandlingService = grievanceHandlingService; | ||
} | ||
|
||
|
||
@CrossOrigin() | ||
@Operation(summary = "/unallocatedGrievanceCount") | ||
|
@@ -53,4 +57,27 @@ public String fetchUnallocatedGrievanceCount() { | |
return responseData.toString(); | ||
|
||
} | ||
|
||
|
||
|
||
@Operation(summary = "Allocate grievances to users") | ||
@PostMapping(value = "/allocateGrievances", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, headers = "Authorization") | ||
public String allocateGrievances(@Param(value = "{\"startDate\":\"ISO-8601 format start date (e.g., 2022-12-01T07:49:00.000Z)\", " | ||
+ "\"endDate\":\"ISO-8601 format end date (e.g., 2025-01-16T07:49:30.561)\", " | ||
+ "\"userID\":\"Array list of User IDs (agents to be allocated grievances)\", " | ||
+ "\"allocateNo\":\"Integer - number of grievances to be allocated to each user\"," | ||
+ "\"language\":\"String - language to filter grievances by\"}") | ||
|
||
@RequestBody String request) { | ||
Comment on lines
+63
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion Enhance API documentation and request validation. The endpoint documentation needs improvement:
@Operation(summary = "Allocate grievances to users")
+@ApiResponse(responseCode = "200", description = "Grievances allocated successfully")
+@ApiResponse(responseCode = "400", description = "Invalid request parameters")
+@ApiResponse(responseCode = "500", description = "Internal server error")
@PostMapping(value = "/allocateGrievances", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, headers = "Authorization")
-public String allocateGrievances(@Param(value = "{\"startDate\":\"ISO-8601 format start date (e.g., 2022-12-01T07:49:00.000Z)\", "
- + "\"endDate\":\"ISO-8601 format end date (e.g., 2025-01-16T07:49:30.561)\", "
- + "\"userID\":\"Array list of User IDs (agents to be allocated grievances)\", "
- + "\"allocateNo\":\"Integer - number of grievances to be allocated to each user\","
- + "\"language\":\"String - language to filter grievances by\"}")
- @RequestBody String request)
+public ResponseEntity<OutputResponse> allocateGrievances(
+ @Parameter(description = "Start date in ISO-8601 format", required = true)
+ @RequestBody @Valid GrievanceAllocationRequest request)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not required There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
OutputResponse response = new OutputResponse(); | ||
try { | ||
// Call the service to allocate grievances based on the incoming JSON request | ||
response.setResponse(grievanceHandlingService.allocateGrievances(request)); | ||
} catch (Exception e) { | ||
logger.error("Grievance allocation failed with error: " + e.getMessage(), e); | ||
response.setError(e); | ||
} | ||
return response.toString(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.iemr.common.data.grievance; | ||
|
||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public class GrievanceAllocationRequest { | ||
|
||
private LocalDateTime startDate; // Start date for filtering grievances | ||
private LocalDateTime endDate; // End date for filtering grievances | ||
private List<Integer> userID; // List of user IDs (agents) to whom grievances will be allocated | ||
private Integer allocateNo; // Number of grievances to be allocated to each user | ||
private String language; | ||
// Getters and Setters | ||
|
||
public LocalDateTime getStartDate() { | ||
return startDate; | ||
} | ||
|
||
public void setStartDate(LocalDateTime startDate) { | ||
this.startDate = startDate; | ||
} | ||
|
||
public LocalDateTime getEndDate() { | ||
return endDate; | ||
} | ||
|
||
public void setEndDate(LocalDateTime endDate) { | ||
this.endDate = endDate; | ||
} | ||
|
||
public List<Integer> getUserID() { | ||
return userID; | ||
} | ||
|
||
public void setUserID(List<Integer> userID) { | ||
this.userID = userID; | ||
} | ||
|
||
public Integer getAllocateNo() { | ||
return allocateNo; | ||
} | ||
|
||
public void setAllocateNo(Integer allocateNo) { | ||
this.allocateNo = allocateNo; | ||
} | ||
|
||
public String getLanguage() { | ||
return language; | ||
} | ||
|
||
public void setLanguage(String language) { | ||
this.language = language; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "GrievanceAllocationRequest{" + | ||
"startDate=" + startDate + | ||
", endDate=" + endDate + | ||
", userID=" + userID + | ||
", allocateNo=" + allocateNo + | ||
", language=" + language + | ||
'}'; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.iemr.common.service.grievance; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public interface GrievanceHandlingService { | ||
public String allocateGrievances(String request) throws Exception; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.iemr.common.service.grievance; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.iemr.common.data.grievance.GrievanceAllocationRequest; | ||
import com.iemr.common.data.grievance.GrievanceDetails; | ||
import com.iemr.common.repository.grievance.GrievanceDataRepo; | ||
import com.iemr.common.utils.mapper.InputMapper; | ||
|
||
@Service | ||
public class GrievanceHandlingServiceImpl implements GrievanceHandlingService { | ||
|
||
|
||
private Logger logger = LoggerFactory.getLogger(GrievanceHandlingServiceImpl.class); | ||
|
||
|
||
private final GrievanceDataRepo grievanceDataRepo; | ||
|
||
@Autowired | ||
public GrievanceHandlingServiceImpl(GrievanceDataRepo grievanceDataRepo) { | ||
this.grievanceDataRepo = grievanceDataRepo; | ||
} | ||
|
||
|
||
@Override | ||
public String allocateGrievances(String request) throws Exception { | ||
// Step 1: Parse the request string into the appropriate GrievanceAllocationRequest object | ||
GrievanceAllocationRequest allocationRequest = InputMapper.gson().fromJson(request, GrievanceAllocationRequest.class); | ||
|
||
// Step 2: Fetch grievances based on the start date, end date range, and language | ||
List<GrievanceDetails> grievances = grievanceDataRepo.findGrievancesInDateRangeAndLanguage( | ||
allocationRequest.getStartDate(), allocationRequest.getEndDate(), allocationRequest.getLanguage()); | ||
|
||
if (grievances.isEmpty()) { | ||
throw new Exception("No grievances found in the given date range and language."); | ||
} | ||
|
||
// Step 3: Sort grievances in ascending order based on creation date | ||
grievances.sort(Comparator.comparing(GrievanceDetails::getCreatedDate)); | ||
|
||
// Step 4: Get the allocation parameters from the request | ||
int totalAllocated = 0; | ||
int userIndex = 0; | ||
List<Integer> userIds = allocationRequest.getUserID(); | ||
int allocateNo = allocationRequest.getAllocateNo(); // Number of grievances to allocate per user | ||
|
||
|
||
for (int i = 0; i < grievances.size(); i++) { | ||
Integer userId = userIds.get(userIndex); | ||
GrievanceDetails grievance = grievances.get(i); | ||
|
||
int rowsAffected = grievanceDataRepo.allocateGrievance(grievance.getGrievanceId(), userId); | ||
if (rowsAffected > 0) { | ||
totalAllocated++; | ||
logger.debug("Allocated grievance ID {} to user ID {}", grievance.getGrievanceId(), userId); | ||
} else { | ||
logger.error("Failed to allocate grievance ID {} to user ID {}", grievance.getGrievanceId(), userId); | ||
} | ||
|
||
// Move to the next user after allocateNo grievances | ||
if ((i + 1) % allocateNo == 0) { | ||
userIndex = (userIndex + 1) % userIds.size(); | ||
} | ||
} | ||
|
||
// Step 6: Return a message with the total number of grievances allocated | ||
return "Successfully allocated " + totalAllocated + " grievances to users."; | ||
} | ||
|
||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace lettuce.core @param with OpenAPI @parameter annotation.
The
@Param
annotation fromio.lettuce.core
is intended for Redis operations, not API documentation. Since you're using OpenAPI/Swagger for documentation, use@Parameter
fromio.swagger.v3.oas.annotations
instead.π Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.