Skip to content

Commit

Permalink
[Phani | Abinaya] | A-1204949175348855 : Save Patient Allergies as pa…
Browse files Browse the repository at this point in the history
…rt of Bahmni Encounter Transaction (#232)

* [Phani | Abinaya] Add Allergies to Bahmni Encounter API

* [Phani] Add test

* [Phani] Update imports
  • Loading branch information
Phanindra-tw authored Oct 10, 2023
1 parent 29b508e commit 71a445a
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 0 deletions.
13 changes: 13 additions & 0 deletions bahmni-emr-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod</artifactId>
<version>${openMRSWebServicesVersion}</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod-common</artifactId>
<version>${openMRSWebServicesVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.openmrs.module.bahmniemrapi.allergy.contract;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class AllergenRequest {
private String allergenKind;
private String codedAllergen;
private String nonCodedAllergen;

public String getAllergenKind() {
return allergenKind;
}

public void setAllergenKind(String allergenKind) {
this.allergenKind = allergenKind;
}

public String getCodedAllergen() {
return codedAllergen;
}

public void setCodedAllergen(String codedAllergen) {
this.codedAllergen = codedAllergen;
}

public String getNonCodedAllergen() {
return nonCodedAllergen;
}

public void setNonCodedAllergen(String nonCodedAllergen) {
this.nonCodedAllergen = nonCodedAllergen;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.openmrs.module.bahmniemrapi.allergy.contract;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class BahmniAllergyRequest {
private AllergenRequest allergen;
private List<ReactionRequest> reactions;
private String severity;
private String comment;

public AllergenRequest getAllergen() {
return allergen;
}

public void setAllergen(AllergenRequest allergen) {
this.allergen = allergen;
}

public List<ReactionRequest> getReactions() {
return reactions;
}

public void setReactions(List<ReactionRequest> reactions) {
this.reactions = reactions;
}

public String getSeverity() {
return severity;
}

public void setSeverity(String severity) {
this.severity = severity;
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.openmrs.module.bahmniemrapi.allergy.contract;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ReactionRequest {
private String reaction;
private String nonCodedReaction;

public String getReaction() {
return reaction;
}

public void setReaction(String reaction) {
this.reaction = reaction;
}

public String getNonCodedReaction() {
return nonCodedReaction;
}

public void setNonCodedReaction(String nonCodedReaction) {
this.nonCodedReaction = nonCodedReaction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.joda.time.DateTime;
import org.openmrs.module.bahmniemrapi.accessionnote.contract.AccessionNote;
import org.openmrs.module.bahmniemrapi.allergy.contract.BahmniAllergyRequest;
import org.openmrs.module.bahmniemrapi.diagnosis.contract.BahmniDiagnosis;
import org.openmrs.module.bahmniemrapi.diagnosis.contract.BahmniDiagnosisRequest;
import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction;
Expand All @@ -30,6 +31,7 @@ public class BahmniEncounterTransaction {
private String visitType;
private String patientId;
private String reason;
private BahmniAllergyRequest allergy;


public BahmniEncounterTransaction() {
Expand Down Expand Up @@ -383,5 +385,13 @@ private EncounterTransaction.DrugOrder getOldestDrugOrder() {
public boolean isAssociatedToPatientProgram() {
return StringUtils.isNotBlank(getPatientProgramUuid());
}

public BahmniAllergyRequest getAllergy() {
return allergy;
}

public void setAllergy(BahmniAllergyRequest allergy) {
this.allergy = allergy;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.VisitType;
import org.openmrs.Concept;
import org.openmrs.Allergen;
import org.openmrs.AllergenType;
import org.openmrs.Allergy;
import org.openmrs.AllergyReaction;
import org.openmrs.api.EncounterService;
import org.openmrs.api.LocationService;
import org.openmrs.api.PatientService;
Expand All @@ -15,6 +20,9 @@
import org.openmrs.api.context.Context;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.bahmniemrapi.BahmniEmrAPIException;
import org.openmrs.module.bahmniemrapi.allergy.contract.AllergenRequest;
import org.openmrs.module.bahmniemrapi.allergy.contract.BahmniAllergyRequest;
import org.openmrs.module.bahmniemrapi.allergy.contract.ReactionRequest;
import org.openmrs.module.bahmniemrapi.encountertransaction.command.EncounterDataPostSaveCommand;
import org.openmrs.module.bahmniemrapi.encountertransaction.command.EncounterDataPreSaveCommand;
import org.openmrs.module.bahmniemrapi.encountertransaction.command.impl.BahmniVisitAttributeService;
Expand All @@ -40,6 +48,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;

@Transactional
public class BahmniEncounterTransactionServiceImpl extends BaseOpenmrsService implements BahmniEncounterTransactionService {
Expand Down Expand Up @@ -128,6 +138,9 @@ public BahmniEncounterTransaction save(BahmniEncounterTransaction bahmniEncounte
//Get the saved encounter transaction from emr-api
String encounterUuid = encounterTransaction.getEncounterUuid();
Encounter currentEncounter = encounterService.getEncounterByUuid(encounterUuid);
if(bahmniEncounterTransaction.getAllergy() != null){
handleAllergy(bahmniEncounterTransaction, currentEncounter, patient);
}

boolean includeAll = false;
EncounterTransaction updatedEncounterTransaction = encounterTransactionMapper.map(currentEncounter, includeAll);
Expand All @@ -138,6 +151,29 @@ public BahmniEncounterTransaction save(BahmniEncounterTransaction bahmniEncounte
return bahmniEncounterTransactionMapper.map(updatedEncounterTransaction, includeAll);
}

private void handleAllergy(BahmniEncounterTransaction bahmniEncounterTransaction, Encounter currentEncounter, Patient patient) {

BahmniAllergyRequest allergyRequest = bahmniEncounterTransaction.getAllergy();
AllergenRequest allergenRequest = allergyRequest.getAllergen();
Concept allergenConcept = Context.getConceptService().getConceptByUuid(allergenRequest.getCodedAllergen());
Allergen allergen = new Allergen(AllergenType.valueOf(allergenRequest.getAllergenKind()), allergenConcept,allergenRequest.getNonCodedAllergen());
Concept severityConcept = Context.getConceptService().getConceptByUuid(allergyRequest.getSeverity());
List<ReactionRequest> reactions = allergyRequest.getReactions();
List<AllergyReaction> allergyReactions = new ArrayList<>();
Allergy allergy = new Allergy(patient, allergen, severityConcept, allergyRequest.getComment(), Collections.emptyList());

reactions.forEach(reaction -> {
Concept reactionConcept = Context.getConceptService().getConceptByUuid(reaction.getReaction());
AllergyReaction allergyReaction = new AllergyReaction(allergy, reactionConcept, reaction.getNonCodedReaction());
allergyReactions.add(allergyReaction);
});

allergy.setEncounter(currentEncounter);
allergy.setReactions(allergyReactions);

patientService.saveAllergy(allergy);
}

private void setEncounterTypeUuid(BahmniEncounterTransaction bahmniEncounterTransaction) {
String encounterUuid = bahmniEncounterTransaction.getEncounterUuid();
if (!StringUtils.isBlank(encounterUuid)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openmrs.module.bahmniemrapi.builder;

import org.openmrs.module.bahmniemrapi.allergy.contract.BahmniAllergyRequest;
import org.openmrs.module.bahmniemrapi.diagnosis.contract.BahmniDiagnosisRequest;
import org.openmrs.module.bahmniemrapi.encountertransaction.contract.BahmniEncounterTransaction;
import org.openmrs.module.bahmniemrapi.encountertransaction.contract.BahmniObservation;
Expand Down Expand Up @@ -82,4 +83,9 @@ public BahmniEncounterTransactionBuilder withDiagnoses(BahmniDiagnosisRequest...
bahmniEncounterTransaction.setBahmniDiagnoses(Arrays.asList(bahmniDiagnosisRequests));
return this;
}

public BahmniEncounterTransactionBuilder withBahmniAllergyRequest(BahmniAllergyRequest bahmniAllergyRequest) {
bahmniEncounterTransaction.setAllergy(bahmniAllergyRequest);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.VisitAttribute;
import org.openmrs.Allergies;
import org.openmrs.api.EncounterService;
import org.openmrs.api.OrderService;
import org.openmrs.api.PatientService;
import org.openmrs.api.VisitService;
import org.openmrs.api.context.Context;
import org.openmrs.module.bahmniemrapi.BaseIntegrationTest;
import org.openmrs.module.bahmniemrapi.allergy.contract.AllergenRequest;
import org.openmrs.module.bahmniemrapi.allergy.contract.BahmniAllergyRequest;
import org.openmrs.module.bahmniemrapi.allergy.contract.ReactionRequest;
import org.openmrs.module.bahmniemrapi.builder.BahmniDiagnosisRequestBuilder;
import org.openmrs.module.bahmniemrapi.builder.BahmniEncounterTransactionBuilder;
import org.openmrs.module.bahmniemrapi.builder.BahmniObservationBuilder;
Expand Down Expand Up @@ -87,6 +91,7 @@ public void setUp() throws Exception {
executeDataSet("visitAttributeDataSet.xml");
executeDataSet("drugOrderTestData.xml");
executeDataSet("concepts.xml");
executeDataSet("allergyTestData.xml");
}

@Test
Expand Down Expand Up @@ -684,6 +689,37 @@ public void shouldSaveDiagnoses(){
assertThat(savedEncounter.getAllObs(false).size(), is(equalTo(6)));
}

@Test
public void shouldSaveAllergyForAPatient() {

BahmniAllergyRequest bahmniAllergyRequest = new BahmniAllergyRequest();
AllergenRequest allergenRequest = new AllergenRequest();
allergenRequest.setAllergenKind("DRUG");
allergenRequest.setCodedAllergen("a0a690fd-75a4-447t-bmc6-4e1c15f4ed6b");

ReactionRequest reactionRequest = new ReactionRequest();
reactionRequest.setReaction("a0a690fd-75a4-447t-bmc6-4e1c15f4ed6c");
bahmniAllergyRequest.setAllergen(allergenRequest);
bahmniAllergyRequest.setReactions(Arrays.asList(reactionRequest));
bahmniAllergyRequest.setSeverity("a0a690fd-75a4-447t-bmc6-4e1c15f4ed6d");
bahmniAllergyRequest.setComment("Onset Date");

BahmniEncounterTransaction bahmniEncounterTransaction = new BahmniEncounterTransactionBuilder()
.withVisitTypeUuid(VISIT_TYPE_UUID)
.withEncounterTypeUuid(ENCOUNTER_TYPE_UUID)
.withPatientUuid(PATIENT_UUID)
.withVisitUuid(VISIT_UUID)
.withBahmniAllergyRequest(bahmniAllergyRequest)
.build();

BahmniEncounterTransaction encounterTransaction = bahmniEncounterTransactionService.save(bahmniEncounterTransaction);
Context.flushSession();
Context.clearSession();
Allergies patientAllergies = patientService.getAllergies(patientService.getPatientByUuid(PATIENT_UUID));
assertThat(patientAllergies.size(), is(equalTo(1)));

}

@Test
public void shouldRunAllRegisteredHandlers() {
Date obsDate = new Date();
Expand Down
9 changes: 9 additions & 0 deletions bahmni-emr-api/src/test/resources/allergyTestData.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<concept concept_id="205" retired="0" datatype_id="5" creator="1"
date_created="2023-09-28 9:30:35" uuid="a0a690fd-75a4-447t-bmc6-4e1c15f4ed6b"/>
<concept concept_id="206" retired="0" datatype_id="5" creator="1"
date_created="2023-09-28 9:32:47" uuid="a0a690fd-75a4-447t-bmc6-4e1c15f4ed6c"/>
<concept concept_id="207" retired="0" datatype_id="5" creator="1"
date_created="2023-09-28 9:35:57" uuid="a0a690fd-75a4-447t-bmc6-4e1c15f4ed6d"/>
</dataset>

0 comments on commit 71a445a

Please sign in to comment.