-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcron_server.js
90 lines (81 loc) · 2.39 KB
/
cron_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const cron = require("node-cron");
const moment = require("moment");
const mysql = require("./handlers/mysql/mysql");
const { sendWebPushNotification } = require("./handlers/helper");
const cronString = "*/1 * * * *";
const SQL_DATE_FORMAT = "YYYY-MM-DD HH:mm:ss";
const isValid = cron.validate(cronString);
const getQuery = (startDate, endDate) => {
return `SELECT
a.slotDate,
a.slotJsDate,
a.slotDuration,
a.slotDurationUnit,
a.slotTime,
a.speciality,
a.userUuid,
a.drName,
a.visitUuid,
a.patientId,
a.patientName,
a.openMrsId,
a.status,
s.notification_object as webpush_obj,
s.locale as locale
FROM
appointments a
INNER JOIN pushnotification s ON a.userUuid = s.user_uuid
WHERE
a.slotJsDate BETWEEN '${startDate}'
AND '${endDate}'
AND a.status = 'booked';`;
};
const queryAndSendNotification = async (query) => {
new Promise((resolve, reject) => {
mysql.query(query, (err, results) => {
if (err) {
reject(err.message);
}
resolve(results);
});
}).then((data) => {
for (let i = 0; i < data.length; i++) {
const schedule = data[i];
if (schedule.webpush_obj) {
const engTitle = `Appointment Reminder(${schedule.slotTime}): ${schedule.patientName}`;
const ruTitle = `Напоминание о встрече(${schedule.slotTime}): ${schedule.patientName}`;
const title = schedule.locale === "ru" ? ruTitle : engTitle;
sendWebPushNotification({
webpush_obj: schedule.webpush_obj,
title,
body: schedule.openMrsId,
});
}
}
});
};
const sendAppointmentNotification1min = async () => {
// trigger 1 mins before
const query = getQuery(
moment.utc().add(1, "second").format(SQL_DATE_FORMAT),
moment.utc().add(1, "minute").format(SQL_DATE_FORMAT)
);
queryAndSendNotification(query);
};
const sendAppointmentNotification15min = async () => {
// trigger 15 mins before
const earlyNotificationQuery = getQuery(
moment.utc().add(14, "minutes").add(1, "second").format(SQL_DATE_FORMAT),
moment.utc().add(15, "minutes").format(SQL_DATE_FORMAT)
);
queryAndSendNotification(earlyNotificationQuery);
};
cron.schedule(cronString, sendAppointmentNotification1min, {
scheduled: true,
});
cron.schedule(cronString, sendAppointmentNotification15min, {
scheduled: true,
});
process.on("uncaughtException", function (err) {
throw err;
});