Skip to content

Commit a047d6f

Browse files
committed
mission planning: notify volunteers per mail #33
1 parent 6d0b3cc commit a047d6f

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

routes/api/missions.js

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const keystone = require('keystone');
2-
// const utils = require('keystone-utils');
3-
// const Email = require('keystone-email');
4-
// const mailConfig = require('../../config').mail;
2+
const Email = require('keystone-email');
3+
const mailConfig = require('../../config').mail;
54

6-
// const Mission = keystone.list('Mission');
5+
const Volunteer = keystone.list('Volunteer');
76
const Mission = keystone.list('Mission');
87

98
/**
@@ -49,14 +48,85 @@ exports.create = (req, res) => {
4948
* Update a Missions Data
5049
*/
5150
exports.update = (req, res) => {
51+
const newData = req.body;
52+
5253
Mission.model
5354
.findById(req.params.id, (err, mission) => {
5455
if (err) return res.apiError(err.detail.errmsg);
5556
if (!mission) return res.apiError('not found');
5657

57-
mission.getUpdateHandler(req).process(req.body, (err2) => {
58+
const data = {};
59+
60+
if (newData.crew) {
61+
const oldCrewIDs = mission.crew.map(a => a.volunteer.toString());
62+
const newCrewIDs = newData.crew.map(a => a.volunteer);
63+
64+
data.removed = oldCrewIDs.filter(id => !newCrewIDs.includes(id));
65+
data.added = newCrewIDs.filter(id => !oldCrewIDs.includes(id));
66+
67+
if (newData.start || newData.end) {
68+
data.unchanged = oldCrewIDs.filter(id => newCrewIDs.includes(id));
69+
newData.crew
70+
.filter(assignment => assignment.status === 'yes')
71+
.forEach(assignment => assignment.status = 'pending');
72+
}
73+
}
74+
75+
mission.getUpdateHandler(req).process(newData, (err2) => {
5876
if (err2) return res.apiError(err2.detail.errmsg);
59-
res.apiResponse({ mission });
77+
78+
if (data.removed || data.added || data.unchanged) {
79+
Volunteer.model
80+
.find({})
81+
.where('_id')
82+
.in([].concat(data.added, data.removed, data.unchanged))
83+
.exec((err3, volunteers) => {
84+
const promises = volunteers.map((volunteer) => {
85+
const values = {
86+
volunteer: volunteer.name.first,
87+
mission: mission.name,
88+
start: new Date(mission.start).toDateString(),
89+
end: new Date(mission.end).toDateString(),
90+
host: `${req.protocol}://${req.get('host')}`,
91+
link: '/volunteer/',
92+
};
93+
94+
if (data.added.includes(volunteer.id)) {
95+
values.reason = 'You\'ve been added to a mission';
96+
}
97+
else if (data.removed.includes(volunteer.id)) {
98+
values.reason = 'You\'ve been removed from a mission';
99+
}
100+
else { // unchanged
101+
values.reason = 'The date of a mission changed';
102+
}
103+
104+
const subject = `cadus crewing | ${values.reason}`;
105+
106+
return sendEmail(volunteer.email, subject, values);
107+
});
108+
109+
Promise.all(promises)
110+
.then(() => res.apiResponse({ mission }))
111+
.catch(() => res.apiError('saved data but couldn\'t send all emails'));
112+
});
113+
}
114+
else {
115+
res.apiResponse({ mission });
116+
}
60117
});
61118
});
62119
};
120+
121+
function sendEmail(to, subject, data) {
122+
const options = {
123+
to,
124+
subject,
125+
from: mailConfig.sender,
126+
nodemailerConfig: mailConfig.nodemailerConfig,
127+
};
128+
const template = 'templates/emails/volunteer-mission-changed.jade';
129+
130+
return new Promise(resolve => new Email(template, { transport: 'nodemailer' })
131+
.send(data, options, resolve));
132+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extends layouts/base
2+
3+
block content
4+
p Hi #{volunteer},
5+
p= reason
6+
h2= mission
7+
p It starts on #{start} and ends on #{end}.
8+
p You can view the mission on your dashboard:
9+
a(href=host + link)= host + link

0 commit comments

Comments
 (0)