Merge pull request #147 from witzig/master

Moved Merge Tag Helpers to helpers.js
This commit is contained in:
Andris Reinman 2017-03-04 23:34:36 +02:00 committed by GitHub
commit 1c38a1a7bb
4 changed files with 81 additions and 75 deletions

76
lib/helpers.js Normal file
View file

@ -0,0 +1,76 @@
'use strict';
let lists = require('./models/lists');
let fields = require('./models/fields');
module.exports = {
getDefaultMergeTags,
getListMergeTags
};
function getDefaultMergeTags(callback) {
// Using a callback for the sake of future-proofness
callback(null, [
{
key: 'LINK_UNSUBSCRIBE',
value: 'URL that points to the unsubscribe page'
}, {
key: 'LINK_PREFERENCES',
value: 'URL that points to the preferences page of the subscriber'
}, {
key: 'LINK_BROWSER',
value: 'URL to preview the message in a browser'
}, {
key: 'EMAIL',
value: 'Email address'
}, {
key: 'FIRST_NAME',
value: 'First name'
}, {
key: 'LAST_NAME',
value: 'Last name'
}, {
key: 'FULL_NAME',
value: 'Full name (first and last name combined)'
}, {
key: 'SUBSCRIPTION_ID',
value: 'Unique ID that identifies the recipient'
}, {
key: 'LIST_ID',
value: 'Unique ID that identifies the list used for this campaign'
}, {
key: 'CAMPAIGN_ID',
value: 'Unique ID that identifies current campaign'
}
]);
}
function getListMergeTags(listId, callback) {
lists.get(listId, (err, list) => {
if (err) {
return callback(err);
}
if (!list) {
list = {
id: listId
};
}
fields.list(list.id, (err, fieldList) => {
if (err && !fieldList) {
fieldList = [];
}
let mergeTags = [];
fieldList.forEach(field => {
mergeTags.push({
key: field.key,
value: field.name
});
});
return callback(null, mergeTags);
});
});
}

View file

@ -20,8 +20,6 @@ module.exports = {
formatMessage,
getMessageLinks,
prepareHtml,
getDefaultMergeTags,
getListMergeTags,
workers: new Set()
};
@ -224,73 +222,3 @@ function prepareHtml(html, callback) {
return callback(null, juice(preparedHtml));
});
}
function getDefaultMergeTags(callback) {
// Using a callback for the sake of future-proofness
callback(null, [
{
key: 'LINK_UNSUBSCRIBE',
value: 'URL that points to the unsubscribe page'
}, {
key: 'LINK_PREFERENCES',
value: 'URL that points to the preferences page of the subscriber'
}, {
key: 'LINK_BROWSER',
value: 'URL to preview the message in a browser'
}, {
key: 'EMAIL',
value: 'Email address'
}, {
key: 'FIRST_NAME',
value: 'First name'
}, {
key: 'LAST_NAME',
value: 'Last name'
}, {
key: 'FULL_NAME',
value: 'Full name (first and last name combined)'
}, {
key: 'SUBSCRIPTION_ID',
value: 'Unique ID that identifies the recipient'
}, {
key: 'LIST_ID',
value: 'Unique ID that identifies the list used for this campaign'
}, {
key: 'CAMPAIGN_ID',
value: 'Unique ID that identifies current campaign'
}
]);
}
function getListMergeTags(listId, callback) {
let lists = require('./models/lists');
let fields = require('./models/fields');
lists.get(listId, (err, list) => {
if (err) {
return callback(err);
}
if (!list) {
list = {
id: listId
};
}
fields.list(list.id, (err, fieldList) => {
if (err && !fieldList) {
fieldList = [];
}
let mergeTags = [];
fieldList.forEach(field => {
mergeTags.push({
key: field.key,
value: field.name
});
});
return callback(null, mergeTags);
});
});
}

View file

@ -9,6 +9,7 @@ let campaigns = require('../lib/models/campaigns');
let subscriptions = require('../lib/models/subscriptions');
let settings = require('../lib/models/settings');
let tools = require('../lib/tools');
let helpers = require('../lib/helpers');
let striptags = require('striptags');
let passport = require('../lib/passport');
let htmlescape = require('escape-html');
@ -183,13 +184,13 @@ router.get('/edit/:id', passport.csrfProtection, (req, res, next) => {
view = 'campaigns/edit';
}
tools.getDefaultMergeTags((err, defaultMergeTags) => {
helpers.getDefaultMergeTags((err, defaultMergeTags) => {
if (err) {
req.flash('danger', err.message || err);
return res.redirect('/');
}
tools.getListMergeTags(campaign.list, (err, listMergeTags) => {
helpers.getListMergeTags(campaign.list, (err, listMergeTags) => {
if (err) {
req.flash('danger', err.message || err);
return res.redirect('/');

View file

@ -6,6 +6,7 @@ let router = new express.Router();
let templates = require('../lib/models/templates');
let settings = require('../lib/models/settings');
let tools = require('../lib/tools');
let helpers = require('../lib/helpers');
let striptags = require('striptags');
let passport = require('../lib/passport');
let mailer = require('../lib/mailer');
@ -112,7 +113,7 @@ router.get('/edit/:id', passport.csrfProtection, (req, res, next) => {
return next(err);
}
tools.getDefaultMergeTags((err, defaultMergeTags) => {
helpers.getDefaultMergeTags((err, defaultMergeTags) => {
if (err) {
req.flash('danger', err.message || err);
return res.redirect('/templates');