Skip to content

Commit ae40837

Browse files
Merge pull request #89 from BahmniIndiaDistro/BAH-2711
BAH-2711 | To read location from cookie
2 parents fa6e4a9 + 6cbbb84 commit ae40837

4 files changed

Lines changed: 36 additions & 16 deletions

File tree

omod/src/main/java/org/bahmni/module/hip/web/controller/PatientController.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import org.bahmni.module.hip.web.client.model.ErrorCode;
66
import org.bahmni.module.hip.web.client.model.ErrorRepresentation;
77
import org.bahmni.module.hip.web.model.ExistingPatient;
8+
import org.bahmni.module.hip.web.model.Location;
89
import org.bahmni.module.hip.web.service.ExistingPatientService;
910
import org.bahmni.module.hip.web.service.ValidationService;
11+
import org.codehaus.jackson.map.ObjectMapper;
1012
import org.openmrs.Patient;
1113
import org.openmrs.module.webservices.rest.web.RestConstants;
1214
import org.springframework.beans.factory.annotation.Autowired;
@@ -19,6 +21,9 @@
1921
import org.springframework.web.bind.annotation.RestController;
2022
import org.springframework.web.bind.annotation.RequestParam;
2123
import org.springframework.web.bind.annotation.PathVariable;
24+
import org.springframework.web.bind.annotation.CookieValue;
25+
26+
import java.io.IOException;
2227
import java.util.List;
2328
import java.util.Set;
2429

