-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.ts
173 lines (146 loc) · 4.08 KB
/
checker.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { parse } from 'node-html-parser';
import nodemailer from 'nodemailer';
import schedule from 'node-schedule';
import DbUtil, { User } from './db.js';
import dotenv from 'dotenv';
import twilio from 'twilio';
import fetch from 'node-fetch';
dotenv.config();
type Config = {
lastCheck: Date;
lastStatus: string;
checking: boolean;
};
export default class Checker {
config: Config;
constructor(runScheduler = true) {
this.config = {
lastCheck: new Date(),
lastStatus: 'NULL',
checking: false
};
if (runScheduler) {
//check every 15 minutes
schedule.scheduleJob('*/15 * * * *', () => {
this.fetchStatus();
});
}
}
public async fetchStatus() {
console.log('fetching status HTML page');
if (this.config.checking) {
console.log('checking in process, skipping');
return;
}
this.config.checking = true;
try {
const response = await fetch(process.env.TRAIL_STATUS_URL);
const body = await response.text();
console.log('fetch complete, parsing...');
const root = parse(body);
const tableElem = root.querySelector('table').parentNode;
let trailStatus = tableElem.rawText.toLowerCase().indexOf('open') === -1 ? 'Closed' : 'Open';
console.log(`processing complete, trail status: ${trailStatus}`);
if (trailStatus != '') {
if (trailStatus != this.config.lastStatus && this.config.lastStatus != 'NULL') {
await this.sendNotifications(trailStatus);
}
this.config.lastStatus = trailStatus;
this.config.lastCheck = new Date();
}
} catch (e) {
console.log(e);
} finally {
this.config.checking = false;
}
}
async sendNotifications(trailStatus: string) {
const dbUtil = new DbUtil();
const users = await dbUtil.fetchUsers();
console.log('sending notifications');
const subject = 'Bidwell Trail Status Update';
const text = `Trail status changed to: ${trailStatus}`;
try {
const smsClient = twilio(process.env.SMS_SID, process.env.SMS_TOKEN);
const transporter = this.getTransporter(true);
for (const user of users) {
console.log(`sending notification to: ${user.email}`);
if (user.skipMail) {
console.log('skipMail=true');
} else {
try {
const info = await transporter.sendMail({
from: process.env.SMTP_FROM_EMAIL,
to: user.email,
subject,
text
});
console.log(`Message sent: ${info.messageId}`);
} catch (e) {
console.log(e);
}
}
if (user.phone) {
console.log(`${user.email} has phone ${user.phone}, sending sms`);
try {
const message = await smsClient.messages.create({
body: `${subject}. ${text}`,
from: `+1${process.env.SMS_FROM}`,
to: `+1${user.phone}`
});
console.log(message.sid);
} catch (e) {
console.log(e);
}
}
// rate limit
await this.pause();
}
} catch (e) {
console.log('error sending emails', e);
}
}
currentStatus() {
return {
trailStatus: this.config.lastStatus,
lastCheck: this.config.lastCheck
};
}
getTransporter(pool = false) {
return nodemailer.createTransport({
service: 'Gmail',
pool,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PWD
}
});
}
sendNewUserNotification(email: string, phone: string, existingUser: User) {
this.getTransporter().sendMail({
from: process.env.SMTP_FROM_EMAIL,
to: process.env.SMTP_ADMIN_EMAIL,
subject: existingUser ? 'User updated' : 'New user registered',
text: `user details: email=${email}, phone=${phone}`
});
}
sendUserUnsubNotification(email: string) {
this.getTransporter().sendMail({
from: process.env.SMTP_FROM_EMAIL,
to: process.env.SMTP_ADMIN_EMAIL,
subject: 'User unsubscribed',
text: `user: ${email}`
});
}
sendRegEmail(email: string) {
this.getTransporter().sendMail({
from: process.env.SMTP_FROM_EMAIL,
to: email,
subject: 'Trail Checker Registration',
text: `Thank you for registering! The current trail status is ${this.config.lastStatus}. We'll send you an update when that changes!`
});
}
async pause() {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
}