Commit 5b490926 authored by wu-lee's avatar wu-lee

serve.js - move email template code into lib/email-templates.js

parent 08a46906
'use strict';
/** Defines functions which map registration data into HTML email
* content for user acknowledgements and admin notifications.
*/
const handlebars = require('handlebars');
const fs = require('fs');
const handlebarsHelpers = require('./helpers.js');
const config = require('./config.js');
const maillog = require('./logging.js').loggers.mail;
// Register the helpers - we use the same ones as supplied to the
// theme, out of laziness.
for(var name in handlebarsHelpers) {
handlebars.registerHelper(name, handlebarsHelpers[name]);
}
function defineTemplateLoader(file, name, defaultTemplate) {
if (!file) {
maillog.warn(`no configured ${name} template file, using default`);
return defaultTemplate;
}
maillog.info(`${name} will be loaded from the file: ${file}`);
return (context) => {
if (fs.existsSync(file)) {
maillog.info(`loading ${name} from file: ${file}`);
const content = fs.readFileSync(file, 'utf8');
return handlebars.compile(content)(context);
}
else {
maillog.warn(`using default as no ${name} found at ${file}`);
return defaultTemplate(context);
}
};
}
// Define functions which returns the email templates. These are
// re-loaded on each send so that they can be defined by files in the
// source (with inline handlebars markup). (These templates can be
// excluded from appearing in the output wiki by various means.) The
// one required field in submitted data is an 'email' field with a
// recipient address. All fields, plus parts of the config, are
// supplied to the templates.
var notificationTemplate, acknowledgementTemplate;
if (config.email && config.email.templates) {
notificationTemplate = config.email.templates.notification;
acknowledgementTemplate = config.email.templates.acknowledgement;
}
module.exports = {
notification: defineTemplateLoader(
notificationTemplate,
'email notification template',
(context) =>
`<pre>${JSON.stringify(context.request.body, null, 2)}</pre>`
),
acknowledgement: defineTemplateLoader(
acknowledgementTemplate,
'email acknowledgement template',
(context) => 'Thanks! We shall be in touch.'
),
};
'use strict';
const express = require('express');
const fs = require('fs');
const handlebars = require('handlebars');
const handlebarsHelpers = require('./lib/helpers.js');
const emailTemplates = require('./lib/email-templates.js');
const metalsmith = require('./metalsmith.js');
const email = require('./lib/email.js');
const git = require('./lib/git.js')({});
......@@ -41,51 +40,6 @@ var fileOptions = {
}
};
// Define functions which returns the email templates. These are
// re-loaded on each send so that they can be defined by files in the
// source (with inline handlebars markup). (These templates can be
// excluded from appearing in the output wiki by various means.) The
// one required field in submitted data is an 'email' field with a
// recipient address. All fields, plus parts of the config, are
// supplied to the templates.
for(var name in handlebarsHelpers) {
handlebars.registerHelper(name, handlebarsHelpers[name]);
}
function defineTemplateLoader(file, name, defaultTemplate) {
if (!file) {
maillog.warn(`no configured ${name} template file, using default`);
return defaultTemplate;
}
maillog.info(`${name} will be loaded from the file: ${file}`);
return (context) => {
if (fs.existsSync(file)) {
maillog.info(`loading ${name} from file: ${file}`);
const content = fs.readFileSync(file, 'utf8');
return handlebars.compile(content)(context);
}
else {
maillog.warn(`using default as no ${name} found at ${file}`);
return defaultTemplate(context);
}
};
}
var notificationTemplate, acknowledgementTemplate;
if (config.email && config.email.templates) {
notificationTemplate = config.email.templates.notification;
acknowledgementTemplate = config.email.templates.acknowledgement;
}
const notificationContent = defineTemplateLoader(
notificationTemplate,
'email notification template',
(context) =>
`<pre>${JSON.stringify(context.request.body, null, 2)}</pre>`
);
const acknowledgementContent = defineTemplateLoader(
acknowledgementTemplate,
'email acknowledgement template',
(context) => 'Thanks! We shall be in touch.'
);
async function build() {
buildlog.info('starting build...')
metalsmith().build(function(err) {
......@@ -178,7 +132,7 @@ app.post('/registration', (req, res) => {
subject: sendConfig.notification,
};
try {
const html = notificationContent(templateContext);
const html = emailTemplates.notification(templateContext);
send(notificationConfig,
html,
req.body)
......@@ -196,7 +150,7 @@ app.post('/registration', (req, res) => {
subject: sendConfig.acknowledgement,
};
try {
const html = acknowledgementContent(templateContext);
const html = emailTemplates.acknowledgement(templateContext);
submitlog.info(`emailing registration acknowledgement to ${acknowledgementConfig.to}: `);
send(acknowledgementConfig,
html)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment