From 82c84de33fdfc14cfe683c299d66b9135a7417dd Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 3 Jan 2018 14:22:52 -0700 Subject: [PATCH] code review --- middleware/staticTemplate.js | 1 + routes/index.js | 2 +- services/mailer.js | 28 +++++++++++++--------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/middleware/staticTemplate.js b/middleware/staticTemplate.js index 7f6184a03..c94797519 100644 --- a/middleware/staticTemplate.js +++ b/middleware/staticTemplate.js @@ -54,3 +54,4 @@ module.exports = async (req, res, next) => { }; module.exports.attachStaticLocals = attachStaticLocals; +module.exports.TEMPLATE_LOCALS = TEMPLATE_LOCALS; diff --git a/routes/index.js b/routes/index.js index c563eb434..74e37999a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -189,7 +189,7 @@ router.use('/', (err, req, res, next) => { if (err instanceof errors.APIError) { res.status(err.status); res.render('error', { - message: res.locals.t(err.translation_key), + message: res.locals.t(`error.${err.translation_key}`), error: process.env.NODE_ENV === 'development' ? err : {} }); } else { diff --git a/services/mailer.js b/services/mailer.js index b276a719f..ecb65de8c 100644 --- a/services/mailer.js +++ b/services/mailer.js @@ -4,7 +4,7 @@ const kue = require('./kue'); const path = require('path'); const fs = require('fs'); const _ = require('lodash'); -const {attachStaticLocals} = require('../middleware/staticTemplate'); +const {TEMPLATE_LOCALS} = require('../middleware/staticTemplate'); const i18n = require('./i18n'); @@ -62,11 +62,11 @@ const mailer = {}; // is enabled, we will be simulating that emails are being sent, because in a // production system, emails should and would be sent. mailer.enabled = Boolean( - SMTP_HOST && SMTP_HOST.length > 0 && - SMTP_USERNAME && SMTP_USERNAME.length > 0 && - SMTP_PORT && SMTP_PORT.length > 0 && - SMTP_PASSWORD && SMTP_PASSWORD.length > 0 && - SMTP_FROM_ADDRESS && SMTP_FROM_ADDRESS.length > 0 + SMTP_HOST && + SMTP_USERNAME && + SMTP_PORT && + SMTP_PASSWORD && + SMTP_FROM_ADDRESS ) || process.env.NODE_ENV === 'test'; if (mailer.enabled) { @@ -101,31 +101,29 @@ mailer.task = new kue.Task({ /** * send will create a new message and send it. */ -mailer.send = async ({template, locals, to, subject}) => { +mailer.send = async (options) => { if (!mailer.enabled) { throw new Error('email is not enabled because required configuration is not available'); } - // Attach the template locals. - attachStaticLocals(locals); - - // Attach the translation function. - locals.t = i18n.t; + // Create the new locals object and attach the static locals and the i18n + // framework. + const locals = _.merge({}, options.locals, TEMPLATE_LOCALS, {t: i18n.t}); // Render the templates. const [ html, text, ] = await Promise.all(['html', 'txt'].map((fmt) => { - return templates.render(template, fmt, locals); + return templates.render(options.template, fmt, locals); })); // Create the job. return mailer.task.create({ title: 'Mail', message: { - to, - subject: `${EMAIL_SUBJECT_PREFIX} ${subject}`, + to: options.to, + subject: `${EMAIL_SUBJECT_PREFIX} ${options.subject}`, text, html }