@@ -39,8 +44,10 @@ public PatientController(ExistingPatientService existingPatientService, Validati
3944
ResponseEntity<?> getExistingPatients(@RequestParam(required = false) String patientName,
4045
@RequestParam String patientYearOfBirth,
4146
@RequestParam String patientGender,
42-
@RequestParam String phoneNumber) {
43-
Set<Patient> matchingPatients = existingPatientService.getMatchingPatients(phoneNumber,patientName,
47+
@RequestParam String phoneNumber,
48+
@CookieValue(name = "bahmni.user.location") String location) throws IOException {
49+
String locationUuid = new ObjectMapper().readValue(location,Location.class).getUuid();
50+
Set<Patient> matchingPatients = existingPatientService.getMatchingPatients(locationUuid,phoneNumber,patientName,
4451
Integer.parseInt(patientYearOfBirth), patientGender);
4552
if (matchingPatients.size() == 0) {
4653
return ResponseEntity.ok().body(new ErrorRepresentation(new Error(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.bahmni.module.hip.web.model;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class Location {
9+
String name;
10+
String uuid;
11+
}

omod/src/main/java/org/bahmni/module/hip/web/service/ExistingPatientService.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public ExistingPatientService(PatientDao patientDao, PatientService patientServi
4444
this.locationService = locationService;
4545
}
4646

47-
public Set<Patient> getMatchingPatients(String phoneNumber, String patientName, int patientYearOfBirth, String patientGender) {
47+
public Set<Patient> getMatchingPatients(String locationUuid,String phoneNumber, String patientName, int patientYearOfBirth, String patientGender) {
4848
Set<Patient> matchingPatients = new HashSet<>();
4949
matchingPatients.addAll(getMatchingPatients(phoneNumber));
50-
matchingPatients.addAll(getMatchingPatients(patientName, patientYearOfBirth, patientGender));
50+
matchingPatients.addAll(getMatchingPatients(locationUuid,patientName, patientYearOfBirth, patientGender));
5151
matchingPatients.removeIf(patient -> !getHealthId(patient).equals(""));
5252
return matchingPatients;
5353
}
@@ -115,17 +115,17 @@ public List<Patient> getMatchingPatients(String phoneNumber) {
115115
return new ArrayList<>();
116116
}
117117

118-
public List<Patient> getMatchingPatients(String patientName, int patientYearOfBirth, String patientGender) {
119-
List<PatientResponse> patients = getPatients(patientName, patientYearOfBirth, patientGender);
118+
public List<Patient> getMatchingPatients(String locationUuid, String patientName, int patientYearOfBirth, String patientGender) {
119+
List<PatientResponse> patients = getPatients(locationUuid,patientName, patientYearOfBirth, patientGender);
120120
List<Patient> existingPatients = new ArrayList<>();
121121
for (PatientResponse patient : patients) {
122122
existingPatients.add(patientService.getPatientByUuid(patient.getUuid()));
123123
}
124124
return existingPatients;
125125
}
126126

127-
private List<PatientResponse> getPatients(String patientName, int patientYearOfBirth, String patientGender) {
128-
List<PatientResponse> patientsMatchedWithName = filterPatientsByName(patientName);
127+
private List<PatientResponse> getPatients(String locationUuid, String patientName, int patientYearOfBirth, String patientGender) {
128+
List<PatientResponse> patientsMatchedWithName = filterPatientsByName(locationUuid,patientName);
129129
if (patientsMatchedWithName.size() != 1) {
130130
List<PatientResponse> patientsMatchedWithNameAndAge = filterPatientsByAge(patientYearOfBirth, patientsMatchedWithName);
131131
if (patientsMatchedWithNameAndAge.size() != 1)
@@ -135,9 +135,9 @@ private List<PatientResponse> getPatients(String patientName, int patientYearOfB
135135
return patientsMatchedWithName;
136136
}
137137

138-
private List<PatientResponse> filterPatientsByName(String patientName) {
139-
PatientSearchParameters searchParameters = getPatientSearchParameters(patientName);
140-
Supplier<Location> visitLocation = () -> getVisitLocation(searchParameters.getLoginLocationUuid());
138+
private List<PatientResponse> filterPatientsByName(String locationUuid,String patientName) {
139+
PatientSearchParameters searchParameters = getPatientSearchParameters(locationUuid,patientName);
140+
Supplier<Location> visitLocation = () -> getVisitLocation(locationUuid);
141141
Supplier<List<String>> configuredAddressFields = () -> patientDao.getConfiguredPatientAddressFields();
142142

143143
return patientDao.getPatients(searchParameters, visitLocation, configuredAddressFields);
@@ -228,7 +228,7 @@ public boolean isHealthIdVoided(String uuid){
228228
return false;
229229
}
230230

231-
private PatientSearchParameters getPatientSearchParameters(String patientName) {
231+
private PatientSearchParameters getPatientSearchParameters(String locationUuid,String patientName) {
232232
PatientSearchParameters searchParameters = new PatientSearchParameters();
233233
searchParameters.setIdentifier("");
234234
searchParameters.setName(patientName);
@@ -245,7 +245,7 @@ private PatientSearchParameters getPatientSearchParameters(String patientName) {
245245
searchParameters.setAddressSearchResultFields(null);
246246
searchParameters.setPatientSearchResultFields(null);
247247

248-
searchParameters.setLoginLocationUuid(locationService.getLocation(Config.LOCATION.getValue()).getUuid());
248+
searchParameters.setLoginLocationUuid(locationUuid);
249249
searchParameters.setFilterPatientsByLocation(false);
250250
searchParameters.setFilterOnAllIdentifiers(false);
251251
return searchParameters;

omod/src/test/java/org/bahmni/module/hip/web/controller/PatientControllerTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.openmrs.module.webservices.rest.web.RestConstants;
1818
import org.springframework.beans.factory.annotation.Autowired;
1919
import org.springframework.http.MediaType;
20-
import org.springframework.http.ResponseEntity;
2120
import org.springframework.test.context.ContextConfiguration;
2221
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2322
import org.springframework.test.context.web.WebAppConfiguration;
@@ -27,6 +26,7 @@
2726
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2827
import org.springframework.web.context.WebApplicationContext;
2928

29+
import javax.servlet.http.Cookie;
3030
import java.util.ArrayList;
3131
import java.util.Collections;
3232
import java.util.HashSet;
@@ -68,7 +68,7 @@ public void shouldReturn200OKWhenMatchingPatientFound() throws Exception {
6868
ExistingPatient existingPatient = new ExistingPatient("sam tom", "35", "null, null", "M", "3f81c3b4-04fc-4311-9b50-b863fbe023dc", "9123456780");
6969
when(existingPatientService.getMatchingPatients("+91-9876543210"))
7070
.thenReturn(new ArrayList<>());
71-
when(existingPatientService.getMatchingPatients(anyString(), anyInt(), anyString()))
71+
when(existingPatientService.getMatchingPatients(anyString(),anyString(), anyInt(), anyString()))
7272
.thenReturn(patients);
7373
when(existingPatientService.getMatchingPatientDetails(new HashSet<>(patients)))
7474
.thenReturn(Collections.singletonList(existingPatient));
@@ -78,6 +78,7 @@ public void shouldReturn200OKWhenMatchingPatientFound() throws Exception {
7878
.param("patientYearOfBirth", "1985")
7979
.param("patientGender", "M")
8080
.param("phoneNumber", "+91-9876543210")
81+
.cookie(new Cookie("bahmni.user.location", "{\"name\":\"General Ward\",\"uuid\":\"baf7bd38-d225-11e4-9c67-080027b662ec\"}"))
8182
.accept(MediaType.APPLICATION_JSON))
8283
.andExpect(status().isOk());
8384
}
@@ -87,14 +88,15 @@ public void shouldReturnNoRecordsWhenNoMatchingPatientFound() throws Exception {
8788
List<Patient> patients = new ArrayList<>();
8889
when(existingPatientService.getMatchingPatients("+91-9876543210"))
8990
.thenReturn(new ArrayList<>());
90-
when(existingPatientService.getMatchingPatients(anyString(), anyInt(), anyString()))
91+
when(existingPatientService.getMatchingPatients(anyString(),anyString(), anyInt(), anyString()))
9192
.thenReturn(patients);
9293

9394
MvcResult mvcResult = mockMvc.perform(get(String.format("/rest/%s/hip/existingPatients", RestConstants.VERSION_1))
9495
.param("patientName", "sam tom")
9596
.param("patientYearOfBirth", "1985")
9697
.param("patientGender", "M")
9798
.param("phoneNumber", "+91-9876543210")
99+
.cookie(new Cookie("bahmni.user.location", "{\"name\":\"General Ward\",\"uuid\":\"baf7bd38-d225-11e4-9c67-080027b662ec\"}"))
98100
.accept(MediaType.APPLICATION_JSON))
99101
.andReturn();
100102

0 commit comments

Comments
 (0)