Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ public String completeGrievanceCall(
return response.toString();
}



@Operation(summary = "Get Grievance Details with Remarks")
@PostMapping(value = "/getCompleteGrievanceDetails", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, headers = "Authorization")
public String getGrievanceDetailsWithRemarks(@RequestBody String request) {
OutputResponse response = new OutputResponse();
try {
response.setResponse(grievanceHandlingService.getGrievanceDetailsWithRemarks(request));
} catch (Exception e) {
logger.error("getGrievanceDetailsWithRemarks 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,70 @@
package com.iemr.common.data.grievance;

import java.sql.Timestamp;

public class GrievanceResponse {
private Long grievanceId;
private String complaintID;
private String primaryNumber;
private String complaintResolution;
private String remarks;
private Timestamp createdDate;
private Timestamp lastModDate;

// Getters and Setters
public Long getGrievanceId() {
return grievanceId;
}

public void setGrievanceId(Long grievanceId) {
this.grievanceId = grievanceId;
}

public String getComplaintID() {
return complaintID;
}

public void setComplaintID(String complaintID) {
this.complaintID = complaintID;
}

public String getPrimaryNumber() {
return primaryNumber;
}

public void setPrimaryNumber(String primaryNumber) {
this.primaryNumber = primaryNumber;
}

public String getComplaintResolution() {
return complaintResolution;
}

public void setComplaintResolution(String complaintResolution) {
this.complaintResolution = complaintResolution;
}

public String getRemarks() {
return remarks;
}

public void setRemarks(String remarks) {
this.remarks = remarks;
}

public Timestamp getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Timestamp createdDate) {
this.createdDate = createdDate;
}

public Timestamp getLastModDate() {
return lastModDate;
}

public void setLastModDate(Timestamp lastModDate) {
this.lastModDate = lastModDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,8 @@ public ArrayList<BeneficiaryCall> getExistingBCByCallIDAndAgentID(@Param("callID
public int updateBeneficiaryRegIDInCall(@Param("benCallID") Long benCallID, @Param("beneficiaryRegID") Long beneficiaryRegID);

BeneficiaryCall findByBenCallID(Long benCallID);

@Query("SELECT b.remarks FROM BeneficiaryCall b WHERE b.beneficiaryRegID = :beneficiaryRegID")
List<Object[]> fetchBenCallRemarks(@Param("beneficiaryRegID") Long beneficiaryRegID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -123,5 +124,25 @@ public int updateCallCounter(@Param("callCounter") Integer callCounter,
@Param("beneficiaryRegID") Long beneficiaryRegID,
@Param("providerServiceMapID") Integer providerServiceMapID,
@Param("userID") Integer userID);

@Query("SELECT g FROM GrievanceDetails g WHERE "
+ "(g.state = :state OR :state IS NULL) "
+ "AND (g.complaintResolution = :complaintResolution OR :complaintResolution IS NULL) "
+ "AND g.createdDate BETWEEN :startDate AND :endDate")
List<GrievanceDetails> fetchGrievanceDetailsBasedOnParams(
@Param("state") String state,
@Param("complaintResolution") String complaintResolution,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
Comment on lines +128 to +136
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Good query design but consider including deleted filter.

The query handles optional parameters well, but it doesn't filter out deleted records. Based on other repository methods (like line 41), it appears that deleted is a field used for soft deletion.

@Query("SELECT g FROM GrievanceDetails g WHERE "
        + "(g.state = :state OR :state IS NULL) "
        + "AND (g.complaintResolution = :complaintResolution OR :complaintResolution IS NULL) "
-        + "AND g.createdDate BETWEEN :startDate AND :endDate")
+        + "AND g.createdDate BETWEEN :startDate AND :endDate "
+        + "AND g.deleted = false")
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Query("SELECT g FROM GrievanceDetails g WHERE "
+ "(g.state = :state OR :state IS NULL) "
+ "AND (g.complaintResolution = :complaintResolution OR :complaintResolution IS NULL) "
+ "AND g.createdDate BETWEEN :startDate AND :endDate")
List<GrievanceDetails> fetchGrievanceDetailsBasedOnParams(
@Param("state") String state,
@Param("complaintResolution") String complaintResolution,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
@Query("SELECT g FROM GrievanceDetails g WHERE "
+ "(g.state = :state OR :state IS NULL) "
+ "AND (g.complaintResolution = :complaintResolution OR :complaintResolution IS NULL) "
+ "AND g.createdDate BETWEEN :startDate AND :endDate "
+ "AND g.deleted = false")
List<GrievanceDetails> fetchGrievanceDetailsBasedOnParams(
@Param("state") String state,
@Param("complaintResolution") String complaintResolution,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srishtigrp78 I understand that filtering out deleted records is not required for this query. I'll respect your decision on this implementation choice.


✏️ Learnings added
Learnt from: srishtigrp78
PR: PSMRI/Common-API#166
File: src/main/java/com/iemr/common/repository/grievance/GrievanceDataRepo.java:128-136
Timestamp: 2025-03-03T05:09:09.331Z
Learning: For the GrievanceDataRepo's fetchGrievanceDetailsBasedOnParams method, filtering out deleted records with "AND g.deleted = false" is not required as per the developer's decision.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.



@Query("SELECT g FROM GrievanceDetails g WHERE g.complaintID = :complaintID")
List<GrievanceDetails> fetchGrievanceWorklistByComplaintID(@Param("complaintID") String complaintID);


@Query("SELECT g.remarks FROM GrievanceDetails g WHERE g.complaintID = :complaintID")
List<Object[]> fetchGrievanceWorklistRemarks(@Param("complaintID") String complaintID);
Comment on lines +143 to +144
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Simplify return type and add deleted filter.

The query selects only one column (remarks) but returns List<Object[]>. Consider returning List<String> instead. Also, include deleted record filtering.

-@Query("SELECT g.remarks FROM GrievanceDetails g WHERE g.complaintID = :complaintID")
-List<Object[]> fetchGrievanceWorklistRemarks(@Param("complaintID") String complaintID);
+@Query("SELECT g.remarks FROM GrievanceDetails g WHERE g.complaintID = :complaintID AND g.deleted = false")
+List<String> fetchGrievanceWorklistRemarks(@Param("complaintID") String complaintID);
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Query("SELECT g.remarks FROM GrievanceDetails g WHERE g.complaintID = :complaintID")
List<Object[]> fetchGrievanceWorklistRemarks(@Param("complaintID") String complaintID);
@Query("SELECT g.remarks FROM GrievanceDetails g WHERE g.complaintID = :complaintID AND g.deleted = false")
List<String> fetchGrievanceWorklistRemarks(@Param("complaintID") String complaintID);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srishtigrp78 I understand that you don't want to implement this change. Thank you for letting me know - I'll respect your decision and won't pursue this suggestion further.




}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface GrievanceHandlingService {
public List<GrievanceWorklistDTO> getFormattedGrievanceData(String request) throws Exception;

public String saveComplaintResolution(String request) throws Exception;

public String getGrievanceDetailsWithRemarks(String request) throws Exception;


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.iemr.common.service.grievance;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
Expand All @@ -16,13 +21,16 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iemr.common.data.grievance.GetGrievanceWorklistRequest;
import com.iemr.common.data.grievance.GrievanceAllocationRequest;
import com.iemr.common.data.grievance.GrievanceDetails;
import com.iemr.common.data.grievance.GrievanceReallocationRequest;
import com.iemr.common.data.grievance.GrievanceResponse;
import com.iemr.common.data.grievance.MoveToBinRequest;
import com.iemr.common.dto.grivance.GrievanceTransactionDTO;
import com.iemr.common.dto.grivance.GrievanceWorklistDTO;
import com.iemr.common.repository.callhandling.BeneficiaryCallRepository;
import com.iemr.common.repository.grievance.GrievanceDataRepo;
import com.iemr.common.repository.grievance.GrievanceOutboundRepository;
import com.iemr.common.utils.exception.IEMRException;
Expand All @@ -37,11 +45,14 @@ public class GrievanceHandlingServiceImpl implements GrievanceHandlingService {

private final GrievanceDataRepo grievanceDataRepo;
private final GrievanceOutboundRepository grievanceOutboundRepo;
private final BeneficiaryCallRepository beneficiaryCallRepo;

@Autowired
public GrievanceHandlingServiceImpl(GrievanceDataRepo grievanceDataRepo, GrievanceOutboundRepository grievanceOutboundRepo) {
public GrievanceHandlingServiceImpl(GrievanceDataRepo grievanceDataRepo, GrievanceOutboundRepository grievanceOutboundRepo,
BeneficiaryCallRepository beneficiaryCallRepo) {
this.grievanceDataRepo = grievanceDataRepo;
this.grievanceOutboundRepo = grievanceOutboundRepo;
this.beneficiaryCallRepo = beneficiaryCallRepo;
}

@Value("${grievanceAllocationRetryConfiguration}")
Expand Down Expand Up @@ -386,5 +397,128 @@ public String saveComplaintResolution(String request) throws Exception {
throw new Exception("Failed to update complaint resolution");
}
}




private Date parseDate(String dateStr) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return dateFormat.parse(dateStr);
} catch (ParseException e) {
logger.error("Error parsing date for grievance: " + dateStr, e);
throw new IllegalArgumentException("Invalid date format in request:"+ dateStr);
}
}


@Override
public String getGrievanceDetailsWithRemarks(String request) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

try {
// Parsing request to get the filter parameters (State, ComplaintResolution, StartDate, EndDate)
JSONObject requestObj = new JSONObject(request);
String complaintResolution = requestObj.optString("ComplaintResolution", null);
String state = requestObj.optString("State", null);
String fromDate = requestObj.optString("StartDate");
String toDate = requestObj.optString("EndDate");

if (fromDate == null || toDate == null) {
throw new IllegalArgumentException("fromDate and toDate are required");
}
// Convert StartDate and EndDate to Date objects
Date startDateStr = parseDate(fromDate);
Date endDateStr = parseDate(toDate);

Comment on lines +431 to +433
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle potential NullPointerException for date parsing.

The method doesn't check if fromDate or toDate is null before parsing, which could lead to NullPointerExceptions.

-Date startDateStr = parseDate(fromDate);
-Date endDateStr = parseDate(toDate);
+if (fromDate == null || toDate == null) {
+    throw new IllegalArgumentException("StartDate and EndDate are required");
+}
+Date startDateStr = parseDate(fromDate);
+Date endDateStr = parseDate(toDate);
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Date startDateStr = parseDate(fromDate);
Date endDateStr = parseDate(toDate);
if (fromDate == null || toDate == null) {
throw new IllegalArgumentException("StartDate and EndDate are required");
}
Date startDateStr = parseDate(fromDate);
Date endDateStr = parseDate(toDate);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srishtigrp78 Thank you for confirming! I can see that you've implemented the null check for the date parameters before attempting to parse them. This change will prevent potential NullPointerExceptions and improve the error handling in your code. Good work!


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

List<GrievanceDetails> grievanceDetailsList = grievanceDataRepo.fetchGrievanceDetailsBasedOnParams(state, complaintResolution, startDateStr, endDateStr); // Fetch grievance details based on the request

if (grievanceDetailsList == null || grievanceDetailsList.isEmpty()) {
return "No grievance details found for the provided request.";
}

List<GrievanceResponse> grievanceResponseList = new ArrayList<>();


// Determine if the complaintResolution is "resolved" or "unresolved"
for (GrievanceDetails grievance : grievanceDetailsList) {
GrievanceResponse grievanceResponse = new GrievanceResponse();

// Set basic grievance details
grievanceResponse.setGrievanceId(grievance.getGrievanceId());
grievanceResponse.setComplaintID(grievance.getComplaintID());
grievanceResponse.setPrimaryNumber(grievance.getPrimaryNumber());
grievanceResponse.setComplaintResolution(grievance.getComplaintResolution());
grievanceResponse.setCreatedDate(grievance.getCreatedDate());
grievanceResponse.setLastModDate(grievance.getLastModDate());

// Fetch and set remarks based on complaintResolution value
String remarks = "";
if ("unresolved".equalsIgnoreCase(complaintResolution)) {
// Fetch remarks from t_bencall by joining with t_grievanceworklist based on benRegId
remarks = fetchRemarksFromBenCallByComplaint(grievance.getComplaintID());
} else if ("resolved".equalsIgnoreCase(complaintResolution)) {
// Fetch remarks from t_grievanceworklist
remarks = fetchRemarksFromGrievanceWorklist(grievance.getComplaintID());
} else {
// Default: Fetch remarks based on the grievance's specific conditions (no specific resolution status)
String callRemarks = fetchRemarksFromBenCallByComplaint(grievance.getComplaintID());
if(remarks != null && !remarks.startsWith("No remarks found")) {
remarks = callRemarks;
}
else {
remarks = fetchRemarksFromGrievanceWorklist(grievance.getComplaintID());

}
}

grievanceResponse.setRemarks(remarks);

// Add to response list
grievanceResponseList.add(grievanceResponse);
}

// Convert the list of GrievanceResponse objects to JSON and return as a string
return objectMapper.writeValueAsString(grievanceResponseList);

} catch (Exception e) {
logger.error("Error while getting grievance details with remarks: " + e.getMessage(), e);
throw new Exception("Error processing grievance request");
}
}



private String fetchRemarksFromBenCallByComplaint(String complaintID) throws Exception {
// Query t_grievanceworklist to fetch the benRegId based on complaintID
List<GrievanceDetails> grievanceWorklist = grievanceDataRepo.fetchGrievanceWorklistByComplaintID(complaintID);

if (grievanceWorklist != null && !grievanceWorklist.isEmpty()) {
GrievanceDetails grievance = grievanceWorklist.get(0);
Long beneficiaryRegID = grievance.getBeneficiaryRegID(); // Fetch the beneficiaryRegID from the grievance

// Query t_bencall to fetch remarks based on benRegId
List<Object[]> benCallResults = beneficiaryCallRepo.fetchBenCallRemarks(beneficiaryRegID);

if (benCallResults != null && !benCallResults.isEmpty()) {
return (String) benCallResults.get(0)[0]; // Fetch the remarks
}
}

return "No remarks found in t_bencall";
}

private String fetchRemarksFromGrievanceWorklist(String complaintID) throws JSONException {
// Query t_grievanceworklist to fetch remarks based on complaintID
List<Object[]> grievanceWorklistResults = grievanceDataRepo.fetchGrievanceWorklistRemarks(complaintID);

if (grievanceWorklistResults != null && !grievanceWorklistResults.isEmpty()) {
// Assuming grievanceWorklistResults has a format like [remarks] for simplicity
return (String) grievanceWorklistResults.get(0)[0]; // Fetch the remarks
}
return "No remarks found in t_grievanceworklist";
}


}

Loading