From f844395d0290162104c0157edacd9cf148185527 Mon Sep 17 00:00:00 2001 From: Dominic Farolino Date: Wed, 28 Feb 2018 21:02:16 -0500 Subject: [PATCH] Add API support for sending minor information email --- lib/api/email.js | 97 ++++++++++++++++++++++++++++++++++++++++ lib/models/registrant.js | 8 ++++ package.json | 2 +- 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/lib/api/email.js b/lib/api/email.js index 426cd19..1863a49 100644 --- a/lib/api/email.js +++ b/lib/api/email.js @@ -169,6 +169,71 @@ routes.get('/sendInfoEmail1', apiKeyValidator, eventDeadlineValidator, (request, }); }); +/** + * This is used to send the infoEmailMinors email to all registrants that: + * + * - Are under 18 + * - Have not received this email before + * - Have confirmed their attendance for the event + * + * It is intended to be called manually, or from the dashboard if that + * functionality gets implemented. + * + * usage: /sendInfoEmailMinors?key= + * + */ +routes.get('/sendInfoEmailMinors', apiKeyValidator, eventDeadlineValidator, (request, response) => { + Registrant.find({attendanceConfirmed: "YES", receivedMinorInfoEmail: false}, (err, registrants) => { + if (err) { + console.error(err); + return response.status(400).send('Sorry, something went wrong'); + } + + // We'll do a little preprocessing here to filter out the non-minor registrants + let eighteenYearsUnix = 567993600; + registrants = registrants.filter(registrant => { + let dob = Date.parse(registrant.dob) / 1000; + let now = Date.now() / 1000; + return now - dob < eighteenYearsUnix; + }); + + for (registrant of registrants) { + Registrant.update({email: registrant.email}, {receivedMinorInfoEmail: true}, (err, numberAffected, rawResponse) => {}); + } + + sendInfoEmailMinors(request, registrants); + response.json({"affected users": registrants.length}); + }); // Registrant.find +}); + +/** + * This is used to send the infoEmail3 email to all registrants that: + * + * - Have not received this email before + * - Have confirmed their attendance for the event + * + * It is intended to be called manually, or from the dashboard if that + * functionality gets implemented. + * + * usage: /infoEmail3?key= + * + */ +routes.get('/sendInfoEmail3', apiKeyValidator, eventDeadlineValidator, (request, response) => { + Registrant.find({attendanceConfirmed: "YES", receivedInfoEmail3: false}, (err, registrants) => { + if (err) { + console.error(err); + return response.status(400).send('Sorry, something went wrong'); + } + + for (registrant of registrants) { + Registrant.update({email: registrant.email}, {receivedInfoEmail3: true}, (err, numberAffected, rawResponse) => {}); + } + + sendInfoEmail3(request, registrants); + response.json({"affected users": registrants.length}); + }); // Registrant.find +}); + /* *********************************************************** * Below are helper functions that some of the above @@ -233,6 +298,38 @@ async function sendInfoEmail1(request, registrants) { } } +async function sendInfoEmailMinors(request, registrants) { + for (let registrant of registrants) { + // Build user emailData + let emailData = { + subject: "Minors at RevolutionUC - Important Information for the Event", + shortDescription: "Because you're under 18, there is some extra information you need to know to get ready.", + firstName: registrant.firstName + }; + + // One async task + // 1. Send infoEmailMinors + const html = await build('infoEmailMinors', emailData); + await send(mailgunApiKey, mailgunDomain, 'RevolutionUC ', registrant.email, emailData.subject, html); + } +} + +async function sendInfoEmail3(request, registrants) { + for (let registrant of registrants) { + // Build user emailData + let emailData = { + subject: "Important Information for RevolutionUC!", + shortDescription: "Here's some important information you need to know to get ready for this weekend!", + firstName: registrant.firstName + }; + + // One async task + // 1. Send infoEmail3 + const html = await build('infoEmail3', emailData); + await send(mailgunApiKey, mailgunDomain, 'RevolutionUC ', registrant.email, emailData.subject, html); + } +} + async function sendEmailVerification(request, registrants) { for (let registrant of registrants) { // Build user emailData diff --git a/lib/models/registrant.js b/lib/models/registrant.js index 4ee9034..527a5cb 100644 --- a/lib/models/registrant.js +++ b/lib/models/registrant.js @@ -101,6 +101,14 @@ const registrantScheme = mongoose.Schema({ otherDietaryRestrictions: { type: String, default: '' + }, + receivedInfoEmail3: { + type: Boolean, + default: false + }, + receivedMinorInfoEmail: { + type: Boolean, + default: false } }); diff --git a/package.json b/package.json index dba2125..99dc657 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "mongoose": "^4.7.7", "morgan": "^1.7.0", "multer": "^1.3.0", - "revolutionuc-emails": "github:revolutionuc/revolutionuc-emails#v2.2.0" + "revolutionuc-emails": "github:revolutionuc/revolutionuc-emails#v2.3.0" }, "devDependencies": { "chai": "^3.5.0",