diff --git a/lib/helpers.js b/lib/helpers.js new file mode 100644 index 00000000..521d46a0 --- /dev/null +++ b/lib/helpers.js @@ -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); + }); + }); +} diff --git a/lib/tools.js b/lib/tools.js index b1aecb72..41c8b782 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -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); - }); - }); -} diff --git a/routes/campaigns.js b/routes/campaigns.js index 6636d7e2..ed6ae632 100644 --- a/routes/campaigns.js +++ b/routes/campaigns.js @@ -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('/'); diff --git a/routes/templates.js b/routes/templates.js index 778f4cb4..12cb1ec1 100644 --- a/routes/templates.js +++ b/routes/templates.js @@ -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');