Skip to content

Commit

Permalink
Caught exception when sms-service is not up
Browse files Browse the repository at this point in the history
  • Loading branch information
atishbeehyv123 committed Jul 9, 2023
1 parent ade2221 commit 026084b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.openmrs.module.appointments.service.AppointmentsService;
import org.openmrs.scheduler.tasks.AbstractTask;

import java.net.UnknownHostException;
import java.util.List;

public class ReminderForAppointment extends AbstractTask {
Expand All @@ -24,7 +25,11 @@ public void execute() {
String schedulerReminderTime = administrationService.getGlobalPropertyObject("SchedulerReminderBeforeHours").getPropertyValue();
List<Appointment> appointments = appointmentsService.getAllAppointmentsReminder(schedulerReminderTime);
for (Appointment appointment: appointments) {
appointmentsService.sendAppointmentReminderSMS(appointment);
try {
appointmentsService.sendAppointmentReminderSMS(appointment);
} catch (UnknownHostException e) {
throw new RuntimeException("Exception occurred while sending sms ", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.openmrs.module.appointments.validator.AppointmentValidator;
import org.springframework.transaction.annotation.Transactional;

import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand All @@ -24,11 +25,11 @@ public interface AppointmentsService {

@Transactional
@Authorized({MANAGE_APPOINTMENTS, MANAGE_APPOINTMENTS})
void sendAppointmentReminderSMS(Appointment appointment);
void sendAppointmentReminderSMS(Appointment appointment) throws UnknownHostException;

@Transactional
@Authorized({MANAGE_APPOINTMENTS})
void sendAppointmentBookingSMS(Appointment appointment);
void sendAppointmentBookingSMS(Appointment appointment) throws UnknownHostException;


@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.net.UnknownHostException;
import java.time.Instant;
import java.util.*;
import java.util.function.Supplier;
Expand Down Expand Up @@ -124,7 +125,7 @@ private boolean isCurrentUserSamePersonAsOneOfTheAppointmentProviders(Set<Appoin

@Transactional
@Override
public void sendAppointmentReminderSMS(Appointment appointment) {
public void sendAppointmentReminderSMS(Appointment appointment) throws UnknownHostException {
PersonAttribute phoneNumber = appointment.getPatient().getAttribute("phoneNumber");
if (null == phoneNumber) {
log.info("Since no mobile number found for the patient. SMS not sent.");
Expand All @@ -136,7 +137,7 @@ public void sendAppointmentReminderSMS(Appointment appointment) {

@Transactional
@Override
public void sendAppointmentBookingSMS(Appointment appointment) {
public void sendAppointmentBookingSMS(Appointment appointment) throws UnknownHostException {
PersonAttribute phoneNumber = appointment.getPatient().getAttribute("phoneNumber");
if (null == phoneNumber) {
log.info("Since no mobile number found for the patient. SMS not sent.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openmrs.module.appointments.model.AppointmentProvider;
import org.openmrs.module.appointments.model.SMSRequest;

import java.net.UnknownHostException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -158,7 +159,7 @@ public String getFacilityName(Appointment appointment) {
}


public String sendSMS(String phoneNumber, String message) {
public String sendSMS(String phoneNumber, String message) throws UnknownHostException {
try {
SMSRequest smsRequest = new SMSRequest();
smsRequest.setPhoneNumber(phoneNumber);
Expand All @@ -179,9 +180,13 @@ public String sendSMS(String phoneNumber, String message) {
HttpResponse response = httpClient.execute(request);
httpClient.close();
return response.getStatusLine().getReasonPhrase();
} catch (Exception e) {
logger.error("Exception occured in sending sms ", e);
throw new RuntimeException("Exception occured in sending sms ", e);
}catch(UnknownHostException e) {
logger.error("SMS-service not found or host not known", e);
throw e;
}
catch (Exception e) {
logger.error("Exception occurred in sending sms ", e);
throw new RuntimeException("Exception occurred in sending sms ", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -66,7 +67,7 @@ public void executeShouldSendAppointmentRemindersWhenEnabled() throws Exception
}

@Test
public void executeShouldNotSendAppointmentRemindersWhenDisabled() {
public void executeShouldNotSendAppointmentRemindersWhenDisabled() throws UnknownHostException {
String globalPropertyName = "sms.enableAppointmentReminderSMSAlert";
globalProperty = new GlobalProperty(globalPropertyName, "false");
when(administrationService.getGlobalPropertyObject(globalPropertyName)).thenReturn(globalProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
Expand Down Expand Up @@ -198,7 +199,7 @@ public void shouldPublishTeleconsultationAppointmentSavedEvent() {
}

@Test
public void shouldSendAppointmentReminderSMS() {
public void shouldSendAppointmentReminderSMS() throws UnknownHostException {
Appointment appointment = new Appointment();
PatientIdentifier identifier = new PatientIdentifier();
identifier.setIdentifier("123456789");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.validation.Valid;
import java.io.IOException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
Expand Down Expand Up @@ -69,7 +70,8 @@ public List<AppointmentDefaultResponse> searchAppointments( @Valid @RequestBody

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Object> saveAppointment(@Valid @RequestBody AppointmentRequest appointmentRequest){
public ResponseEntity<Object> saveAppointment(@Valid @RequestBody AppointmentRequest appointmentRequest) {
Appointment appointment = null;
try {
/**
* The above code has been done so because to make appointment save within a transaction boundary.
Expand All @@ -78,11 +80,14 @@ public ResponseEntity<Object> saveAppointment(@Valid @RequestBody AppointmentReq
* start another transaction, and before that the dirty persistent entity will be flushed to DB.
* appointmentMapper should be fixed and validateAndSave() signature should be changed.
*/
Appointment appointment = appointmentsService.validateAndSave(() -> appointmentMapper.fromRequest(appointmentRequest));
appointment = appointmentsService.validateAndSave(() -> appointmentMapper.fromRequest(appointmentRequest));
AdministrationService administrationService = Context.getService(AdministrationService.class);
boolean bookSMS = Boolean.valueOf(administrationService.getGlobalPropertyObject("sms.enableAppointmentBookingSMSAlert").getPropertyValue());
if (bookSMS){
appointmentsService.sendAppointmentBookingSMS(appointment);}
if (bookSMS) {
appointmentsService.sendAppointmentBookingSMS(appointment);
}
return new ResponseEntity<>(appointmentMapper.constructResponse(appointment), HttpStatus.OK);
} catch (UnknownHostException e) {
return new ResponseEntity<>(appointmentMapper.constructResponse(appointment), HttpStatus.OK);
} catch (Exception e) {
log.error("Runtime error while trying to create new appointment", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -84,24 +85,29 @@ public class RecurringAppointmentsController extends BaseRestController {
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Object> save(@RequestBody RecurringAppointmentRequest recurringAppointmentRequest) {
AppointmentRecurringPattern appointmentRecurringPattern = null;
try {
RecurringPattern recurringPattern = recurringAppointmentRequest.getRecurringPattern();
validateRecurringPattern(recurringPattern);
AppointmentRecurringPattern appointmentRecurringPattern = recurringPatternMapper.fromRequest(recurringPattern);
appointmentRecurringPattern = recurringPatternMapper.fromRequest(recurringPattern);
List<Appointment> appointmentsList = recurringAppointmentsService.generateRecurringAppointments(recurringAppointmentRequest);
appointmentRecurringPattern.setAppointments(new LinkedHashSet<>(appointmentsList));
if (appointmentsList.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
appointmentRecurringPatternService.validateAndSave(appointmentRecurringPattern);
AdministrationService administrationService = Context.getService(AdministrationService.class);
boolean bookSMS = Boolean.valueOf(administrationService.getGlobalPropertyObject("sms.enableAppointmentBookingSMSAlert").getPropertyValue());
if (bookSMS){
appointmentsService.sendAppointmentBookingSMS(appointmentRecurringPattern.getAppointments().iterator().next());}
if (bookSMS) {
appointmentsService.sendAppointmentBookingSMS(appointmentRecurringPattern.getAppointments().iterator().next());
}
return new ResponseEntity<>(recurringAppointmentMapper.constructResponse(
new ArrayList<>(appointmentRecurringPattern.getAppointments())), HttpStatus.OK);
} catch (RuntimeException e) {
log.error("Runtime error while trying to create recurring appointments", e);
return new ResponseEntity<>(RestUtil.wrapErrorResponse(e, e.getMessage()), HttpStatus.BAD_REQUEST);
} catch (UnknownHostException e) {
return new ResponseEntity<>(recurringAppointmentMapper.constructResponse(
new ArrayList<>(appointmentRecurringPattern.getAppointments())), HttpStatus.OK);
}
}

Expand Down

0 comments on commit 026084b

Please sign in to comment.