Skip to content

Commit

Permalink
BAH-3745 | Fix. Handle Syncing Of Quantity For Revised Drug Orders (#108
Browse files Browse the repository at this point in the history
)

* Parvathy | BAH-3745 | Fix. Handle Syncing Of Quantity For Same Drug Orders

* Parvathy | BAH-3745 | Refactor. Move removeDuplicateOrders Function to MapERPOrders Class

* Parvathy | BAH-3745 | Fix. Add Previous Order Of Revised Order To OpenERPOrders

* Parvathy | BAH-3745 | Add. Test Cases

* Parvathy | BAH-3745 | Refactor. Rename drugOrderUri Property

* Parvathy | BAH-3745 | Fix. Failing Tests
  • Loading branch information
parvathy00 authored May 15, 2024
1 parent befbb41 commit 75198c5
Show file tree
Hide file tree
Showing 16 changed files with 842 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public String getFeedUri(String feedname) {
@Value("${saleable.feed.generator.uri}")
private String saleableFeedUri;

@Value("${drugorder.uri}")
private String drugOrderUri;

public String getFeedUriForJob(Jobs feedJob) {
switch (feedJob){
case CUSTOMER_FEED: return customFeedUri;
Expand Down Expand Up @@ -88,6 +91,11 @@ public String getOdooRESTUrlEndPoint(Jobs feedJob) {
default: throw new RuntimeException("Can not identify endpoint URI for requested Job.");
}
}

public String getDrugOrderUri() {
return drugOrderUri;
}

@Value("${openerp.host}")
private String openErpHost;

Expand Down Expand Up @@ -224,6 +232,7 @@ private HashMap<String, String> getInfo() {
values.put("drug.feed.generator.uri",drugFeedUri );
values.put("lab.feed.generator.uri",labFeedUri );
values.put("saleorder.feed.generator.uri", saleOrderFeedUri);
values.put("drugorder.uri", drugOrderUri);
values.put("openerp.host",openErpHost );
values.put("openerp.port",openErpPort );
values.put("openerp.database",openErpDatabase );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public AtomFeedClient getFeedClient(OpenERPAtomFeedProperties openERPAtomFeedPro
}
try {
String urlPrefix = getURLPrefix(jobName,openERPAtomFeedProperties);
EventWorker eventWorker = workerFactory.getWorker(jobName, feedUri, odooURL, openERPContext, urlPrefix);
EventWorker eventWorker = workerFactory.getWorker(jobName, feedUri, odooURL, openERPContext, urlPrefix, openERPAtomFeedProperties, isRestEnabled);
return new AtomFeedClient(allFeeds, allMarkers, allFailedEvents, atomFeedProperties(openERPAtomFeedProperties), transactionManager, new URI(feedUri), eventWorker) ;
} catch (URISyntaxException e) {
throw new RuntimeException("error for uri:" + feedUri, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
package org.bahmni.feed.openerp.domain.encounter;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.bahmni.feed.openerp.ObjectMapperRepository;
import org.bahmni.feed.openerp.OpenERPAtomFeedProperties;
import org.bahmni.feed.openerp.client.OpenMRSWebClient;
import org.bahmni.feed.openerp.domain.visit.OpenMRSVisit;
import org.bahmni.feed.openerp.domain.visit.VisitAttributes;
import org.bahmni.openerp.web.request.builder.Parameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MapERPOrders extends OpenMRSEncounterEvent {

private final Boolean isOdoo16;
private OpenMRSEncounter openMRSEncounter;
private OpenMRSVisit openMRSVisit;
private OpenMRSWebClient openMRSWebClient;
private OpenERPAtomFeedProperties openERPAtomFeedProperties;
private static Logger logger = LoggerFactory.getLogger(MapERPOrders.class);

public MapERPOrders(OpenMRSEncounter openMRSEncounter, OpenMRSVisit openMRSVisit) {
public MapERPOrders(OpenMRSEncounter openMRSEncounter, OpenMRSVisit openMRSVisit, OpenMRSWebClient openMRSWebClient, OpenERPAtomFeedProperties openERPAtomFeedProperties, Boolean isOdoo16) {
this.openMRSEncounter = openMRSEncounter;
this.openMRSVisit = openMRSVisit;

this.openMRSWebClient = openMRSWebClient;
this.openERPAtomFeedProperties = openERPAtomFeedProperties;
this.isOdoo16 = isOdoo16;
}

public List<Parameter> getParameters(String eventId, String feedURIForLastReadEntry, String feedURI) throws IOException {
Expand Down Expand Up @@ -74,10 +88,12 @@ private String mapOpenERPOrders() throws IOException {
openERPOrder.setConceptName(drugOrder.getConceptName());
openERPOrders.add(openERPOrder);

if(isOdoo16) {
addPreviousDrugOrder(openERPOrder, openERPOrders);
}
}



Map<String, OpenERPOrder> latestOrders = new LinkedHashMap<>();
for (OpenMRSOrder order : openMRSEncounter.getOrders()) {
OpenERPOrder openERPOrder = new OpenERPOrder();
openERPOrder.setVisitId(openMRSEncounter.getVisitUuid());
Expand All @@ -95,12 +111,73 @@ private String mapOpenERPOrders() throws IOException {
openERPOrder.setType(order.getOrderType());
openERPOrder.setVisitType(getVisitType());
openERPOrder.setProviderName(providerName);
openERPOrders.add(openERPOrder);
}

latestOrders = findLatestOrder(openERPOrder, latestOrders);
}
for (OpenERPOrder latestOrder : latestOrders.values()) {
openERPOrders.add(latestOrder);
}
return ObjectMapperRepository.objectMapper.writeValueAsString(openERPOrders);
}

private void addPreviousDrugOrder(OpenERPOrder openERPOrder, OpenERPOrders openERPOrders) {
if ("REVISE".equals(openERPOrder.getAction()) && !isPreviousOrderFound(openERPOrder, openERPOrders)) {
OpenERPOrder previousOrder = fetchPreviousDrugOrder(openERPOrder.getPreviousOrderId());
if (previousOrder != null) {
openERPOrders.add(previousOrder);
}
}
}

private boolean isPreviousOrderFound(OpenERPOrder openERPOrder, OpenERPOrders openERPOrders) {
List<OpenERPOrder> existingOrders = openERPOrders.getOpenERPOrders();
for (OpenERPOrder existingOrder : existingOrders) {
if (existingOrder.getOrderId().equals(openERPOrder.getPreviousOrderId())) {
return true;
}
}
return false;
}

private OpenERPOrder fetchPreviousDrugOrder(String previousOrderId) {
String apiUrl = openERPAtomFeedProperties.getDrugOrderUri() + "/" + previousOrderId;
final ObjectMapper mapper = new ObjectMapper();
try {
String response = openMRSWebClient.get(URI.create(apiUrl));
OpenMRSDrugOrder previousOrder = mapper.readValue(response, OpenMRSDrugOrder.class);
OpenERPOrder order = new OpenERPOrder();
order.setOrderId(previousOrder.getUuid());
order.setPreviousOrderId(previousOrder.getUuid());
order.setEncounterId(previousOrder.getEncounterUuid());
order.setProductId(previousOrder.getDrugUuid());
order.setProductName(previousOrder.getDrugName());
order.setQuantity(previousOrder.getQuantity());
order.setQuantityUnits(previousOrder.getQuantityUnits());
order.setAction(previousOrder.getAction());
order.setVisitId(previousOrder.getVisitUuid());
order.setVisitType(getVisitType());
order.setType(previousOrder.getOrderType());
order.setVoided(previousOrder.isVoided());
order.setProviderName(previousOrder.getProviderName());
order.setDispensed("false");
order.setConceptName(previousOrder.getConceptName());
order.setDateCreated(previousOrder.getDateActivated());
return order;
} catch (Exception e) {
logger.error("Error occurred while fetching previous drug order: " + e.getMessage());
throw new RuntimeException("Failed to fetch previous drug order", e);
}
}

//Filters orders to keep only the latest action for each product. This is necessary for ensuring consistent and accurate quotation generation, particularly when order objects may not be in chronological order.
private Map<String, OpenERPOrder> findLatestOrder(OpenERPOrder openERPOrder, Map<String, OpenERPOrder> latestOrders) {
String productId = openERPOrder.getProductId();
if (!latestOrders.containsKey(productId) || latestOrders.get(productId).getDateCreated().before(openERPOrder.getDateCreated())) {
latestOrders.put(productId, openERPOrder);
}
return latestOrders;
}

private String getVisitType() {
for (VisitAttributes visitAttribute : openMRSVisit.getAttributes()) {
if (visitAttribute.getAttributeType().getDisplay().equals("Visit Status")) {
Expand All @@ -109,6 +186,4 @@ private String getVisitType() {
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,11 @@ public OpenERPOrders(String id){
this.openERPOrders = new ArrayList<OpenERPOrder>();
}

public List<OpenERPOrder> getOpenERPOrders() {
return removeDuplicateOrders(openERPOrders);
public OpenERPOrders(){
}

//Filters orders to keep only the latest action for each product. This is necessary for ensuring consistent and accurate quotation generation, particularly when order objects may not be in chronological order.
public List<OpenERPOrder> removeDuplicateOrders(List<OpenERPOrder> orders) {
Map<String, OpenERPOrder> latestOrders = new LinkedHashMap<>();
for (OpenERPOrder order : orders) {
if("Drug Order".equals(order.getType())) {
String productId = order.getProductId();
if (latestOrders.containsKey(productId)) {
OpenERPOrder existingOrder = latestOrders.get(productId);
double updatedQuantity = "NEW".equals(order.getAction()) ? existingOrder.getQuantity() + order.getQuantity() : existingOrder.getQuantity() - order.getQuantity();
existingOrder.setQuantity(updatedQuantity);
latestOrders.put(productId, existingOrder);
} else {
latestOrders.put(productId, order);
}
} else {
latestOrders.merge(order.getProductId(), order, (existingOrder, newOrder) ->
(existingOrder.getDateCreated().before(newOrder.getDateCreated())) ? newOrder : existingOrder
);
}
}
return new ArrayList<>(latestOrders.values());
public List<OpenERPOrder> getOpenERPOrders() {
return openERPOrders;
}

public void add(OpenERPOrder order) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class OpenMRSDrugOrder {
private String orderType;
private Date dateActivated;
private boolean voided;
private String encounterUuid;
private OpenMRSVisit visit;
private Provider provider;

public OpenMRSConcept getConcept() {
return concept;
Expand Down Expand Up @@ -74,6 +77,18 @@ public String getQuantityUnits() {
return getDosingInstructions() != null ? getDosingInstructions().getQuantityUnits() : null;
}

public String getEncounterUuid() {
return encounterUuid;
}

public String getVisitUuid() {
return getVisit().getUuid();
}

public OpenMRSVisit getVisit() {
return visit;
}

public boolean isVoided() {
return voided;
}
Expand All @@ -82,6 +97,14 @@ public String getOrderType() {
return orderType;
}

public String getProviderName() {
return getProvider().getName();
}

public Provider getProvider() {
return provider;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class OpenMRSDrug {
private String uuid;
Expand Down Expand Up @@ -114,4 +137,20 @@ public String getQuantityUnits() {
return quantityUnits;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class OpenMRSVisit {
private String uuid;
public String getUuid() {
return uuid;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Provider {
private String name;
public String getName() {
return name;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bahmni.feed.openerp.worker;

import org.bahmni.feed.openerp.OpenERPAtomFeedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bahmni.feed.openerp.ObjectMapperRepository;
Expand All @@ -17,21 +18,25 @@
import java.net.URI;

public class OpenERPSaleOrderEventWorker implements EventWorker {
private final Boolean isOdoo16;
OpenERPContext openERPContext;
private final String feedUrl;
private final String odooURL;
private final OpenMRSWebClient webClient;
private final String urlPrefix;
private final OpenERPAtomFeedProperties openERPAtomFeedProperties;


private static Logger logger = LoggerFactory.getLogger(OpenERPSaleOrderEventWorker.class);

public OpenERPSaleOrderEventWorker(String feedUrl, String odooURL, OpenERPContext openERPContext, OpenMRSWebClient webClient, String urlPrefix) {
public OpenERPSaleOrderEventWorker(String feedUrl, String odooURL, OpenERPContext openERPContext, OpenMRSWebClient webClient, String urlPrefix, OpenERPAtomFeedProperties openERPAtomFeedProperties, Boolean isOdoo16) {
this.feedUrl = feedUrl;
this.odooURL = odooURL;
this.openERPContext = openERPContext;
this.webClient = webClient;
this.urlPrefix = urlPrefix;
this.openERPAtomFeedProperties = openERPAtomFeedProperties;
this.isOdoo16 = isOdoo16;
}

@Override
Expand Down Expand Up @@ -63,7 +68,7 @@ private OpenERPRequest mapRequest(Event event) throws IOException {
String visitContent = webClient.get(URI.create(urlPrefix + visitURL));

OpenMRSVisit openMRSVisit = ObjectMapperRepository.objectMapper.readValue(visitContent, OpenMRSVisit.class);
MapERPOrders mapERPOrders = new MapERPOrders(openMRSEncounter, openMRSVisit);
MapERPOrders mapERPOrders = new MapERPOrders(openMRSEncounter, openMRSVisit, webClient, openERPAtomFeedProperties, isOdoo16);

OpenERPRequest erpRequest = new OpenERPRequest("atom.event.worker", "process_event", mapERPOrders.getParameters(event.getId(), event.getFeedUri(), feedUrl));
if (event.getFeedUri() == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bahmni.feed.openerp.worker;

import org.bahmni.feed.openerp.OpenERPAtomFeedProperties;
import org.bahmni.feed.openerp.job.Jobs;
import org.bahmni.feed.openerp.client.WebClientProvider;
import org.bahmni.openerp.web.client.strategy.OpenERPContext;
Expand All @@ -14,10 +15,10 @@ public WorkerFactory(WebClientProvider webClientProvider) {
}


public EventWorker getWorker(Jobs jobName, String feedUrl, String odooURL, OpenERPContext openERPContext, String urlPrefix) {
public EventWorker getWorker(Jobs jobName, String feedUrl, String odooURL, OpenERPContext openERPContext, String urlPrefix, OpenERPAtomFeedProperties openERPAtomFeedProperties, Boolean isOdoo16) {
switch (jobName){
case CUSTOMER_FEED: return new OpenERPCustomerServiceEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix);
case SALEORDER_FEED: return new OpenERPSaleOrderEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix);
case SALEORDER_FEED: return new OpenERPSaleOrderEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix, openERPAtomFeedProperties, isOdoo16);
case OPENELIS_SALEORDER_FEED: return new OpenElisSaleOrderEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.openElisWebClient(), urlPrefix);
case DRUG_FEED: return new OpenERPDrugServiceEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix);
case LAB_TEST_FEED: return new OpenERPLabTestServiceEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ openelis.saleorder.feed.generator.uri=http://localhost:8052/openelis/ws/feed/pat
drug.feed.generator.uri=http://localhost:8050/openmrs/ws/atomfeed/drug/recent
lab.feed.generator.uri=http://localhost:8050/openmrs/ws/atomfeed/lab/recent
saleable.feed.generator.uri=http://localhost:8050/openmrs/ws/atomfeed/saleable/recent
drugorder.uri=http://localhost:8050/openmrs/ws/rest/v1/bahmnicore/drugOrders

chunking.strategy=number

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public class OpenERPCustomerFeedIT {

@Autowired
private AtomFeedSpringTransactionSupport transactionSupport;
private Boolean isOdoo16;

@Before
public void setUp() throws URISyntaxException {
Expand Down Expand Up @@ -191,7 +192,7 @@ public void shouldCreateCustomerInOpenERP() throws URISyntaxException, FeedExcep

when(openMRSAuthenticator.authenticate("mrsuser", "mrspwd", ObjectMapperRepository.objectMapper)).thenReturn(authenticationResponse);
when(webClientProvider.getWebClient(any(Jobs.class))).thenReturn(webClient);
when(workerFactory.getWorker(Jobs.CUSTOMER_FEED, feedUrl, odooURL, openERPContext, "http://mrs.auth.uri")).thenReturn(openERPCustomerServiceEventWorker);
when(workerFactory.getWorker(Jobs.CUSTOMER_FEED, feedUrl, odooURL, openERPContext, "http://mrs.auth.uri", atomFeedProperties, isOdoo16)).thenReturn(openERPCustomerServiceEventWorker);

AtomFeedClientHelper clientHelper = mock(AtomFeedClientHelper.class);
AtomFeedClient atomFeedClient = new AtomFeedClient(allFeedsMock, allMarkersJdbc, allFailedEvents, FeedClientFactory.atomFeedProperties(atomFeedProperties),
Expand Down
Loading

0 comments on commit 75198c5

Please sign in to comment.