Skip to content

Commit

Permalink
BAH-2763 | refactor to consolidate the params for message building in…
Browse files Browse the repository at this point in the history
… an array
  • Loading branch information
kavitha-sundararajan committed Feb 8, 2023
1 parent ae43b69 commit 047392e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public interface SMSService {

String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail);
String getPrescriptionMessage(Object[] prescriptionArguments);

Object sendSMS(String phoneNumber, String message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ public Object sendSMS(String phoneNumber, String message) {
}

@Override
public String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail) {
String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale(lang));
String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, new Locale(lang));
public String getPrescriptionMessage(Object[] prescriptionArguments) {
String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale((String) prescriptionArguments[0]));
String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, new Locale((String) prescriptionArguments[0]));
String smsTemplate = Context.getAdministrationService().getGlobalProperty(PRESCRIPTION_SMS_TEMPLATE);
Object[] arguments = {convertUTCToGivenFormat(visitDate, smsDateFormat, smsTimeZone),
Patient patient = (Patient) prescriptionArguments[2];
Object[] arguments = {convertUTCToGivenFormat((Date) prescriptionArguments[1], smsDateFormat, smsTimeZone),
patient.getGivenName() + " " + patient.getFamilyName(), patient.getGender(), patient.getAge().toString(),
providerDetail, location, prescriptionDetail};
(String) prescriptionArguments[4], (String) prescriptionArguments[3], (String) prescriptionArguments[5]};
if (StringUtils.isBlank(smsTemplate)) {
return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale(lang)).replace("\\n", System.lineSeparator());
return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale((String) prescriptionArguments[0])).replace("\\n", System.lineSeparator());
} else {
return new MessageFormat(smsTemplate).format(arguments).replace("\\n", System.lineSeparator());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public Object sendPresciptionSMS(PrescriptionSMS prescription) {
Map<BahmniDrugOrder, String> mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList);
String providerString = StringUtils.collectionToCommaDelimitedString(drugOrderService.getUniqueProviderNames(drugOrderList));
String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap, new Locale(prescription.getLocale()));
String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString);
Object[] prescriptionArguments = {prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString};
String prescriptionSMSContent = smsService.getPrescriptionMessage(prescriptionArguments);
return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws Parse
person.setGender("M");
person.setBirthdate(birthDate);
Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build();

String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023");
Object[] prescriptionArguments = {"en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"};
String prescriptionContent = smsService.getPrescriptionMessage(prescriptionArguments);
String expectedPrescriptionContent = "Date: 30-01-2023\n" +
"Prescription For Patient: testPersonName null, M, 13 years.\n" +
"Doctor: Superman (Bahmni)\n" +
Expand All @@ -73,9 +73,9 @@ public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws Parse

@Test
public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseException {
Object[] args = {"30-01-2023", "testPersonName null", "M", "13", "Superman", "Bahmni", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"};
Object[] arguments = {"30-01-2023", "testPersonName null", "M", "13", "Superman", "Bahmni", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"};
when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn(null);
when(messageSourceService.getMessage("bahmni.prescriptionSMSTemplate", args, new Locale("en"))).thenReturn("Date: 30-01-2023\nPrescription For Patient: testPersonName null, M, 13 years.\nDoctor: Superman (Bahmni)\n1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023");
when(messageSourceService.getMessage("bahmni.prescriptionSMSTemplate", arguments, new Locale("en"))).thenReturn("Date: 30-01-2023\nPrescription For Patient: testPersonName null, M, 13 years.\nDoctor: Superman (Bahmni)\n1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023");
when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST");
when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy");
Date visitDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023");
Expand All @@ -84,8 +84,8 @@ public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseExcepti
person.setGender("M");
person.setBirthdate(birthDate);
Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build();

String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023");
Object[] prescriptionArguments = {"en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"};
String prescriptionContent = smsService.getPrescriptionMessage(prescriptionArguments);
String expectedPrescriptionContent = "Date: 30-01-2023\n" +
"Prescription For Patient: testPersonName null, M, 13 years.\n" +
"Doctor: Superman (Bahmni)\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception {
"Prescription For Patient: testPersonName null, M, 13 years.\n" +
"Doctor: Superman (Bahmni)\n" +
"1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023";

Object[] prescriptionArguments = {prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"};
when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn("Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6}");
when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST");
when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy");
Expand All @@ -80,9 +80,7 @@ public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception {
when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList<BahmniDrugOrder>())).thenReturn(null);
when(bahmniDrugOrderService.getUniqueProviderNames(new ArrayList<BahmniDrugOrder>())).thenReturn(Stream.of("Superman").collect(Collectors.toSet()));
when(bahmniDrugOrderService.getPrescriptionAsString(null, new Locale("en"))).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023");
when(smsService.getPrescriptionMessage(prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(),
"Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"))
.thenReturn(sampleSMSContent);
when(smsService.getPrescriptionMessage(prescriptionArguments)).thenReturn(sampleSMSContent);
sharePrescriptionService.sendPresciptionSMS(prescriptionSMS);
verify(smsService, times(1)).sendSMS("+919999999999", sampleSMSContent);
}
Expand Down

0 comments on commit 047392e

Please sign in to comment.