Skip to content

Commit

Permalink
Merge pull request #47 from Bahmni/my-patient-count
Browse files Browse the repository at this point in the history
A-1206860834641287 | API Changes to show count of myPatients
  • Loading branch information
umair-fayaz authored Mar 26, 2024
2 parents 21930b1 + 29ae167 commit c37c286
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public interface WardDAO {

List<AdmittedPatient> getAdmittedPatients(Location location, Provider provider, Date currentDateTime, String sortBy);

WardPatientsSummary getWardPatientSummary(Location location);
WardPatientsSummary getWardPatientSummary(Location location, Provider provider, Date currentDateTime);

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public List<AdmittedPatient> getAdmittedPatients(Location location, Provider pro
"LEFT JOIN CareTeam careTeam on careTeam.visit = v " +
"JOIN org.openmrs.module.bedmanagement.entity.BedLocationMapping locmap on locmap.bed = assignment.bed " +
"JOIN org.openmrs.Location l on locmap.location = l " +
"LEFT JOIN careTeam.participants ctp " +
"LEFT JOIN careTeam.participants ctp ON ctp.voided = 0 " +
"LEFT JOIN org.openmrs.Order o on o.encounter = e " +
"LEFT JOIN Slot s on s.order = o " +
"where assignment.endDatetime is null and v.stopDatetime is null and l.parentLocation = :location ";
Expand Down Expand Up @@ -98,21 +98,45 @@ public List<AdmittedPatient> getAdmittedPatients(Location location, Provider pro
}

@Override
public WardPatientsSummary getWardPatientSummary(Location location) {
public WardPatientsSummary getWardPatientSummary(Location location, Provider provider, Date dateTime) {
Session session = this.sessionFactory.getCurrentSession();
try {
Query query = session.createQuery(
"select NEW org.openmrs.module.ipd.api.model.WardPatientsSummary(COUNT(assignment)) " +
"from org.openmrs.module.bedmanagement.entity.BedPatientAssignment assignment " +
"JOIN org.openmrs.module.bedmanagement.entity.BedLocationMapping locmap on locmap.bed = assignment.bed " +
"JOIN org.openmrs.Location l on locmap.location = l " +
"JOIN org.openmrs.Visit v on v.patient = assignment.patient " +
"where assignment.endDatetime is null and v.stopDatetime is null and l.parentLocation = :location");
query.setParameter("location", location);
return (WardPatientsSummary) query.getSingleResult();
Query totalPatientsQuery = session.createQuery(
"SELECT COUNT(assignment) " +
"FROM org.openmrs.module.bedmanagement.entity.BedPatientAssignment assignment " +
"JOIN org.openmrs.module.bedmanagement.entity.BedLocationMapping locmap ON locmap.bed = assignment.bed " +
"JOIN org.openmrs.Location l ON locmap.location = l " +
"JOIN org.openmrs.Visit v ON v.patient = assignment.patient " +
"WHERE assignment.endDatetime IS NULL AND v.stopDatetime IS NULL AND l.parentLocation = :location"
);

totalPatientsQuery.setParameter("location", location);

Long totalPatients = (Long) totalPatientsQuery.uniqueResult();

Query totalProviderPatientsQuery = session.createQuery(
"SELECT COUNT(DISTINCT CASE WHEN ctp.provider = :provider THEN assignment.patient ELSE null END) " +
"FROM org.openmrs.module.bedmanagement.entity.BedPatientAssignment assignment " +
"JOIN org.openmrs.module.bedmanagement.entity.BedLocationMapping locmap ON locmap.bed = assignment.bed " +
"JOIN org.openmrs.Location l ON locmap.location = l " +
"JOIN org.openmrs.Visit v ON v.patient = assignment.patient " +
"LEFT JOIN CareTeam careTeam ON careTeam.patient = v.patient " +
"LEFT JOIN careTeam.participants ctp ON ctp.voided = 0 " +
"WHERE assignment.endDatetime IS NULL AND v.stopDatetime IS NULL AND l.parentLocation = :location " +
"AND (ctp.provider = :provider AND :dateTime BETWEEN ctp.startTime AND ctp.endTime)"
);

totalProviderPatientsQuery.setParameter("location", location);
totalProviderPatientsQuery.setParameter("provider", provider);
totalProviderPatientsQuery.setParameter("dateTime", dateTime);

Long totalProviderPatients = (Long) totalProviderPatientsQuery.uniqueResult();

return new WardPatientsSummary(totalPatients, totalProviderPatients);
} catch (Exception e) {
log.error("Exception at WardDAO getAdmittedPatients ",e.getStackTrace());
log.error("Exception at WardDAO getAdmittedPatients ", e);
}

return new WardPatientsSummary();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
@Getter
public class WardPatientsSummary {
private Long totalPatients = 0L;
private Long totalProviderPatients = 0L;

// to be added in future
// private Integer myPatients;
// private Integer toBeDischargedPatients;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface WardService {

WardPatientsSummary getIPDWardPatientSummary(String wardUuid);
WardPatientsSummary getIPDWardPatientSummary(String wardUuid, String providerUuid);

List<AdmittedPatient> getWardPatientsByUuid(String wardUuid, String sortBy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ public WardServiceImpl(WardDAO wardDAO) {


@Override
public WardPatientsSummary getIPDWardPatientSummary(String wardUuid) {
public WardPatientsSummary getIPDWardPatientSummary(String wardUuid, String providerUuid) {
Location location= Context.getService(LocationService.class).getLocationByUuid(wardUuid);
return wardDAO.getWardPatientSummary(location);
Provider provider = Context.getProviderService().getProviderByUuid(providerUuid);
Date currentDateTime = new Date();
return wardDAO.getWardPatientSummary(location, provider, currentDateTime);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
public class IPDWardPatientSummaryResponse {

private Long totalPatients;
private Long totalProviderPatients;

public static IPDWardPatientSummaryResponse createFrom(WardPatientsSummary wardPatientsSummary){
return IPDWardPatientSummaryResponse.builder().
totalPatients(wardPatientsSummary.getTotalPatients()).
totalProviderPatients(wardPatientsSummary.getTotalProviderPatients()).
build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public IPDWardController(IPDWardService ipdWardService) {

@RequestMapping(value = "{wardUuid}/summary",method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Object> getIPDWardPatientStats (@PathVariable("wardUuid") String wardUuid) throws ParseException {
WardPatientsSummary wardPatientsSummary = ipdWardService.getIPDWardPatientSummary(wardUuid);
public ResponseEntity<Object> getIPDWardPatientStats (@PathVariable("wardUuid") String wardUuid,
@RequestParam(value = "providerUuid") String providerUuid) throws ParseException {
WardPatientsSummary wardPatientsSummary = ipdWardService.getIPDWardPatientSummary(wardUuid, providerUuid);
return new ResponseEntity<>(IPDWardPatientSummaryResponse.createFrom(wardPatientsSummary), OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface IPDWardService {

WardPatientsSummary getIPDWardPatientSummary(String wardUuid);
WardPatientsSummary getIPDWardPatientSummary(String wardUuid, String providerUuid);

IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, Integer limit, String sortBy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public IPDWardServiceImpl(WardService wardService) {


@Override
public WardPatientsSummary getIPDWardPatientSummary(String wardUuid) {
return wardService.getIPDWardPatientSummary(wardUuid);
public WardPatientsSummary getIPDWardPatientSummary(String wardUuid, String providerUuid) {
return wardService.getIPDWardPatientSummary(wardUuid, providerUuid);
}

@Override
Expand Down

0 comments on commit c37c286

Please sign in to comment.