This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoffice_directors_email.js
231 lines (181 loc) · 7.85 KB
/
office_directors_email.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// heroku scheduler script for automated email to office directors
// heroku scheduler every friday
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectId;
var moment = require('moment');
moment().format();
const sgClient = require('@sendgrid/mail');
sgClient.setApiKey(process.env.SENDGRID_API_KEY);
var sender = process.env.SENDGRID_EMAIL;
var url = process.env.MONGO_URL;
const fs = require('fs');
var content = fs.readFileSync("./server/assets/email_templates/officeDirectorsReport.html").toString("utf-8");
let directors = {
"Aberdeen": "[email protected]",
"Albuquerque": "[email protected]",
"Atlanta": "[email protected]",
"Austin": "[email protected]",
"Beijing":"[email protected]",
"Boston": ["[email protected]","[email protected]"],
"Bristol": "[email protected]",
"Chicago": "[email protected]",
"Copenhagen": "[email protected]",
"Dallas": "[email protected]",
"Denver": "[email protected]",
"Dubai": "[email protected]",
"Edinburgh": "[email protected]",
"Fort Lauderdale": ["[email protected]","[email protected]"],
"Halifax": ["[email protected]","[email protected]"],
"Ho Chi Minh City": "[email protected]",
"Hong Kong": "[email protected]",
"Houston": ["[email protected]", "[email protected]"],
"Kansas City": "[email protected]",
"London": "[email protected]",
"Los Angeles": "[email protected]",
"Miami": ["[email protected]","[email protected]"],
"Milwaukee": "[email protected]",
"Mississauga": ["[email protected]","[email protected]"],
"Moscow": "[email protected]",
"Mumbai": "[email protected]",
"New York": ["[email protected]", "[email protected]"],
"Newark": "[email protected]",
"Ottawa": ["[email protected]","[email protected]"],
"Perth": "[email protected]",
"Philadelphia": "[email protected]",
"Phoenix": "[email protected]",
"Portland": ["[email protected]","[email protected]"],
"Romsey": "[email protected]",
"San Diego": "[email protected]",
'San Francisco': ["[email protected]","[email protected]"],
"Santa Clara": "[email protected]",
"Seattle": "[email protected]",
"Tampa": "[email protected]",
"Toronto": "[email protected]",
"Warrington": "[email protected]",
'Washington': "[email protected]",
"West Hartford": "[email protected]",
'Wellington': "[email protected]",
"Shanghai": "[email protected]",
"Sydney": "[email protected]"
}
//"Name,Email\r\n";
function nodeToCsv(node) {
return `${node.name},${node.email}\r\n`;
}
let allUsers = [];
MongoClient.connect(url, {
useUnifiedTopology: true
}).then(function (db) {
console.log("CONNECTED TO DB");
getUsers(db).then(function () {
//console.log("allUsers", allUsers);
Object.keys(directors).forEach((key, index) => {
// three attachment
// 1. employees who have signed up for the app,
// 2. employees who have signed up for the app but haven't reported their health status for more than 7 days,
// 3. the number of app users in your office with a health status of Red and/or Orange.
console.log(key); // office
// 1. employees who have signed up for the app,
const usersbyOffice = allUsers.filter(u => u.location === key);
console.log("usersbyOffice", usersbyOffice.length);
if(usersbyOffice.length === 0 ) return;
let csvHeader = "Name,Email\r\n";
let csv = csvHeader;
usersbyOffice.forEach((user) => {
csv += nodeToCsv(user)
})
let attachment = Buffer.from(csv).toString('base64');
// 2. employees who have signed up for the app but haven't reported their health status for more than 7 days,
var checkDate = new Date();
var pastDate = checkDate.getDate() - 7;
checkDate.setDate(pastDate);
const usersneedsUpdate = usersbyOffice.filter(u => u.status.date < checkDate);
let csv2 = csvHeader;
usersneedsUpdate.forEach((user) => {
csv2 += nodeToCsv(user)
})
let attachment2 = Buffer.from(csv2).toString('base64');
// 3. the number of app users in your office with a health status of Red and/or Orange.
const usersStatusOrange = usersbyOffice.filter(u => u.status.status === 1);
const usersStatusRed = usersbyOffice.filter(u => u.status.status === 2);
let csv3 = "Orange,Red,Total App Signups in Office\r\n";
var numberOfOrange = usersStatusOrange.length;
var numberOfRed = usersStatusRed.length;
var total = usersbyOffice.length;
csv3 += `${numberOfOrange},${numberOfRed},${total}\r\n`
let attachment3 = Buffer.from(csv3).toString('base64');
sendEmail(directors[key], key, attachment, attachment2, attachment3);
});
})
}).catch(err => {
console.error("An error occurred reading the database.");
console.error(err);
});
function getUsers(client_db) {
return new Promise((resolve, reject) => {
let db = client_db.db();
let include = {
"_id": 1,
// "dateOfConsent": 1,
"name": 1,
"location": 1
}
let collection = db.collection('users');
let statusCollection = db.collection('status');
collection.find({}, include).toArray(function (err, users) {
var counter = 0;
for (let u of users) {
statusCollection.find({
"user": u._id
})
.sort({
date: -1
})
.limit(1).toArray(function (error, st) {
u.status = st[0];
allUsers.push(u);
isDone();
});
}
function isDone() {
counter += 1;
if (users.length === counter) {
resolve(true);
}
}
});
});
}
function sendEmail(toEmail, location, attachment, attachment2, attachment3) {
const mailOptions = {
to: toEmail,
from: sender,
subject: "Healthy Reentry – Weekly Report for " + location,
html: content
};
if (attachment) {
mailOptions.attachments = [{
"content": attachment,
"filename": "Employees Who Signed up.csv",
"type": "text/csv"
},
{
"content": attachment2,
"filename": "Employees Who Have Not Updated in 7 Days.csv",
"type": "text/csv"
},
{
"content": attachment3,
"filename": "Breakdown by Health Status.csv",
"type": "text/csv"
}
]
}
sgClient.send(mailOptions, function (err) {
console.log("err?", err)
if (err)
return;
else
console.log('sent');
});
}