Skip to content

Commit

Permalink
mission planning: notify volunteers per mail #33
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeier committed Apr 27, 2017
1 parent 6d0b3cc commit a047d6f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
82 changes: 76 additions & 6 deletions routes/api/missions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const keystone = require('keystone');
// const utils = require('keystone-utils');
// const Email = require('keystone-email');
// const mailConfig = require('../../config').mail;
const Email = require('keystone-email');
const mailConfig = require('../../config').mail;

// const Mission = keystone.list('Mission');
const Volunteer = keystone.list('Volunteer');
const Mission = keystone.list('Mission');

/**
Expand Down Expand Up @@ -49,14 +48,85 @@ exports.create = (req, res) => {
* Update a Missions Data
*/
exports.update = (req, res) => {
const newData = req.body;

Mission.model
.findById(req.params.id, (err, mission) => {
if (err) return res.apiError(err.detail.errmsg);
if (!mission) return res.apiError('not found');

mission.getUpdateHandler(req).process(req.body, (err2) => {
const data = {};

if (newData.crew) {
const oldCrewIDs = mission.crew.map(a => a.volunteer.toString());
const newCrewIDs = newData.crew.map(a => a.volunteer);

data.removed = oldCrewIDs.filter(id => !newCrewIDs.includes(id));
data.added = newCrewIDs.filter(id => !oldCrewIDs.includes(id));

if (newData.start || newData.end) {
data.unchanged = oldCrewIDs.filter(id => newCrewIDs.includes(id));
newData.crew
.filter(assignment => assignment.status === 'yes')
.forEach(assignment => assignment.status = 'pending');
}
}

mission.getUpdateHandler(req).process(newData, (err2) => {
if (err2) return res.apiError(err2.detail.errmsg);
res.apiResponse({ mission });

if (data.removed || data.added || data.unchanged) {
Volunteer.model
.find({})
.where('_id')
.in([].concat(data.added, data.removed, data.unchanged))
.exec((err3, volunteers) => {
const promises = volunteers.map((volunteer) => {
const values = {
volunteer: volunteer.name.first,
mission: mission.name,
start: new Date(mission.start).toDateString(),
end: new Date(mission.end).toDateString(),
host: `${req.protocol}://${req.get('host')}`,
link: '/volunteer/',
};

if (data.added.includes(volunteer.id)) {
values.reason = 'You\'ve been added to a mission';
}
else if (data.removed.includes(volunteer.id)) {
values.reason = 'You\'ve been removed from a mission';
}
else { // unchanged
values.reason = 'The date of a mission changed';
}

const subject = `cadus crewing | ${values.reason}`;

return sendEmail(volunteer.email, subject, values);
});

Promise.all(promises)
.then(() => res.apiResponse({ mission }))
.catch(() => res.apiError('saved data but couldn\'t send all emails'));
});
}
else {
res.apiResponse({ mission });
}
});
});
};

function sendEmail(to, subject, data) {
const options = {
to,
subject,
from: mailConfig.sender,
nodemailerConfig: mailConfig.nodemailerConfig,
};
const template = 'templates/emails/volunteer-mission-changed.jade';

return new Promise(resolve => new Email(template, { transport: 'nodemailer' })
.send(data, options, resolve));
}
9 changes: 9 additions & 0 deletions templates/emails/volunteer-mission-changed.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extends layouts/base

block content
p Hi #{volunteer},
p= reason
h2= mission
p It starts on #{start} and ends on #{end}.
p You can view the mission on your dashboard:
a(href=host + link)= host + link

0 comments on commit a047d6f

Please sign in to comment.