Skip to content
8 changes: 4 additions & 4 deletions src/main/java/com/iemr/flw/controller/StopTBController.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getAllGeneralExaminations(@RequestBody Map<String, Object> body) {
if (raw == null) throw new Exception("providerServiceMapID is required");
Integer villageID = body.get("villageID") != null ? Integer.parseInt(body.get("villageID").toString()) : null;
Map<String, Object> data = stopTBService.getAllGeneralExaminations(Integer.parseInt(raw.toString()), villageID);
response.setResponse(gson.toJson(data));
response.setResponse(gson.toJsonTree(data));
} catch (Exception e) {
logger.error("Error in getAllGeneralExaminations: " + e);
response.setError(5000, "Error fetching general examinations: " + e.getMessage());
Expand Down Expand Up @@ -85,7 +85,7 @@ public String getAllNurseTBScreenings(@RequestBody Map<String, Object> body) {
if (raw == null) throw new Exception("providerServiceMapID is required");
Integer villageID = body.get("villageID") != null ? Integer.parseInt(body.get("villageID").toString()) : null;
Map<String, Object> data = stopTBService.getAllNurseTBScreenings(Integer.parseInt(raw.toString()), villageID);
response.setResponse(gson.toJson(data));
response.setResponse(gson.toJsonTree(data));
} catch (Exception e) {
logger.error("Error in getAllNurseTBScreenings: " + e);
response.setError(5000, "Error fetching TB screenings: " + e.getMessage());
Expand Down Expand Up @@ -122,7 +122,7 @@ public String getAllGeneralOpd(@RequestBody Map<String, Object> body) {
if (raw == null) throw new Exception("providerServiceMapID is required");
Integer villageID = body.get("villageID") != null ? Integer.parseInt(body.get("villageID").toString()) : null;
Map<String, Object> data = stopTBService.getAllGeneralOpd(Integer.parseInt(raw.toString()), villageID);
response.setResponse(gson.toJson(data));
response.setResponse(gson.toJsonTree(data));
} catch (Exception e) {
logger.error("Error in getAllGeneralOpd: " + e);
response.setError(5000, "Error fetching general OPD records: " + e.getMessage());
Expand Down Expand Up @@ -155,7 +155,7 @@ public String getAllDiagnostics(@RequestBody Map<String, Object> body) {
if (raw == null) throw new Exception("providerServiceMapID is required");
Integer villageID = body.get("villageID") != null ? Integer.parseInt(body.get("villageID").toString()) : null;
Map<String, Object> data = stopTBService.getAllDiagnostics(Integer.parseInt(raw.toString()), villageID);
response.setResponse(gson.toJson(data));
response.setResponse(gson.toJsonTree(data));
} catch (Exception e) {
logger.error("Error in getAllDiagnostics: " + e);
response.setError(5000, "Error fetching diagnostics records: " + e.getMessage());
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/iemr/flw/repo/identity/BeneficiaryRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ Page<RMNCHMBeneficiaryaddress> getBenDataWithinDates(
@Query(nativeQuery = true, value = " SELECT HealthID,HealthIdNumber,isNewAbha FROM db_iemr.t_healthid WHERE HealthIdNumber = :healthIdNumber ")
ArrayList<Object[]> getBenHealthDetails(@Param("healthIdNumber") String healthIdNumber);

// Same rows FHIR-API getBenhealthID reads, so the worklist can fill healthId/healthIdNumber without an HTTP call
@Query(nativeQuery = true, value = " SELECT HealthID, HealthIdNumber FROM db_iemr.m_benhealthidmapping WHERE BeneficiaryRegID = :benRegId ")
List<Object[]> getHealthIdMappingRows(@Param("benRegId") Long benRegId);

@Query(nativeQuery = true, value = " SELECT HealthId FROM db_iemr.t_healthid WHERE HealthIdNumber = :healthIdNumber ")
List<String> getHealthIdByNumber(@Param("healthIdNumber") String healthIdNumber);

// benRegId has no unique constraint on i_beneficiarymapping β€” a beneficiary can have
// more than one mapping row, so this must return a list, not assume a single result.
@Query("SELECT b FROM RMNCHMBeneficiarymapping b WHERE b.benRegId = :benRegId ORDER BY b.benMapId DESC")
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/iemr/flw/repo/iemr/DiagnosticOrderRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,16 @@ List<Long> findBeneficiaryIdsClosed(@Param("orderType") String orderType,
"AND (:providerServiceMapId IS NULL OR b.providerServiceMapId = :providerServiceMapId))")
List<Long> findBeneficiaryIdsAwaitingManualEntry(@Param("orderType") String orderType,
@Param("villageId") Integer villageId, @Param("providerServiceMapId") Integer providerServiceMapId);

// The five queries above in one pass: beneficiaryId + status of each beneficiary's latest order,
// same filters, for the caller to bucket by status.
@Query("SELECT o.beneficiaryId, o.status FROM DiagnosticOrder o WHERE o.orderType = :orderType AND o.deleted = false " +
"AND o.status IN ('PENDING', 'IN_PROGRESS', 'COMPLETED', 'FAILED', 'CLOSED', 'CANCELLED', 'EXPIRED', 'REFUSED', 'MANUAL_ENTRY') " +
"AND o.id = (SELECT MAX(o2.id) FROM DiagnosticOrder o2 " +
"WHERE o2.beneficiaryId = o.beneficiaryId AND o2.orderType = :orderType AND o2.deleted = false) " +
"AND o.beneficiaryId IN (SELECT b.beneficiaryID FROM BenFlowStatus b WHERE b.deleted = false " +
"AND (:villageId IS NULL OR b.villageID = :villageId) " +
"AND (:providerServiceMapId IS NULL OR b.providerServiceMapId = :providerServiceMapId))")
List<Object[]> findLatestOrderStatusByBeneficiary(@Param("orderType") String orderType,
@Param("villageId") Integer villageId, @Param("providerServiceMapId") Integer providerServiceMapId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import jakarta.persistence.QueryHint;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -19,6 +21,8 @@ public interface StopTBDiagnosticsRepo extends JpaRepository<StopTBDiagnostics,
@Query("SELECT d FROM StopTBDiagnostics d WHERE d.providerServiceMapID = :psmId AND d.deleted = false ORDER BY d.visitDate DESC")
List<StopTBDiagnostics> findAllByProviderServiceMapID(@Param("psmId") Integer psmId);

// Read-only: getAll only reads these rows, so Hibernate keeps no change-tracking snapshot per row
@QueryHints(@QueryHint(name = "org.hibernate.readOnly", value = "true"))
@Query("SELECT d FROM StopTBDiagnostics d WHERE d.benRegID IN " +
"(SELECT b.beneficiaryRegID FROM BenFlowStatus b WHERE b.providerServiceMapId = :psmId AND b.villageID = :villageId) " +
"AND d.deleted = false ORDER BY d.visitDate DESC")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import jakarta.persistence.QueryHint;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -32,6 +34,8 @@ StopTBGeneralExamination findByBeneficiaryRegIDAndVisitCode(@Param("beneficiaryR
@Query("SELECT e FROM StopTBGeneralExamination e WHERE e.providerServiceMapID = :psmId AND e.deleted = false ORDER BY e.createdDate DESC")
List<StopTBGeneralExamination> findAllByProviderServiceMapID(@Param("psmId") Integer psmId);

// Read-only: getAll only reads these rows, so Hibernate keeps no change-tracking snapshot per row
@QueryHints(@QueryHint(name = "org.hibernate.readOnly", value = "true"))
@Query("SELECT e FROM StopTBGeneralExamination e WHERE e.beneficiaryRegID IN " +
"(SELECT b.beneficiaryRegID FROM BenFlowStatus b WHERE b.providerServiceMapId = :psmId AND b.villageID = :villageId) " +
"AND e.deleted = false ORDER BY e.createdDate DESC")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import jakarta.persistence.QueryHint;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -30,6 +32,8 @@ public interface StopTBGeneralOpdRepo extends JpaRepository<StopTBGeneralOpd, Lo
@Query("SELECT o FROM StopTBGeneralOpd o WHERE o.providerServiceMapID = :psmId AND o.deleted = false ORDER BY o.createdDate DESC")
List<StopTBGeneralOpd> findAllByProviderServiceMapID(@Param("psmId") Integer psmId);

// Read-only: getAll only reads these rows, so Hibernate keeps no change-tracking snapshot per row
@QueryHints(@QueryHint(name = "org.hibernate.readOnly", value = "true"))
@Query("SELECT o FROM StopTBGeneralOpd o WHERE o.benRegID IN " +
"(SELECT b.beneficiaryRegID FROM BenFlowStatus b WHERE b.providerServiceMapId = :psmId AND b.villageID = :villageId) " +
"AND o.deleted = false ORDER BY o.createdDate DESC")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/iemr/flw/repo/iemr/TBScreeningRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import jakarta.persistence.QueryHint;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -37,6 +39,8 @@ public interface TBScreeningRepo extends JpaRepository<TBScreening, Long> {
@Query("SELECT tbs FROM TBScreening tbs WHERE tbs.providerServiceMapID = :psmId AND tbs.deleted = false ORDER BY tbs.visitDate DESC")
List<TBScreening> findAllByProviderServiceMapID(@Param("psmId") Integer psmId);

// Read-only: getAll only reads these rows, so Hibernate keeps no change-tracking snapshot per row
@QueryHints(@QueryHint(name = "org.hibernate.readOnly", value = "true"))
@Query("SELECT tbs FROM TBScreening tbs WHERE tbs.benRegID IN " +
"(SELECT b.beneficiaryRegID FROM BenFlowStatus b WHERE b.providerServiceMapId = :psmId AND b.villageID = :villageId) " +
"AND tbs.deleted = false ORDER BY tbs.visitDate DESC")
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/iemr/flw/repo/iemr/TBSuspectedRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ TBSuspected getByUserIdAndBenIdAndVisitCode(@Param("benId") Long benId, @Param("
@Query("SELECT tbs FROM TBSuspected tbs WHERE tbs.userId = :userId and tbs.visitDate >= :fromDate and tbs.visitDate <= :toDate")
List<TBSuspected> getByUserId(@Param("userId") Integer userId, @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate);

@Query("SELECT tbs FROM TBSuspected tbs WHERE tbs.benId IN " +
"(SELECT b.beneficiaryID FROM BenFlowStatus b WHERE b.providerServiceMapId = :psmId AND b.villageID = :villageId)")
// Same query as before in SQL, with an optimizer hint so the village subquery reads
// idx_flow_psm_village_reg instead of the PSM-only index (ignored if that index is missing).
@Query(nativeQuery = true, value = "SELECT t.* FROM db_iemr.tb_suspected t WHERE t.ben_id IN " +
"(SELECT /*+ INDEX(b idx_flow_psm_village_reg) */ b.beneficiary_id FROM db_iemr.i_ben_flow_outreach b " +
"WHERE b.providerServiceMapID = :psmId AND b.villageID = :villageId)")
List<TBSuspected> getByProviderServiceMapIdAndVillageId(@Param("psmId") Integer psmId, @Param("villageId") Integer villageId);

@Transactional
Expand Down
Loading
Loading