Skip to content

Commit

Permalink
Added reminder emails cron job.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGilleran committed Jul 3, 2017
1 parent 7adce37 commit 948af16
Show file tree
Hide file tree
Showing 23 changed files with 1,151 additions and 297 deletions.
2 changes: 2 additions & 0 deletions common/questionnaires.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ module.exports = [
introduction: "INTRODUCTION FOR CDC MATERIAL TO GO HERE",
detail_link: "DETAIL LINK TO GO HERE",
age_groups: { min: 16, max: 21 },
remind_at: 18,
analysis: {
strategy: "cdc",
redFlagThreshold: 1,
Expand Down Expand Up @@ -711,6 +712,7 @@ module.exports = [
introduction: "INTRODUCTION FOR CDC MATERIAL TO GO HERE",
detail_link: "DETAIL LINK TO GO HERE",
age_groups: { min: 21, max: 27 },
remind_at: 24,
analysis: {
strategy: "cdc",
redFlagThreshold: 1,
Expand Down
Binary file added email-pics/green_flag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added email-pics/red_flag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 0 additions & 43 deletions lambda/mark-and-send/Results.html

This file was deleted.

68 changes: 0 additions & 68 deletions lambda/mark-and-send/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions lambda/mark-and-send/package.json

This file was deleted.

77 changes: 0 additions & 77 deletions lambda/mark-and-send/sample-parameters.json

This file was deleted.

File renamed without changes.
42 changes: 42 additions & 0 deletions lambda/reminder-cron/.yarnclean
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# test directories
__tests__
test
tests
powered-test

# asset directories
docs
doc
website
images
assets

# examples
example
examples

# code coverage directories
coverage
.nyc_output

# build scripts
Makefile
Gulpfile.js
Gruntfile.js

# configs
.tern-project
.gitattributes
.editorconfig
.*ignore
.eslintrc
.jshintrc
.flowconfig
.documentup.json
.yarn-metadata.json
.*.yml
*.yml

# misc
*.gz
*.md
4 changes: 4 additions & 0 deletions lambda/reminder-cron/Reminder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Thank you for previously completing the Watch Me Grow app. It is recommended that you complete this when your child is aged
approximately 12, 18 and 24 months. This is a reminder to complete the Watch Me Grow app for your child again. As before,
these results will be sent via email. These can also be sent to your nominated health professional. Please go to the
<a href="https://watchmegrow.care">Watch Me Grow app.</a>
File renamed without changes.
81 changes: 81 additions & 0 deletions lambda/reminder-cron/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"use strict";

let https = require("https");
var aws = require("aws-sdk");
const moment = require("moment");
const mailgunJs = require("mailgun-js");
var markupJs = require("markup-js");
var fs = require("fs");
var strings = require("wmg-common/strings");
var questionnaires = require("wmg-common/questionnaires");
const _ = require("lodash");

const mailgun = mailgunJs({
apiKey: process.env.MAILGUN_API_KEY,
domain: "auto.watchmegrow.care"
});

process.env["PATH"] =
process.env["PATH"] + ":" + process.env["LAMBDA_TASK_ROOT"];

const questionnaireReminderAges = questionnaires
.filter(questionnaire => questionnaire.remind_at)
.map(questionnaire => ({
id: questionnaire.id,
remindAgeInDays: moment.duration(questionnaire.remind_at, "months").asDays()
}));

exports.handler = function(event, context, callback) {
console.log(questionnaireReminderAges);

return mailgun
.lists("[email protected]")
.members()
.list()
.then(members => {
const children = _.flatMap(members.items, member => {
return Object.keys(member.vars).map(varKey =>
Object.assign(member.vars[varKey], {
name: varKey,
email: member.address,
ageInDays: moment().diff(moment(member.vars[varKey].dob), "days")
})
);
});

console.log(children);

const toBeReminded = children.filter(child =>
questionnaireReminderAges.some(
questionnaire =>
!child.completed[questionnaire.id] &&
questionnaire.remindAgeInDays === child.ageInDays
)
);

const promises = toBeReminded.map(child => sendReminder(child));

return Promise.all(promises);
})
.then(result => {
console.log(result);
callback(null, "Successfully executed");
})
.catch(e => callback(e));
};

const reminderTemplateBody = fs.readFileSync(
__dirname + "/Reminder.html",
"utf-8"
);

function sendReminder(child) {
var params = {
from: "[email protected]",
to: child.email,
subject: "Watch Me Grow Reminder for " + child.name,
html: reminderTemplateBody
};

return mailgun.messages().send(params);
}
10 changes: 10 additions & 0 deletions lambda/reminder-cron/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "wmg-email",
"version": "0.0.1",
"dependencies": {
"lodash": "^4.17.4",
"mailgun-js": "^0.11.2",
"moment": "^2.18.1",
"wmg-common": "file:../../common"
}
}
Loading

0 comments on commit 948af16

Please sign in to comment.