diff --git a/.github/counter.txt b/.github/counter.txt index 3f10ffe7a4..56a6051ca2 100644 --- a/.github/counter.txt +++ b/.github/counter.txt @@ -1 +1 @@ -15 \ No newline at end of file +1 \ No newline at end of file diff --git a/.github/last_date.txt b/.github/last_date.txt index 1135f30580..65b8ef1703 100644 --- a/.github/last_date.txt +++ b/.github/last_date.txt @@ -1 +1 @@ -20240727 \ No newline at end of file +20240728 diff --git a/README.md b/README.md index 65c31a69d3..6a7c20172f 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Developed using Java Enterprise Edition, the system offers both a web applicatio ## Current Version -Current Version: 3.0.0.20240727.15 (This line will be automatically updated to reflect the latest version) +Current Version: 3.0.0.20240728.1 (This line will be automatically updated to reflect the latest version) ## History diff --git a/src/main/java/com/divudi/ejb/SmsManagerEjb.java b/src/main/java/com/divudi/ejb/SmsManagerEjb.java index 9eb85c3d0f..796c389184 100644 --- a/src/main/java/com/divudi/ejb/SmsManagerEjb.java +++ b/src/main/java/com/divudi/ejb/SmsManagerEjb.java @@ -37,6 +37,7 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.time.Instant; +import java.util.ArrayList; import java.util.Base64; import java.util.Map; import java.util.logging.Level; @@ -58,7 +59,7 @@ public class SmsManagerEjb { UserPreferenceFacade userPreferenceFacade; @EJB SmsFacade smsFacade; - @EJB + @EJB private SessionInstanceFacade sessionInstanceFacade; @EJB private ChannelBean channelBean; @@ -67,14 +68,18 @@ public class SmsManagerEjb { ConfigOptionApplicationController configOptionApplicationController; @Inject private SessionController sessionController; - + // Schedule sendSmsToDoctorsBeforeSession to run every 30 minutes @SuppressWarnings("unused") @Schedule(second = "0", minute = "*/30", hour = "*", persistent = false) public void sendSmsToDoctorsBeforeSessionTimer() { - if(configOptionApplicationController.getBooleanValueByKey("Send SMS for Doctor Reminder")){ - //sendSmsToDoctorsBeforeSession(); - } + if (configOptionApplicationController.getBooleanValueByKey("Send SMS for Doctor Reminder")) { + if (configOptionApplicationController.getBooleanValueByKey("Check & Send SMS for Doctor Reminder")) { + sendSmsToDoctorsBeforeSessionWithChecks(); + } else { + sendSmsToDoctorsBeforeSession(); + } + } } @SuppressWarnings("unused") @@ -102,9 +107,52 @@ private void sendSmsAwaitingToSendInDatabase() { } } + private void sendSmsToDoctorsBeforeSessionWithChecks() { + // Define fromDate as the start of today + Date fromDate = CommonFunctions.getStartOfDay(); + + // Define toDate as the end of today + Date toDate = CommonFunctions.getEndOfDay(); + + // Fetch all session instances for today + List sessions = channelBean.listSessionInstances(fromDate, toDate, null, null, null); + + // Define fromTime as the current time + Date fromTime = new Date(); + Calendar calf = Calendar.getInstance(); + calf.setTime(fromTime); + calf.add(Calendar.MINUTE, 30); + Date fromTime1 = calf.getTime(); + + // Calculate toTime as 60 minutes from fromTime + Calendar cal = Calendar.getInstance(); + cal.setTime(fromTime); + cal.add(Calendar.MINUTE, 60); + Date toTime = cal.getTime(); + + // Filter sessions that start within the next 60 minutes + List upcomingSessions = sessions.stream() + .filter(session -> { + Date sessionStartDateTime = getSessionStartDateTime(session); // Combine session date and time + return sessionStartDateTime != null && sessionStartDateTime.after(fromTime1) && sessionStartDateTime.before(toTime); + }) + .collect(Collectors.toList()); + // Iterate over the filtered sessions and send SMS to doctor + for (SessionInstance s : upcomingSessions) { + if (s.getBookedPatientCount() != null) { + if (!s.isCancelled() && s.getBookedPatientCount() > 0) { + boolean isSmsSentBefore = checkSmsToDoctors(s); + if (!isSmsSentBefore) { + sendSmsToDoctors(s); + } + } + } + } + } + public void sendSmsToDoctorsBeforeSession() { - - // Define fromDate as the start of today + + // Define fromDate as the start of today Date fromDate = CommonFunctions.getStartOfDay(); // Define toDate as the end of today @@ -135,8 +183,8 @@ public void sendSmsToDoctorsBeforeSession() { .collect(Collectors.toList()); // Iterate over the filtered sessions and send SMS to doctors for (SessionInstance s : upcomingSessions) { - if(s.getBookedPatientCount() != null){ - if(!s.isCancelled() && s.getBookedPatientCount() > 0){ + if (s.getBookedPatientCount() != null) { + if (!s.isCancelled() && s.getBookedPatientCount() > 0) { sendSmsToDoctors(s); } } @@ -160,22 +208,54 @@ private Date getSessionStartDateTime(SessionInstance session) { private void sendSmsToDoctors(SessionInstance session) { Sms e = new Sms(); - e.setCreatedAt(new Date()); - if(session.getStaff().getPerson().getMobile() == null || session.getStaff().getPerson().getMobile().isEmpty()){ - e.setReceipientNumber(session.getStaff().getPerson().getPhone()); - }else{ - e.setReceipientNumber(session.getStaff().getPerson().getMobile()); - } - e.setSendingMessage(createDoctorRemiderSms(session)); - e.setPending(false); - e.setSmsType(MessageType.ChannelDoctorReminder); - getSmsFacade().create(e); - Boolean sent = sendSms(e); - e.setSentSuccessfully(sent); - getSmsFacade().edit(e); - + e.setCreatedAt(new Date()); + if (session.getStaff().getPerson().getMobile() == null || session.getStaff().getPerson().getMobile().isEmpty()) { + e.setReceipientNumber(session.getStaff().getPerson().getPhone()); + } else { + e.setReceipientNumber(session.getStaff().getPerson().getMobile()); + } + e.setSendingMessage(createDoctorRemiderSms(session)); + e.setPending(false); + e.setSmsType(MessageType.ChannelDoctorReminder); + getSmsFacade().create(e); + Boolean sent = sendSms(e); + e.setSentSuccessfully(sent); + getSmsFacade().edit(e); + } - + + private boolean checkSmsToDoctors(SessionInstance session) { + List sentSmsList = new ArrayList<>(); + Sms ec = new Sms(); + ec.setCreatedAt(new Date()); + if (session.getStaff().getPerson().getMobile() == null || session.getStaff().getPerson().getMobile().isEmpty()) { + ec.setReceipientNumber(session.getStaff().getPerson().getPhone()); + } else { + ec.setReceipientNumber(session.getStaff().getPerson().getMobile()); + } + ec.setSendingMessage(createDoctorRemiderSms(session)); + ec.setPending(false); + ec.setSmsType(MessageType.ChannelDoctorReminder); + + String jpql = "select s " + + "from Sms s " + + "where s.retired = :ret " + + "and s.sendingMessage = :sm " + + "and s.receipientNumber = :rn " + + "and s.smsType = :st " + + "and s.createdAt <= :bd " + + "order by s.id"; + Map params = new HashMap<>(); + params.put("ret", false); + params.put("sm", ec.getSendingMessage()); + params.put("st", ec.getSmsType()); + params.put("rn", ec.getReceipientNumber()); + params.put("bd", ec.getCreatedAt()); + + sentSmsList = smsFacade.findByJpql(jpql, params, TemporalType.TIMESTAMP); + return !sentSmsList.isEmpty(); + } + private String createDoctorRemiderSms(SessionInstance s) { String template = configOptionApplicationController.getLongTextValueByKey("Template for SMS sent on Doctor Reminder"); if (template == null || template.isEmpty()) { @@ -183,9 +263,9 @@ private String createDoctorRemiderSms(SessionInstance s) { } return createSmsForCDoctorRemider(s, template); } - + public String createSmsForCDoctorRemider(SessionInstance si, String template) { - if(si == null){ + if (si == null) { return ""; } String s; @@ -194,7 +274,7 @@ public String createSmsForCDoctorRemider(SessionInstance si, String template) { String doc = si.getStaff().getPerson().getNameWithTitle(); String booked = si.getBookedPatientCount().toString(); String paid = si.getPaidPatientCount().toString(); - + String insName = si.getInstitution().getName(); s = template.replace("{doctor}", doc) diff --git a/src/main/resources/VERSION.txt b/src/main/resources/VERSION.txt index a24670c905..ab3dc5d2da 100644 --- a/src/main/resources/VERSION.txt +++ b/src/main/resources/VERSION.txt @@ -1 +1 @@ -3.0.0.20240727.15 \ No newline at end of file +3.0.0.20240728.1 \ No newline at end of file