Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Add API support for sending minor information email
Browse files Browse the repository at this point in the history
  • Loading branch information
domfarolino committed Mar 1, 2018
1 parent 7cf4ed4 commit f844395
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
97 changes: 97 additions & 0 deletions lib/api/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -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=<apikeyhere>
*
*/
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=<apikeyhere>
*
*/
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
Expand Down Expand Up @@ -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 <[email protected]>', 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 <[email protected]>', registrant.email, emailData.subject, html);
}
}

async function sendEmailVerification(request, registrants) {
for (let registrant of registrants) {
// Build user emailData
Expand Down
8 changes: 8 additions & 0 deletions lib/models/registrant.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ const registrantScheme = mongoose.Schema({
otherDietaryRestrictions: {
type: String,
default: ''
},
receivedInfoEmail3: {
type: Boolean,
default: false
},
receivedMinorInfoEmail: {
type: Boolean,
default: false
}
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f844395

Please sign in to comment.