Skip to content

Commit

Permalink
Merge branch 'master' into Issue#6198
Browse files Browse the repository at this point in the history
  • Loading branch information
DamithDeshan authored Jul 28, 2024
2 parents f497f85 + 3a6dfaa commit fffbfe2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/counter.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15
1
2 changes: 1 addition & 1 deletion .github/last_date.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20240727
20240728
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
134 changes: 107 additions & 27 deletions src/main/java/com/divudi/ejb/SmsManagerEjb.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,7 +59,7 @@ public class SmsManagerEjb {
UserPreferenceFacade userPreferenceFacade;
@EJB
SmsFacade smsFacade;
@EJB
@EJB
private SessionInstanceFacade sessionInstanceFacade;
@EJB
private ChannelBean channelBean;
Expand All @@ -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")
Expand Down Expand Up @@ -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<SessionInstance> 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<SessionInstance> 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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -160,32 +208,64 @@ 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<Sms> 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<String, Object> 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()) {
template = "Dear {doctor}, Your consultation session on today {appointment_date} at {appointment_time} has {booked} appointments ( {paid} paid). {ins_name}";
}
return createSmsForCDoctorRemider(s, template);
}

public String createSmsForCDoctorRemider(SessionInstance si, String template) {
if(si == null){
if (si == null) {
return "";
}
String s;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0.20240727.15
3.0.0.20240728.1

0 comments on commit fffbfe2

Please sign in to comment.