-
Notifications
You must be signed in to change notification settings - Fork 1
/
send-reminders.ts
121 lines (108 loc) · 4.13 KB
/
send-reminders.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import fs from 'fs';
import moment from 'moment-timezone';
import path from 'path';
import { Twilio } from "twilio";
import _ from 'lodash';
import { initialize, NotificationDao, ReminderDao } from '../dao/mongoose';
import { getInstanceMethods } from '../types/i-instance-methods';
import logger from '../utils/logger';
const client = new Twilio(process.env.TWILIO_ACCOUNT_SID || '', process.env.TWILIO_AUTH_TOKEN || '');
(async () => {
try {
// connect to database
logger.debug('connecting to database');
await initialize();
logger.debug('connected');
// get a list of instances
const instanceDirectory = path.join(process.cwd(), './instances/');
const items = fs.readdirSync(instanceDirectory);
// go thru each item found in the directory
for(let instanceIndex = 0; instanceIndex < items.length; instanceIndex++) {
const instance = items[instanceIndex];
try {
// if the item is a directory then lets assume it's a valid instance
if (fs.lstatSync(path.join(instanceDirectory, instance)).isDirectory()) {
// get the case instance
const instanceMethods = await getInstanceMethods(instance);
// get the day after tomorrows start date & time to use as time bound
const startDate = moment().toDate();
const endDate = moment.tz(instanceMethods.getTimezone()).startOf('day').add(2, 'days').toDate();
logger.info(`Searching for dates between ${startDate} - ${endDate}`, { metadata: {
service: `send-reminders.ts`,
instance,
}});
// find all cases within the time bounds
const cases = await instanceMethods.findAll({
startDate,
endDate,
});
// add the test case for any reminders
cases.push(await instanceMethods.getTestCase(1));
// lets get a list of uids to query off
const uids = cases.map(o => o.uid);
logger.info(`Cases Found: ${uids}`, { metadata: {
service: `send-reminders.ts`,
instance,
}});
// find all reminders that match the dockets
const reminders = await ReminderDao.find({
active: true,
uid: {
$in: uids,
},
}).exec();
// go thru each reminder to check to see if it matches a case
// then send a text if it does
for(let i = 0; i < reminders.length; i++) {
const reminder = reminders[i];
try {
const c = _.find(cases, (o) => o.uid === reminder.uid);
if (c) {
// send the sms
const options = {
to: reminder.phone,
from: process.env.TWILIO_PHONE_NUMBER,
body: `Just a reminder that you have an appointment coming up on ${moment(c.date).tz(instanceMethods.getTimezone()).format('l LT')} @ ${c.address}. Case is ${c.number}`,
};
logger.info(JSON.stringify(options), { metadata: {
service: `send-reminders.ts`,
instance,
reminder: reminder.toJSON(),
case: c,
}});
await client.messages.create(options);
// set the reminder active to false
await reminder.updateOne({ active: false });
// add a notification entry
await NotificationDao.create({
uid: reminder.uid,
number: reminder.number,
phone: reminder.phone,
event_date: c.date,
});
}
}
catch(ex) {
logger.error(ex, { metadata: {
service: `send-reminders.ts`,
instance,
reminder: reminder.toJSON(),
}});
}
}
}
}
catch (ex) {
logger.error(ex, { metadata: {
service: `send-reminders.ts`,
instance,
}});
}
}
} catch (ex) {
logger.error(ex, { metadata: {
service: `send-reminders.ts`,
}});
}
process.exit();
})();