Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.openmrs.PatientState;
import org.openmrs.api.PatientService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
Expand All @@ -32,8 +34,10 @@
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.IllegalRequestException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -86,7 +90,64 @@ public PatientProgram newDelegate() {
public PatientProgram save(PatientProgram delegate) {
return Context.getProgramWorkflowService().savePatientProgram(delegate);
}


@Override
public Object create(SimpleObject propertiesToCreate, RequestContext context) throws ResponseException {
Object patientRef = propertiesToCreate.get("patient");
if (patientRef != null) {
Patient patient = Context.getPatientService().getPatientByUuid(patientRef.toString());
if (patient != null) {
Date birthdate = patient.getBirthdate();
if (birthdate != null) {
Object dateEnrolledRaw = propertiesToCreate.get("dateEnrolled");
if (dateEnrolledRaw != null) {
Date dateEnrolled = (Date) ConversionUtil.convert(dateEnrolledRaw, Date.class);
if (dateEnrolled != null && dateEnrolled.before(birthdate)) {
throw new IllegalRequestException(
"Enrollment date cannot be before the patient's date of birth");
}
}
Object dateCompletedRaw = propertiesToCreate.get("dateCompleted");
if (dateCompletedRaw != null) {
Date dateCompleted = (Date) ConversionUtil.convert(dateCompletedRaw, Date.class);
if (dateCompleted != null && dateCompleted.before(birthdate)) {
throw new IllegalRequestException(
"Completion date cannot be before the patient's date of birth");
}
}
}
}
}
return super.create(propertiesToCreate, context);
}

@Override
public Object update(String uuid, SimpleObject propertiesToUpdate, RequestContext context) throws ResponseException {
PatientProgram patientProgram = getByUniqueId(uuid);
if (patientProgram != null) {
Date birthdate = patientProgram.getPatient().getBirthdate();
if (birthdate != null) {
Object dateEnrolledRaw = propertiesToUpdate.get("dateEnrolled");
if (dateEnrolledRaw != null) {
Date dateEnrolled = (Date) ConversionUtil.convert(dateEnrolledRaw, Date.class);
if (dateEnrolled != null && dateEnrolled.before(birthdate)) {
throw new IllegalRequestException(
"Enrollment date cannot be before the patient's date of birth");
}
}
Object dateCompletedRaw = propertiesToUpdate.get("dateCompleted");
if (dateCompletedRaw != null) {
Date dateCompleted = (Date) ConversionUtil.convert(dateCompletedRaw, Date.class);
if (dateCompleted != null && dateCompleted.before(birthdate)) {
throw new IllegalRequestException(
"Completion date cannot be before the patient's date of birth");
}
}
}
}
return super.update(uuid, propertiesToUpdate, context);
}

@Override
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
if (rep instanceof DefaultRepresentation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.test.Util;
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
import org.openmrs.module.webservices.rest.web.response.IllegalRequestException;
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.mock.web.MockHttpServletRequest;
Expand Down Expand Up @@ -191,6 +192,47 @@ public void shouldVoidPatientState() throws Exception {
Assert.assertEquals(existingPatientState.getState().getUuid(), Util.getByPath(result,"states[0]/state/uuid") );
}

// patient da7f524f (PATIENT_IN_A_PROGRAM_UUID) has birthdate 1975-04-08
@Test(expected = IllegalRequestException.class)
public void shouldRejectCreateWhenDateEnrolledIsBeforeBirthdate() throws Exception {
String json = "{ \"patient\":\"" + RestTestConstants1_8.PATIENT_IN_A_PROGRAM_UUID
+ "\", \"program\":\"" + RestTestConstants1_8.PROGRAM_UUID
+ "\", \"dateEnrolled\":\"1970-01-01\" }";
handle(newPostRequest(getURI(), json));
}

@Test(expected = IllegalRequestException.class)
public void shouldRejectCreateWhenDateCompletedIsBeforeBirthdate() throws Exception {
String json = "{ \"patient\":\"" + RestTestConstants1_8.PATIENT_IN_A_PROGRAM_UUID
+ "\", \"program\":\"" + RestTestConstants1_8.PROGRAM_UUID
+ "\", \"dateEnrolled\":\"2000-01-01\", \"dateCompleted\":\"1970-01-01\" }";
handle(newPostRequest(getURI(), json));
}

@Test(expected = IllegalRequestException.class)
public void shouldRejectUpdateWhenDateEnrolledIsBeforeBirthdate() throws Exception {
// PATIENT_PROGRAM_UUID belongs to patient da7f524f (birthdate 1975-04-08)
String json = "{ \"dateEnrolled\":\"1970-01-01\" }";
handle(newPostRequest(getURI() + "/" + getUuid(), json));
}

@Test(expected = IllegalRequestException.class)
public void shouldRejectUpdateWhenDateCompletedIsBeforeBirthdate() throws Exception {
// PATIENT_PROGRAM_UUID belongs to patient da7f524f (birthdate 1975-04-08)
String json = "{ \"dateCompleted\":\"1970-01-01\" }";
handle(newPostRequest(getURI() + "/" + getUuid(), json));
}

@Test
public void shouldCreateWhenDateEnrolledIsAfterBirthdate() throws Exception {
// patient da7f524f (PATIENT_IN_A_PROGRAM_UUID) has birthdate 1975-04-08
String json = "{ \"patient\":\"" + RestTestConstants1_8.PATIENT_IN_A_PROGRAM_UUID
+ "\", \"program\":\"" + RestTestConstants1_8.PROGRAM_UUID
+ "\", \"dateEnrolled\":\"2000-01-01\" }";
Object result = deserialize(handle(newPostRequest(getURI(), json)));
Assert.assertNotNull(result);
}

private static void sortPatientStatesBasedOnStartDate(List<PatientState> patientStates) {
Collections.sort(patientStates, new Comparator<PatientState>() {

Expand Down