Skip to content

Commit

Permalink
feat(mailer): mailer integration
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Apr 5, 2020
1 parent e757b6f commit 9e3fb59
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 96 deletions.
4 changes: 4 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const config = {
password: process.env.PG_PASSWORD || '5ecr3t',
database: process.env.DB_DATABASE || 'core'
},
mailer: {
url: 'http://oms-mailer',
port: 4000
},
logger: {
silent: false,
level: process.env.LOGLEVEL || 'debug'
Expand Down
3 changes: 3 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ module.exports = {
REFRESH_TOKEN: 128,
PASSWORD: 10,
PASSWORD_RESET: 128
},
MAIL_SUBJECTS: {
MAIL_CONFIRMATION: 'Please confirm your MyAEGEE account'
}
};
37 changes: 37 additions & 0 deletions lib/mailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const request = require('request-promise-native');

const config = require('../config');

/**
* @param {Object} options
* @param {string|string[]} options.to
* @param {string} options.template
* @param {string} options.subject
* @param {object} options.reply_to
* @param {object} options.parameters
*/
module.exports.sendMail = async (options) => {
const mailerBody = await request({
url: config.mailer.url + ':' + config.mailer.port + '/',
method: 'POST',
simple: false,
json: true,
body: {
to: options.to,
subject: options.subject,
template: options.template,
parameters: options.parameters,
reply_to: options.reply_to
}
});

if (typeof mailerBody !== 'object') {
throw new Error('Malformed response from mailer: ' + mailerBody);
}

if (!mailerBody.success) {
throw new Error('Unsuccessful response from mailer: ' + JSON.stringify(mailerBody));
}

return mailerBody;
};
46 changes: 30 additions & 16 deletions middlewares/campaigns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {
const errors = require('../lib/errors');
const helpers = require('../lib/helpers');
const constants = require('../lib/constants');
const { sequelize } = require('../lib/sequelize');
const mailer = require('../lib/mailer');

exports.registerUser = async (req, res) => {
const campaign = await Campaign.findOne({ where: { url: req.params.campaign_id } });
Expand All @@ -17,27 +19,39 @@ exports.registerUser = async (req, res) => {
return errors.makeForbiddenError(res, 'Campaign is not active.');
}

const user = await User.scope('noExtraFields').create({
...req.body,
campaign_id: campaign.id
}, { fields: constants.FIELDS_TO_UPDATE.USER.CREATE });
let user;

// Adding a person to a body if campaign has the autojoin body.
if (campaign.autojoin_body_id) {
await BodyMembership.create({
user_id: user.id,
body_id: campaign.autojoin_body_id
});
}
await sequelize.transaction(async (t) => {
user = await User.scope('noExtraFields').create({
...req.body,
campaign_id: campaign.id
}, { fields: constants.FIELDS_TO_UPDATE.USER.CREATE, transaction: t });

const confirmation = await MailConfirmation.createForUser(user.id);
// Adding a person to a body if campaign has the autojoin body.
if (campaign.autojoin_body_id) {
await BodyMembership.create({
user_id: user.id,
body_id: campaign.autojoin_body_id
}, { transaction: t });
}

const confirmation = await MailConfirmation.createForUser(user.id, t);

await mailer.sendMail({
to: user.email,
subject: constants.MAIL_SUBJECTS.MAIL_CONFIRMATION,
template: 'confirm_email.html',
parameters: {
name: user.first_name,
surname: user.last_name,
token: confirmation.value
}
});
});

return res.json({
success: true,
data: {
user,
confirmation
}
data: user
});
};

Expand Down
4 changes: 2 additions & 2 deletions models/MailConfirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ const MailConfirmation = sequelize.define('mail_confirmation', {
updatedAt: 'updated_at',
});

MailConfirmation.createForUser = async function createForUser(userId) {
MailConfirmation.createForUser = async function createForUser(userId, transaction) {
const value = await helpers.getRandomBytes(constants.TOKEN_LENGTH.MAIL_CONFIRMATION);
return MailConfirmation.create({
user_id: userId,
expires_at: moment().add(config.ttl.mail_confirmation, 'seconds').toDate(),
value
});
}, { transaction });
};


Expand Down
Loading

0 comments on commit 9e3fb59

Please sign in to comment.