Skip to content

Commit e1cad2b

Browse files
Amrita PAmrita P
authored andcommitted
O3-5428: Validate program enrollment dates against patient DOB
1 parent b465737 commit e1cad2b

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

omod/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ProgramEnrollmentResource1_8.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.openmrs.PatientState;
2121
import org.openmrs.api.PatientService;
2222
import org.openmrs.api.context.Context;
23+
import org.openmrs.module.webservices.rest.SimpleObject;
24+
import org.openmrs.module.webservices.rest.web.ConversionUtil;
2325
import org.openmrs.module.webservices.rest.web.RequestContext;
2426
import org.openmrs.module.webservices.rest.web.RestConstants;
2527
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
@@ -32,8 +34,10 @@
3234
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
3335
import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult;
3436
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
37+
import org.openmrs.module.webservices.rest.web.response.IllegalRequestException;
3538
import org.openmrs.module.webservices.rest.web.response.ResponseException;
3639

40+
import java.util.Date;
3741
import java.util.List;
3842
import java.util.stream.Collectors;
3943

@@ -86,7 +90,64 @@ public PatientProgram newDelegate() {
8690
public PatientProgram save(PatientProgram delegate) {
8791
return Context.getProgramWorkflowService().savePatientProgram(delegate);
8892
}
89-
93+
94+
@Override
95+
public Object create(SimpleObject propertiesToCreate, RequestContext context) throws ResponseException {
96+
Object patientRef = propertiesToCreate.get("patient");
97+
if (patientRef != null) {
98+
Patient patient = Context.getPatientService().getPatientByUuid(patientRef.toString());
99+
if (patient != null) {
100+
Date birthdate = patient.getBirthdate();
101+
if (birthdate != null) {
102+
Object dateEnrolledRaw = propertiesToCreate.get("dateEnrolled");
103+
if (dateEnrolledRaw != null) {
104+
Date dateEnrolled = (Date) ConversionUtil.convert(dateEnrolledRaw, Date.class);
105+
if (dateEnrolled != null && dateEnrolled.before(birthdate)) {
106+
throw new IllegalRequestException(
107+
"Enrollment date cannot be before the patient's date of birth");
108+
}
109+
}
110+
Object dateCompletedRaw = propertiesToCreate.get("dateCompleted");
111+
if (dateCompletedRaw != null) {
112+
Date dateCompleted = (Date) ConversionUtil.convert(dateCompletedRaw, Date.class);
113+
if (dateCompleted != null && dateCompleted.before(birthdate)) {
114+
throw new IllegalRequestException(
115+
"Completion date cannot be before the patient's date of birth");
116+
}
117+
}
118+
}
119+
}
120+
}
121+
return super.create(propertiesToCreate, context);
122+
}
123+
124+
@Override
125+
public Object update(String uuid, SimpleObject propertiesToUpdate, RequestContext context) throws ResponseException {
126+
PatientProgram patientProgram = getByUniqueId(uuid);
127+
if (patientProgram != null) {
128+
Date birthdate = patientProgram.getPatient().getBirthdate();
129+
if (birthdate != null) {
130+
Object dateEnrolledRaw = propertiesToUpdate.get("dateEnrolled");
131+
if (dateEnrolledRaw != null) {
132+
Date dateEnrolled = (Date) ConversionUtil.convert(dateEnrolledRaw, Date.class);
133+
if (dateEnrolled != null && dateEnrolled.before(birthdate)) {
134+
throw new IllegalRequestException(
135+
"Enrollment date cannot be before the patient's date of birth");
136+
}
137+
}
138+
Object dateCompletedRaw = propertiesToUpdate.get("dateCompleted");
139+
if (dateCompletedRaw != null) {
140+
Date dateCompleted = (Date) ConversionUtil.convert(dateCompletedRaw, Date.class);
141+
if (dateCompleted != null && dateCompleted.before(birthdate)) {
142+
throw new IllegalRequestException(
143+
"Completion date cannot be before the patient's date of birth");
144+
}
145+
}
146+
}
147+
}
148+
return super.update(uuid, propertiesToUpdate, context);
149+
}
150+
90151
@Override
91152
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
92153
if (rep instanceof DefaultRepresentation) {

omod/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_10/ProgramEnrollmentController1_10Test.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openmrs.module.webservices.rest.SimpleObject;
2222
import org.openmrs.module.webservices.rest.test.Util;
2323
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
24+
import org.openmrs.module.webservices.rest.web.response.IllegalRequestException;
2425
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
2526
import org.openmrs.util.OpenmrsUtil;
2627
import org.springframework.mock.web.MockHttpServletRequest;
@@ -191,6 +192,47 @@ public void shouldVoidPatientState() throws Exception {
191192
Assert.assertEquals(existingPatientState.getState().getUuid(), Util.getByPath(result,"states[0]/state/uuid") );
192193
}
193194

195+
// patient da7f524f (PATIENT_IN_A_PROGRAM_UUID) has birthdate 1975-04-08
196+
@Test(expected = IllegalRequestException.class)
197+
public void shouldRejectCreateWhenDateEnrolledIsBeforeBirthdate() throws Exception {
198+
String json = "{ \"patient\":\"" + RestTestConstants1_8.PATIENT_IN_A_PROGRAM_UUID
199+
+ "\", \"program\":\"" + RestTestConstants1_8.PROGRAM_UUID
200+
+ "\", \"dateEnrolled\":\"1970-01-01\" }";
201+
handle(newPostRequest(getURI(), json));
202+
}
203+
204+
@Test(expected = IllegalRequestException.class)
205+
public void shouldRejectCreateWhenDateCompletedIsBeforeBirthdate() throws Exception {
206+
String json = "{ \"patient\":\"" + RestTestConstants1_8.PATIENT_IN_A_PROGRAM_UUID
207+
+ "\", \"program\":\"" + RestTestConstants1_8.PROGRAM_UUID
208+
+ "\", \"dateEnrolled\":\"2000-01-01\", \"dateCompleted\":\"1970-01-01\" }";
209+
handle(newPostRequest(getURI(), json));
210+
}
211+
212+
@Test(expected = IllegalRequestException.class)
213+
public void shouldRejectUpdateWhenDateEnrolledIsBeforeBirthdate() throws Exception {
214+
// PATIENT_PROGRAM_UUID belongs to patient da7f524f (birthdate 1975-04-08)
215+
String json = "{ \"dateEnrolled\":\"1970-01-01\" }";
216+
handle(newPostRequest(getURI() + "/" + getUuid(), json));
217+
}
218+
219+
@Test(expected = IllegalRequestException.class)
220+
public void shouldRejectUpdateWhenDateCompletedIsBeforeBirthdate() throws Exception {
221+
// PATIENT_PROGRAM_UUID belongs to patient da7f524f (birthdate 1975-04-08)
222+
String json = "{ \"dateCompleted\":\"1970-01-01\" }";
223+
handle(newPostRequest(getURI() + "/" + getUuid(), json));
224+
}
225+
226+
@Test
227+
public void shouldCreateWhenDateEnrolledIsAfterBirthdate() throws Exception {
228+
// patient da7f524f (PATIENT_IN_A_PROGRAM_UUID) has birthdate 1975-04-08
229+
String json = "{ \"patient\":\"" + RestTestConstants1_8.PATIENT_IN_A_PROGRAM_UUID
230+
+ "\", \"program\":\"" + RestTestConstants1_8.PROGRAM_UUID
231+
+ "\", \"dateEnrolled\":\"2000-01-01\" }";
232+
Object result = deserialize(handle(newPostRequest(getURI(), json)));
233+
Assert.assertNotNull(result);
234+
}
235+
194236
private static void sortPatientStatesBasedOnStartDate(List<PatientState> patientStates) {
195237
Collections.sort(patientStates, new Comparator<PatientState>() {
196238

0 commit comments

Comments
 (0)