-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathemail.js
421 lines (365 loc) · 15.7 KB
/
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
const { URL } = require('url');
const moment = require('moment');
// eslint-disable-next-line import/no-unresolved
const asyncBatch = require('async-batch').default;
const fileSystem = require('fs');
const path = require('path');
const mustache = require('mustache');
const emailService = require('./email/service-email');
const db = require('../db');
const { notificationType } = require('./email/constants');
const expiryMinutes = 30;
const ASYNC_REPORT_TYPES = {
audit: 'audit',
treasury: 'treasury',
};
const HELPDESK_EMAIL = '[email protected]';
async function deliverEmail({
toAddress,
ccAddress,
emailHTML,
emailPlain,
subject,
}) {
return emailService.getTransport().sendEmail({
toAddress,
ccAddress,
subject,
body: emailHTML,
text: emailPlain,
});
}
function addBaseBranding(emailHTML, brandDetails) {
const { tool_name, title, notifications_url } = brandDetails;
const baseBrandedTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/base.html'));
const brandedHTML = mustache.render(baseBrandedTemplate.toString(), {
tool_name,
title,
webview_available: false, // Preheader and webview are not setup for Grant notification email.
// preheader: 'Test preheader',
// webview_url: 'http://localhost:8080',
usdr_url: 'http://usdigitalresponse.org',
usdr_logo_url: 'https://grants.usdigitalresponse.org/usdr_logo_transparent.png',
notifications_url,
}, {
email_body: emailHTML,
});
return brandedHTML;
}
function sendPassCode(email, passcode, httpOrigin, redirectTo) {
if (!httpOrigin) {
throw new Error('must specify httpOrigin in sendPassCode');
}
const url = new URL(`${httpOrigin}/api/sessions`);
url.searchParams.set('passcode', passcode);
if (redirectTo) {
url.searchParams.set('redirect_to', redirectTo);
}
const href = url.toString();
const formattedBodyTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_formatted_body.html'));
const formattedBody = mustache.render(formattedBodyTemplate.toString(), {
body_title: 'Login Passcode',
body_detail: `<p>Your link to access USDR's Grants Tool is <a href=${href}>${href}</a>.
It expires in ${expiryMinutes} minutes</p>`,
});
const emailHTML = module.exports.addBaseBranding(
formattedBody,
{
tool_name: href.includes('reporter') ? 'Grants Reporter Tool' : 'Grants Identification Tool',
title: 'Login Passcode',
},
);
if (process.env.DEV_LOGIN_LINK && process.env.NODE_ENV === 'development') {
const BLUE = '\x1b[34m';
const message = `| Login link generated: ${href} |`;
console.log(`${BLUE}${'-'.repeat(message.length)}`);
console.log(`${BLUE}${message}`);
console.log(`${BLUE}${'-'.repeat(message.length)}`);
}
return module.exports.deliverEmail({
toAddress: email,
emailHTML,
emailPlain: `Your link to access USDR's Grants tool is ${href}. It expires in ${expiryMinutes} minutes`,
subject: 'USDR Grants Tool Access Link',
});
}
async function sendReportErrorEmail(user, reportType) {
const body = `There was an error generating a your requested ${reportType} Report. `
+ 'Someone from USDR will reach out within 24 hours to debug the problem. '
+ 'We apologize for any inconvenience.';
const subject = `${reportType} Report generation has failed for ${user.tenant.display_name}`;
const formattedBodyTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_formatted_body.html'));
const formattedBody = mustache.render(formattedBodyTemplate.toString(), {
body_title: subject,
body_detail: body,
});
const emailHTML = module.exports.addBaseBranding(
formattedBody,
{
tool_name: 'Grants Reporter Tool',
title: subject,
},
);
return module.exports.deliverEmail({
toAddress: user.email,
ccAddress: HELPDESK_EMAIL,
emailHTML,
emailPlain: body,
subject,
});
}
function sendWelcomeEmail(email, httpOrigin) {
if (!httpOrigin) {
throw new Error('must specify httpOrigin in sendWelcomeEmail');
}
const formattedBodyTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_formatted_body.html'));
const formattedBody = mustache.render(formattedBodyTemplate.toString(), {
body_title: 'Welcome!',
body_detail: `<p>Visit USDR's Grants Tool at:
<a href="${httpOrigin}">${httpOrigin}</a>.`,
});
const emailHTML = module.exports.addBaseBranding(
formattedBody,
{
tool_name: httpOrigin.includes('reporter') ? 'Grants Reporter Tool' : 'Grants Identification Tool',
title: 'Welcome to the USDR Grants tool',
},
);
return module.exports.deliverEmail({
toAddress: email,
emailHTML,
emailPlain: `Visit USDR's Grants Tool at: ${httpOrigin}.`,
subject: 'Welcome to USDR Grants Tool',
});
}
function getGrantDetail(grant, emailNotificationType) {
const grantDetailTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_grant_detail.html'));
const description = grant.description.substring(0, 380).replace(/(<([^>]+)>)/ig, '');
const grantsUrl = new URL(process.env.WEBSITE_DOMAIN);
if (emailNotificationType === notificationType.grantDigest) {
grantsUrl.pathname = 'grants';
} else {
grantsUrl.pathname = 'my-grants';
}
grantsUrl.searchParams.set('utm_source', 'subscription');
grantsUrl.searchParams.set('utm_medium', 'email');
grantsUrl.searchParams.set('utm_campaign', emailNotificationType);
const grantDetail = mustache.render(
grantDetailTemplate.toString(), {
title: grant.title,
description,
status: grant.opportunity_status,
show_date_range: grant.open_date && grant.close_date,
open_date: grant.open_date ? new Date(grant.open_date).toLocaleDateString('en-US', { timeZone: 'UTC' }) : undefined,
close_date: grant.close_date ? new Date(grant.close_date).toLocaleDateString('en-US', { timeZone: 'UTC' }) : undefined,
award_floor: grant.award_floor || '$0',
award_ceiling: grant.award_ceiling || 'Not available',
// estimated_funding: grant.estimated_funding, TODO: add once field is available in the database.
cost_sharing: grant.cost_sharing,
link_url: `https://www.grants.gov/search-results-detail/${grant.grant_id}`,
grants_url: grantsUrl.toString(),
view_grant_label: emailNotificationType === notificationType.grantDigest ? undefined : 'View My Grants',
},
);
return grantDetail;
}
async function buildGrantDetail(grantId, emailNotificationType) {
// Add try catch here.
const grant = await db.getGrant({ grantId });
const grantDetail = module.exports.getGrantDetail(grant, emailNotificationType);
return grantDetail;
}
async function sendGrantAssignedNotficationForAgency(assignee_agency, grantDetail, assignorUserId) {
const grantAssignedBodyTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_grant_assigned_body.html'));
const assignor = await db.getUser(assignorUserId);
const grantAssignedBody = mustache.render(grantAssignedBodyTemplate.toString(), {
assignor_name: assignor.name,
assignor_agency_name: assignor.agency.name,
assignee_agency_name: assignee_agency.name,
}, {
grant_detail: grantDetail,
});
const baseUrl = new URL(process.env.WEBSITE_DOMAIN);
baseUrl.pathname = 'my-grants';
baseUrl.searchParams.set('utm_source', 'subscription');
baseUrl.searchParams.set('utm_medium', 'email');
baseUrl.searchParams.set('utm_campaign', 'GRANT_ASSIGNMENT');
const emailHTML = module.exports.addBaseBranding(grantAssignedBody, {
tool_name: 'Grants Identification Tool',
title: 'Grants Assigned Notification',
notifications_url: baseUrl.toString(),
});
// TODO: add plain text version of the email
const emailPlain = emailHTML.replace(/<[^>]+>/g, '');
const emailSubject = `Grant Assigned to ${assignee_agency.name}`;
const assginees = await db.getSubscribersForNotification(assignee_agency.id, notificationType.grantAssignment);
const inputs = [];
assginees.forEach((assignee) => inputs.push(
{
toAddress: assignee.email,
emailHTML,
emailPlain,
subject: emailSubject,
},
));
asyncBatch(inputs, module.exports.deliverEmail, 2);
}
async function sendGrantAssignedEmail({ grantId, agencyIds, userId }) {
/*
1. Build the grant detail template
2. For each agency
2a. Build the grant_assigned_body
2b. For each user part of the agency
i. Send email
*/
const grantDetail = await buildGrantDetail(grantId, notificationType.grantAssignment);
const agencies = await db.getAgenciesByIds(agencyIds);
agencies.forEach((agency) => module.exports.sendGrantAssignedNotficationForAgency(agency, grantDetail, userId));
}
async function buildDigestBody({ name, openDate, matchedGrants }) {
const grantDetails = [];
matchedGrants.slice(0, 30).forEach((grant) => grantDetails.push(module.exports.getGrantDetail(grant, notificationType.grantDigest)));
const formattedBodyTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_formatted_body.html'));
const contentSpacerTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_content_spacer.html'));
const contentSpacerStr = contentSpacerTemplate.toString();
let additionalBody = grantDetails.join(contentSpacerStr).concat(contentSpacerStr);
const additionalButtonTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_additional_grants_button.html'));
additionalBody += mustache.render(additionalButtonTemplate.toString(), { additional_grants_url: `${process.env.WEBSITE_DOMAIN}/grants` });
const formattedBody = mustache.render(formattedBodyTemplate.toString(), {
body_title: `${name} - ${matchedGrants.length} NEW GRANTS`,
body_detail: moment(openDate).format('MMMM Do YYYY'),
additional_body: additionalBody,
});
return formattedBody;
}
async function sendGrantDigest({
name, matchedGrants, recipients, openDate,
}) {
console.log(`${name} is subscribed for notifications on ${openDate}`);
if (!matchedGrants || matchedGrants?.length === 0) {
console.error(`There were no grants available for ${name}`);
return;
}
if (!recipients || recipients?.length === 0) {
console.error(`There were no email recipients available for ${name}`);
return;
}
const formattedBody = await buildDigestBody({ name, openDate, matchedGrants });
const emailHTML = module.exports.addBaseBranding(formattedBody, {
tool_name: 'Federal Grant Finder',
title: 'New Grants Digest',
notifications_url: (process.env.ENABLE_MY_PROFILE === 'true') ? `${process.env.WEBSITE_DOMAIN}/my-profile` : `${process.env.WEBSITE_DOMAIN}/grants?manageSettings=true`,
});
// TODO: add plain text version of the email
const emailPlain = emailHTML.replace(/<[^>]+>/g, '');
const inputs = [];
recipients.forEach(
(recipient) => inputs.push(
{
toAddress: recipient.trim(),
emailHTML,
emailPlain,
subject: `New Grants published for ${name}`,
},
),
);
asyncBatch(inputs, module.exports.deliverEmail, 2);
}
async function getAndSendGrantForSavedSearch({
userSavedSearch, openDate,
}) {
const criteriaObj = db.formatSearchCriteriaToQueryFilters(userSavedSearch.criteria);
// NOTE: can't pass this as a separate parameter as it exceeds the complexity limit of 5
criteriaObj.openDate = openDate;
// Only 30 grants are shown on any given email and 31 will trigger a place to click to see more
const response = await db.getGrantsNew(
criteriaObj,
await db.buildPaginationParams({ currentPage: 1, perPage: 31 }),
{},
userSavedSearch.tenantId,
false,
);
return sendGrantDigest({
name: userSavedSearch.name,
matchedGrants: response.data,
recipients: [userSavedSearch.email],
openDate,
});
}
async function buildAndSendUserSavedSearchGrantDigest(userId, openDate) {
if (!openDate) {
openDate = moment().subtract(1, 'day').format('YYYY-MM-DD');
}
console.log(`Building and sending Grants Digest email for user: ${userId} on ${openDate}`);
/*
1. get all saved searches mapped to each user
2. call getAndSendGrantForSavedSearch to find new grants and send the digest
*/
const userSavedSearches = await db.getAllUserSavedSearches(userId);
const inputs = [];
userSavedSearches.forEach((userSavedSearch) => {
inputs.push({
userSavedSearch,
openDate,
});
});
await asyncBatch(inputs, getAndSendGrantForSavedSearch, 2);
console.log(`Successfully built and sent grants digest emails for ${inputs.length} saved searches on ${openDate}`);
}
async function buildAndSendGrantDigest() {
const openDate = moment().subtract(1, 'day').format('YYYY-MM-DD');
console.log(`Building and sending Grants Digest email for all agencies on ${openDate}`);
/*
1. get all agencies with notificaiton turned on (temporarily get all agencies with a custom keyword)
2. for each agency
call sendGrantDigest
*/
const agencies = await db.getAgenciesSubscribedToDigest(openDate);
const inputs = [];
agencies.forEach((agency) => inputs.push({
name: agency.name,
matchedGrants: agency.matched_grants,
recipients: agency.recipients,
openDate,
}));
await asyncBatch(inputs, module.exports.sendGrantDigest, 2);
console.log(`Successfully built and sent grants digest emails for ${openDate}`);
}
async function sendAsyncReportEmail(recipient, signedUrl, reportType) {
const formattedBodyTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_formatted_body.html'));
const formattedBody = mustache.render(formattedBodyTemplate.toString(), {
body_title: `Your ${reportType} report is ready for download`,
body_detail: `<p><a href=${signedUrl}><b>Click here</b></a> to download your file<br>Or, paste this link into your browser:<br><b>${signedUrl}</b><br><br>This link will remain active for 7 days.</p>`,
});
const emailHTML = module.exports.addBaseBranding(
formattedBody,
{
tool_name: 'Grants Reporter Tool',
title: `Your ${reportType} report is ready for download`,
},
);
return module.exports.deliverEmail({
toAddress: recipient,
emailHTML,
emailPlain: `Your ${reportType} report is ready for download. Paste this link into your browser to download it: ${signedUrl} This link will remain active for 7 days.`,
subject: `Your ${reportType} report is ready for download`,
});
}
module.exports = {
sendPassCode,
sendWelcomeEmail,
sendReportErrorEmail,
sendGrantAssignedEmail,
deliverEmail,
buildGrantDetail,
sendGrantAssignedNotficationForAgency,
buildAndSendUserSavedSearchGrantDigest,
buildAndSendGrantDigest,
getAndSendGrantForSavedSearch,
sendGrantDigest,
getGrantDetail,
addBaseBranding,
buildDigestBody,
sendAsyncReportEmail,
ASYNC_REPORT_TYPES,
};