Skip to content

Commit

Permalink
RESTWS-907: Cannot search for inactive drug orders in RefApp 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMujuziMoses committed Oct 5, 2023
1 parent 7ee4c56 commit 294ff7f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public static List<Order> getOrders(Patient patient, CareSetting careSetting, Or
Date asOfDate, boolean includeVoided) {

OrderService os = Context.getOrderService();
if (!INACTIVE.equals(status) && !ANY.equals(status)) {
if (!INACTIVE.equalsIgnoreCase(status) && !ANY.equalsIgnoreCase(status)) {
return os.getActiveOrders(patient, orderType, careSetting, asOfDate);
}

if (INACTIVE.equals(status)) {
if (INACTIVE.equalsIgnoreCase(status)) {
includeVoided = false;
}

List<Order> orders = os.getOrders(patient, careSetting, orderType, includeVoided);
if (INACTIVE.equals(status)) {
if (INACTIVE.equalsIgnoreCase(status)) {
removeActiveOrders(orders, asOfDate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.stream.Collectors;

import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -486,6 +490,31 @@ public void shouldGetAllInActiveOrdersForAPatient() throws Exception {
PropertyUtils.getProperty(resultList.get(1), "uuid").toString() });
assertThat(uuids, hasItems(expectedOrderUuids));
}

@Test
public void doSearch_shouldGetAllInActiveOrdersForAPatient() throws Exception {
SimpleObject results = deserialize(handle(newGetRequest(getURI(),
new Parameter("patient", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5"), new Parameter("status", "INACTIVE"),
new Parameter("careSetting", RestTestConstants1_10.CARE_SETTING_UUID))));
assertEquals(4, Util.getResultsSize(results));

List<Object> resultsList = Util.getResultsList(results);
List<Map<String, Object>> resultMap = new LinkedList<Map<String, Object>>();
for (Object o : resultsList) {
resultMap.add((Map<String, Object>) o);
}

String[] expectedStatuses = new String[] {"inactive", "inactive", "inactive", "inactive"};
List<String> statusList = new LinkedList<String>();
for (Map<String, Object> m : resultMap) {
for (String key : m.keySet()) {
if (key.equals("status")) {
statusList.add((String) m.get(key));
}
}
}
assertEquals(Arrays.asList(expectedStatuses), statusList);
}

@Test
public void shouldGetAllOrdersForAPatientInTheSpecifiedCareSetting() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -25,11 +26,14 @@

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.proxy.HibernateProxy;
import org.openmrs.OpenmrsObject;
import org.openmrs.Order;
import org.openmrs.api.context.Context;
import org.openmrs.module.ModuleUtil;
import org.openmrs.module.webservices.rest.SimpleObject;
Expand Down Expand Up @@ -397,7 +401,8 @@ public SimpleObject asRepresentation(T delegate, Representation representation)
simple = (SimpleObject) meth.invoke(handler, delegate);
else
simple = (SimpleObject) meth.invoke(handler, delegate, representation);


maybeDecorateWithStatus(simple, delegate);
maybeDecorateWithType(simple, delegate);
decorateWithResourceVersion(simple, representation);

Expand Down Expand Up @@ -441,6 +446,46 @@ private void maybeDecorateWithType(SimpleObject simple, T delegate) {
if (hasTypesDefined())
simple.add(RestConstants.PROPERTY_FOR_TYPE, getTypeName(delegate));
}

/**
* If this resource is an Order, add a STATUS field to the REST response which indicates whether the order is ACTIVE or INACTIVE.
*
* @param simple simplified representation which will be decorated with the status
* @param delegate the object that simple represents
*/

private void maybeDecorateWithStatus(SimpleObject simple, T delegate) {
if (delegate instanceof Order) {
Date asOfDate = new Date();
Order order = (Order) delegate;
if (order.isDiscontinued(asOfDate) || isExpired(order, asOfDate)) {
simple.add("status", "inactive");
} else {
simple.add("status", "active");
}
}
}

/**
* @param order to check the autoExpireDate
* @param checkDate to check against the order.autoExpireDate
* @return boolean true or false if the order.autoExpireDate is passed or not
*/
private boolean isExpired(Order order, Date checkDate) {
if (order.isVoided()) {
return false;
} else {
if (checkDate == null) {
checkDate = new Date();
}

if (checkDate.after(order.getDateCreated())) {
return order.getAutoExpireDate() != null && checkDate.after(order.getAutoExpireDate());
} else {
return false;
}
}
}

/**
* If this resources supports subclasses, this method gets the user-friendly type name for the
Expand Down

0 comments on commit 294ff7f

Please sign in to comment.