Skip to content

Commit

Permalink
Merge pull request #43 from Bahmni/ct-participant-patients
Browse files Browse the repository at this point in the history
Add new endpoint for Cure Team Participant patients
  • Loading branch information
umair-fayaz authored Mar 18, 2024
2 parents 5809e89 + 3fcf066 commit 3f77311
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.openmrs.module.ipd.api.dao;

import org.openmrs.Location;
import org.openmrs.Provider;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;

import java.util.Date;
import java.util.List;

public interface WardDAO {

List<AdmittedPatient> searchAdmittedPatients(Location location,List<String> searchKeys,String searchValue);

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

WardPatientsSummary getWardPatientSummary(Location location);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.openmrs.Location;
import org.openmrs.Provider;
import org.openmrs.module.ipd.api.dao.WardDAO;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;
Expand All @@ -14,6 +15,7 @@
import org.hibernate.query.Query;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
Expand All @@ -29,25 +31,46 @@ public HibernateWardDAO(SessionFactory sessionFactory) {
}

@Override
public List<AdmittedPatient> getAdmittedPatientsByLocation(Location location) {
public List<AdmittedPatient> getAdmittedPatients(Location location, Provider provider, Date dateTime) {
Session session = this.sessionFactory.getCurrentSession();
try {
Query query = session.createQuery("select NEW org.openmrs.module.ipd.api.model.AdmittedPatient(assignment," +
String queryString = "select NEW org.openmrs.module.ipd.api.model.AdmittedPatient(assignment," +
"(COUNT(DISTINCT o.orderId) - COUNT (DISTINCT s.order.orderId)), careTeam)" +
"from org.openmrs.module.bedmanagement.entity.BedPatientAssignment assignment " +
"JOIN org.openmrs.Visit v on v.patient = assignment.patient " +
"JOIN org.openmrs.Encounter e on e.visit = v " +
"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 " +
"JOIN careTeam.participants ctp " +
"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 " +
"GROUP BY assignment.patient, v " +
"ORDER BY assignment.startDatetime desc");
"where assignment.endDatetime is null and v.stopDatetime is null and l.parentLocation = :location ";

if (provider != null) {
queryString += "and ctp.provider = :provider ";
}

if (dateTime != null) {
queryString += "and :dateTime between ctp.startTime and ctp.endTime ";
}


queryString += "GROUP BY assignment.patient, v " +
"ORDER BY assignment.startDatetime desc";

Query query = session.createQuery(queryString);

query.setParameter("location", location);

if (provider != null) {
query.setParameter("provider", provider);
}

if (dateTime != null) {
query.setParameter("dateTime", dateTime);
}

return query.getResultList();
}
catch (Exception ex){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface WardService {

List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> searchKeys, String searchValue);

List<AdmittedPatient> getPatientsByWardAndProvider(String wardUuid, String providerUuid);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openmrs.module.ipd.api.service.impl;

import org.openmrs.Location;
import org.openmrs.Provider;
import org.openmrs.api.LocationService;
import org.openmrs.api.context.Context;
import org.openmrs.module.ipd.api.dao.WardDAO;
Expand All @@ -11,7 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
Expand All @@ -35,7 +36,15 @@ public WardPatientsSummary getIPDWardPatientSummary(String wardUuid) {
@Override
public List<AdmittedPatient> getWardPatientsByUuid(String wardUuid) {
Location location= Context.getService(LocationService.class).getLocationByUuid(wardUuid);
return wardDAO.getAdmittedPatientsByLocation(location);
return wardDAO.getAdmittedPatients(location,null, null);
}

@Override
public List<AdmittedPatient> getPatientsByWardAndProvider(String wardUuid, String providerUuid) {
Location location = Context.getService(LocationService.class).getLocationByUuid(wardUuid);
Provider provider = Context.getProviderService().getProviderByUuid(providerUuid);
Date currentDateTime = new Date();
return wardDAO.getAdmittedPatients(location, provider, currentDateTime);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class HibernateWardDAOIntegrationTest extends BaseIntegrationTest {
public void shouldGetAdmittedPatientsByLocation() {

Location location= Context.getService(LocationService.class).getLocationByUuid("7779d653-393b-4118-9c83-a3715b82d4ac");
List<AdmittedPatient> admittedPatients= wardDAO.getAdmittedPatientsByLocation(location);
List<AdmittedPatient> admittedPatients= wardDAO.getAdmittedPatients(location, null, null);

Assertions.assertEquals(0, admittedPatients.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ public ResponseEntity<Object> getIPDWardPatient(@PathVariable("wardUuid") String
}
}

@RequestMapping(value = "{wardUuid}/myPatients", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Object> getIPDWardPatientsForProvider(@PathVariable("wardUuid") String wardUuid,
@RequestParam(value = "providerUuid") String providerUuid,
@RequestParam(value = "offset") Integer offset,
@RequestParam (value = "limit") Integer limit) throws ParseException {
try {
IPDPatientDetails ipdPatientDetails = ipdWardService.getIPDPatientsByWardAndProvider(wardUuid, providerUuid, offset, limit);
return new ResponseEntity<>(IPDPatientDetailsResponse.createFrom(ipdPatientDetails), OK);
} catch (Exception e) {
log.error("Runtime error while trying to create new schedule", e);
return new ResponseEntity<>(RestUtil.wrapErrorResponse(e, e.getMessage()), BAD_REQUEST);
}
}

@RequestMapping(value = "{wardUuid}/patients/search", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Object> searchIPDWardPatient(@PathVariable("wardUuid") String wardUuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ public interface IPDWardService {

IPDPatientDetails searchIPDPatientsInWard(String wardUuid, List<String> searchKeys, String searchValue, Integer offset, Integer limit);



IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String providerUuid, Integer offset, Integer limit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ public IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, In
return new IPDPatientDetails(admittedPatients.subList(offset, offset + limit), admittedPatients.size());
}

@Override
public IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String providerUuid, Integer offset, Integer limit) {

List<AdmittedPatient> admittedPatients = wardService.getPatientsByWardAndProvider(wardUuid, providerUuid);

if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
}

offset = Math.min(offset, admittedPatients.size());
limit = Math.min(limit, admittedPatients.size() - offset);

return new IPDPatientDetails(admittedPatients.subList(offset, offset + limit), admittedPatients.size());
}

@Override
public IPDPatientDetails searchIPDPatientsInWard(String wardUuid, List<String> searchKeys, String searchValue,
Integer offset, Integer limit) {
Expand Down

0 comments on commit 3f77311

Please sign in to comment.