-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatchlist.js
73 lines (61 loc) · 1.9 KB
/
watchlist.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
const nodemailer = require('nodemailer');
let transporter = null;
let MYRMEYDB = null;
let SOC = null;
let intervalHandle = null;
function init(soc, db, auth, interval) {
let poolConfig = {
pool: true,
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth
}
let defaults = {
from: '"MyrmeyAssist" <[email protected]>'
}
SOC = soc;
MYRMEYDB = db;
transporter = nodemailer.createTransport(poolConfig, defaults);
intervalHandle = setInterval(checkWatchlist, 1000 * interval);
}
function stop(){
if(!intervalHandle){
return;
}
clearInterval(intervalHandle);
intervalHandle = null;
}
function checkWatchlist(){
let watchedCourses = undefined;
MYRMEYDB.getWatchedCourses()
.then((courses) => {
//If nothing is on our watchlist don't bother making a request to uci
if(courses.length === 0){ return Promise.resolve([]); }
watchedCourses = courses;
return SOC.searchSchedule({
CourseCodes: Object.keys(courses).join(',')
})
})
.then((searchResults) => {
searchResults.forEach((course) => {
course.offerings.forEach((offering) => {
console.log(`Checking Code: ${offering.Code}`);
if(offering.Status !== "FULL"){
notifyCourseOpen(watchedCourses[offering.Code],offering.Code);
MYRMEYDB.deleteWatch(offering.Code);
}
})
})
})
}
function notifyCourseOpen(emails, code) {
console.log(`Watchlist triggered for code:${code}`);
transporter.sendMail({
bcc: emails,
subject: 'Course Status Change',
html: `The following course code on your watchlist is no longer full: <b>${code}</b>! This course code has been removed from your watchlist.`
})
}
module.exports.init = init;
module.exports.stop = stop;