-
Notifications
You must be signed in to change notification settings - Fork 96
BAH-1280: Sms implementation #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shrey-beehyv
wants to merge
4
commits into
Bahmni:master
Choose a base branch
from
vishalreddysarasani:sms-implementation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9fa5dc5
SMS implementation changes
vishalreddysarasani 5ae5259
BAH-1280 WIP sms flow
vishalreddysarasani 6e3a082
BAH-1280 SMS flow added with new config values and even notifier flow
vishalreddysarasani dd18ffc
BAH-1280 removing default implementation logic
vishalreddysarasani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
api/src/main/java/org/openmrs/module/appointments/notification/SmsSender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.openmrs.module.appointments.notification; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface SmsSender { | ||
| void send(String message, List<String> to); | ||
| } |
24 changes: 24 additions & 0 deletions
24
api/src/main/java/org/openmrs/module/appointments/notification/impl/DefaultSmsSender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.openmrs.module.appointments.notification.impl; | ||
|
|
||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.openmrs.api.AdministrationService; | ||
| import org.openmrs.module.appointments.notification.SmsSender; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class DefaultSmsSender implements SmsSender { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one does not do anything. Any reason for it to be here? just the interfac "SmsSender" should not be enough? We can always check if the dependency is provided in code and not do anything. |
||
|
|
||
| private Log log = LogFactory.getLog(this.getClass()); | ||
|
|
||
| private AdministrationService administrationService; | ||
|
|
||
| public DefaultSmsSender(AdministrationService administrationService) { | ||
| this.administrationService = administrationService; | ||
| } | ||
|
|
||
| @Override | ||
| public void send(String message, List<String> to) { | ||
| //Write your implementation based on your SMS API | ||
| } | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
...va/org/openmrs/module/appointments/notification/impl/DefaultTCAppointmentSmsNotifier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package org.openmrs.module.appointments.notification.impl; | ||
|
|
||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.openmrs.Patient; | ||
| import org.openmrs.PersonAttribute; | ||
| import org.openmrs.api.context.Context; | ||
| import org.openmrs.module.appointments.model.Appointment; | ||
| import org.openmrs.module.appointments.model.AppointmentKind; | ||
| import org.openmrs.module.appointments.model.AppointmentProvider; | ||
| import org.openmrs.module.appointments.model.AppointmentServiceDefinition; | ||
| import org.openmrs.module.appointments.notification.*; | ||
| import org.openmrs.util.LocaleUtility; | ||
|
|
||
| import java.text.MessageFormat; | ||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class DefaultTCAppointmentSmsNotifier implements AppointmentEventNotifier { | ||
|
|
||
| private final static String PROP_PATIENT_SMS_TEMPLATE = "bahmni.appointment.teleConsultation.patientSmsNotificationTemplate"; | ||
| private final static String PROP_SEND_TC_APPT_SMS = "bahmni.appointment.teleConsultation.sendSms"; | ||
| private final static String PROP_SEND_TC_APPT_CONTACT_ATTRIBUTE = "bahmni.appointment.teleConsultation.contactAttribute"; | ||
|
|
||
| private static final String MEDIUM_SMS = "SMS"; | ||
| private static final String SMS_NOT_CONFIGURED = "Notification can not be sent to patient. Mobile number not configured."; | ||
| private static final String SMS_SENT = "SMS sent to Patient"; | ||
| private static final String SMS_FAILURE = "Failed to send sms to patient"; | ||
| private static final String SMS_NOT_SENT = "SMS notification for tele-consultation not configured to be sent to patient."; | ||
|
|
||
|
|
||
| private Log log = LogFactory.getLog(this.getClass()); | ||
| private SmsSender smsSender; | ||
|
|
||
| public DefaultTCAppointmentSmsNotifier() {} | ||
| public DefaultTCAppointmentSmsNotifier(SmsSender smsSender) { | ||
| this.smsSender = smsSender; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMedium() { | ||
| return MEDIUM_SMS; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isApplicable(final Appointment appointment) { | ||
| boolean sendSmsToPatient = shouldSendSmsToPatient(); | ||
| if (!sendSmsToPatient) { | ||
| log.warn(SMS_NOT_SENT); | ||
| } | ||
| if (appointment.getAppointmentKind() != null && appointment.getAppointmentKind().equals(AppointmentKind.Virtual)) { | ||
| return sendSmsToPatient; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public NotificationResult sendNotification(final Appointment appointment) throws NotificationException { | ||
| Patient patient = appointment.getPatient(); | ||
| String contactAttribute = Context.getAdministrationService().getGlobalProperty(PROP_SEND_TC_APPT_CONTACT_ATTRIBUTE); | ||
| PersonAttribute patientContactAttribute = patient.getPerson().getAttribute(contactAttribute); | ||
| Set<AppointmentProvider> providers = appointment.getProviders(); | ||
| List<String> contacts = new ArrayList<>(); | ||
|
|
||
| if (patientContactAttribute != null) { | ||
| contacts.add(patientContactAttribute.getValue()); | ||
| } | ||
|
|
||
| if (providers != null) { | ||
| for (AppointmentProvider provider : providers) { | ||
| PersonAttribute providerContactAttribute = provider.getProvider().getPerson().getAttribute(contactAttribute); | ||
| if (providerContactAttribute != null) { | ||
| contacts.add(providerContactAttribute.getValue()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (contacts.size() > 0) { | ||
| String patientName = appointment.getPatient().getGivenName(); | ||
| String message = getMessage(patientName, appointment.getService(), | ||
| providers, appointment.getStartDateTime(), | ||
| appointment.getTeleHealthVideoLink()); | ||
| try { | ||
| log.info("Sending sms through: " + smsSender.getClass()); | ||
| smsSender.send(message, contacts); | ||
| return new NotificationResult("", "SMS", NotificationResult.SUCCESS_STATUS, SMS_SENT); | ||
| } catch (Exception e) { | ||
| log.error(SMS_FAILURE, e); | ||
| throw new NotificationException(SMS_FAILURE, e); | ||
| } | ||
| } else { | ||
| log.warn(SMS_NOT_CONFIGURED); | ||
| return new NotificationResult(null, "SMS", NotificationResult.GENERAL_ERROR, SMS_NOT_CONFIGURED); | ||
| } | ||
| } | ||
|
|
||
| private String getMessage(String patientName, | ||
| AppointmentServiceDefinition service, | ||
| Set<AppointmentProvider> providers, | ||
| Date appointmentDate, String link) { | ||
| String smsTemplate = Context.getAdministrationService().getGlobalProperty(PROP_PATIENT_SMS_TEMPLATE); | ||
| String practitioners = | ||
| providers != null ? | ||
| providers.stream() | ||
| .map(appointmentProvider -> appointmentProvider.getProvider().getName()) | ||
| .collect(Collectors.joining(",")) | ||
| : ""; | ||
| Object[] arguments = {patientName, practitioners, appointmentDate, link}; | ||
| if (smsTemplate == null || "".equals(smsTemplate)) { | ||
| return Context.getMessageSourceService().getMessage(PROP_PATIENT_SMS_TEMPLATE, arguments, LocaleUtility.getDefaultLocale()); | ||
| } else { | ||
| return new MessageFormat(smsTemplate).format(arguments); | ||
| } | ||
| } | ||
|
|
||
| private boolean shouldSendSmsToPatient() { | ||
| String shouldSendSMS = Context.getAdministrationService().getGlobalProperty(PROP_SEND_TC_APPT_SMS, "false"); | ||
| return Boolean.valueOf(shouldSendSMS); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...ava/org/openmrs/module/appointments/service/impl/DefaultTCAppointmentSmsNotifierTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package org.openmrs.module.appointments.service.impl; | ||
|
|
||
| import org.databene.commons.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Ignore; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.ExpectedException; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.AdditionalMatchers; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
| import org.openmrs.Patient; | ||
| import org.openmrs.PersonAttribute; | ||
| import org.openmrs.PersonAttributeType; | ||
| import org.openmrs.api.AdministrationService; | ||
| import org.openmrs.api.context.Context; | ||
| import org.openmrs.messagesource.MessageSourceService; | ||
| import org.openmrs.module.appointments.model.Appointment; | ||
| import org.openmrs.module.appointments.notification.AppointmentEventNotifier; | ||
| import org.openmrs.module.appointments.notification.NotificationException; | ||
| import org.openmrs.module.appointments.notification.SmsSender; | ||
| import org.openmrs.module.appointments.notification.impl.DefaultTCAppointmentPatientEmailNotifier; | ||
| import org.openmrs.module.appointments.notification.impl.DefaultTCAppointmentSmsNotifier; | ||
| import org.powermock.api.mockito.PowerMockito; | ||
| import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| import org.powermock.modules.junit4.PowerMockRunner; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.mockito.Matchers.any; | ||
| import static org.mockito.Matchers.eq; | ||
| import static org.mockito.Mockito.*; | ||
| import static org.powermock.api.mockito.PowerMockito.mockStatic; | ||
| import static org.powermock.api.mockito.PowerMockito.when; | ||
|
|
||
| @RunWith(PowerMockRunner.class) | ||
| @PrepareForTest(Context.class) | ||
| public class DefaultTCAppointmentSmsNotifierTest { | ||
| private static final String BAHMNI_APPOINTMENT_TELE_CONSULTATION_SMS_NOTIFICATION_TEMPLATE = "bahmni.appointment.teleConsultation.patientSmsNotificationTemplate"; | ||
| private static final String BAHMNI_APPOINTMENT_TELE_CONSULTATION_CONTACT_ATTRIBUTE = "bahmni.appointment.teleConsultation.contactAttribute"; | ||
| private AppointmentEventNotifier tcAppointmentEventNotifier; | ||
|
|
||
| @Mock | ||
| private AdministrationService administrationService; | ||
|
|
||
| @Mock | ||
| private SmsSender smsSender; | ||
|
|
||
| @Rule | ||
| public ExpectedException expectedException = ExpectedException.none(); | ||
|
|
||
| @Before | ||
| public void init() { | ||
| MockitoAnnotations.initMocks(this); | ||
| mockStatic(Context.class); | ||
| tcAppointmentEventNotifier = new DefaultTCAppointmentSmsNotifier(smsSender); | ||
| PowerMockito.when(Context.getAdministrationService()).thenReturn(administrationService); | ||
| when(administrationService.getGlobalProperty(BAHMNI_APPOINTMENT_TELE_CONSULTATION_SMS_NOTIFICATION_TEMPLATE)).thenReturn("Email body"); | ||
| when(administrationService.getGlobalProperty(BAHMNI_APPOINTMENT_TELE_CONSULTATION_CONTACT_ATTRIBUTE)).thenReturn("primaryContact"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldSendTeleconsultationAppointmentLinkSms() throws Exception { | ||
| Appointment appointment = buildAppointment(); | ||
| tcAppointmentEventNotifier.sendNotification(appointment); | ||
| List<String> contacts = new ArrayList<>(); | ||
| contacts.add("+61411111111"); | ||
| verify(smsSender).send( | ||
| eq("Email body"), | ||
| eq(contacts)); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldThrowExceptionIfSendingFails() throws NotificationException { | ||
| Appointment appointment = buildAppointment(); | ||
| doThrow(new RuntimeException()).when(smsSender).send(any(), any()); | ||
| expectedException.expect(NotificationException.class); | ||
| tcAppointmentEventNotifier.sendNotification(appointment); | ||
| } | ||
|
|
||
| private Appointment buildAppointment() { | ||
| Appointment appointment = new Appointment(); | ||
| Patient patient = new Patient(); | ||
| patient.setUuid("patientUuid"); | ||
| PersonAttributeType personAttributeType = new PersonAttributeType(); | ||
| personAttributeType.setName("primaryContact"); | ||
| patient.addAttribute(new PersonAttribute(personAttributeType, "+61411111111")); | ||
| appointment.setPatient(patient); | ||
| return appointment; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need another http client? We already use apache's httpclient