Skip to content

Commit

Permalink
feat(events): mail organizers on event create/update. Fixes MEMB-750
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Jan 4, 2020
1 parent 278c6c3 commit b315ffc
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ const merge = require('./merge');
const constants = require('./constants');
const helpers = require('./helpers');
const { Event, Application } = require('../models');
const { Sequelize } = require('./sequelize');
const { Sequelize, sequelize } = require('./sequelize');
const core = require('./core');
const mailer = require('./mailer');


exports.listEvents = async (req, res) => {
// Get default query obj.
const defaultQueryObj = helpers.getDefaultQuery(req);
Expand Down Expand Up @@ -144,8 +143,20 @@ exports.addEvent = async (req, res) => {
core.fetchBody(body, req.headers['x-auth-token'])));
}


await event.save();
await sequelize.transaction(async (t) => {
// Creating the event in a transaction, so if mail sending fails, the update would be reverted.
await event.save({ transaction: t });

// Sending the mail to a user.
await mailer.sendMail({
to: event.organizers.map((organizer) => organizer.email),
subject: 'The event was created',
template: 'events_event_created.html',
parameters: {
event
}
});
});

return res.status(201).json({
success: true,
Expand Down Expand Up @@ -201,7 +212,21 @@ exports.editEvent = async (req, res) => {
core.fetchBody(body, req.headers['x-auth-token'])));
}

await event.update(data);
await sequelize.transaction(async (t) => {
// Updating the event in a transaction, so if mail sending fails, the update would be reverted.
await event.update(data, { transaction: t });

// Sending the mail to a user.
await mailer.sendMail({
to: event.organizers.map((organizer) => organizer.email),
subject: 'The event was updated',
template: 'events_event_updated.html',
parameters: {
event
}
});
});


return res.json({
success: true,
Expand Down

0 comments on commit b315ffc

Please sign in to comment.