Skip to content

Commit

Permalink
kyyhky: Move custom data fields to template-specific files
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Jul 26, 2018
1 parent b4df04b commit 27cc47d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 69 deletions.
4 changes: 4 additions & 0 deletions config/message-templates/hugo-packet-series-extra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (data) => {
data.voter_email = data.email
data.email = '[email protected]'
}
14 changes: 14 additions & 0 deletions config/message-templates/hugo-update-nominations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function categoryString({ category, nominations }, wrap) {
const title = category.charAt(0) + category.slice(1).replace(/[A-Z]/g, ' $&')
const cn = nominations.map(n => {
const ns = Object.values(n).join('; ')
return ' - ' + wrap(' ', ns)
});
return `${title}:\n${cn.join('\n')}`
}

module.exports = (data, wrap) => {
data.nominations = data.nominations
.map(n => categoryString(n, wrap))
.join('\n\n')
}
14 changes: 14 additions & 0 deletions config/message-templates/hugo-update-votes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function categoryString({ category, finalists }, wrap) {
const fi = finalists && finalists.filter(f => f)
if (!fi || fi.length === 0) return null
const title = category.charAt(0) + category.slice(1).replace(/[A-Z]/g, ' $&')
const votes = fi.map((finalist, i) => ` ${i + 1}. ` + wrap(' ', finalist))
return `${title}:\n${votes.join('\n')}`
}

module.exports = (data, wrap) => {
data.votes = data.votes
.map(v => categoryString(v, wrap))
.filter(s => s)
.join('\n\n')
}
19 changes: 19 additions & 0 deletions config/message-templates/kansa-new-payment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function paymentDataString(data, shape, ignored) {
if (!data) return ''
return Object.keys(data)
.filter(key => key && data[key] && !ignored[key])
.map(key => {
const field = shape && shape.find(s => s.key === key)
const label = field && field.label || key;
return `${label}: ${data[key]}`
})
.join('\n')
}

module.exports = (data) => {
data.data = paymentDataString(data.data, data.shape, { mandate_url: true })
data.strAmount = data.currency.toUpperCase() + ' ' + (data.amount / 100).toFixed(2)
if (data.type === 'ss-token' && data.status === 'succeeded') {
return 'kansa-new-siteselection-token'
}
}
1 change: 1 addition & 0 deletions config/message-templates/kansa-update-payment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./kansa-new-payment')
3 changes: 3 additions & 0 deletions config/message-templates/kansa-upgrade-person.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (data) => {
if (data.paper_pubs) data.membership += ' with paper pubs'
}
3 changes: 2 additions & 1 deletion kyyhky/lib/login-uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ const apiRoot = process.env.API_URI_ROOT
const loginRoot = process.env.LOGIN_URI_ROOT

function barcodeUri({ id, key, memberId, membership }) {
if (membership === 'Supporter') return ''
if (!key || membership === 'Supporter') return ''
const parts = [apiRoot, 'barcode', key, memberId || id]
return encodeURI(parts.join('/'))
}

function loginUri({ email, key, memberId, path }) {
if (!key) return ''
const parts = [loginRoot, email, key]
if (memberId) parts.push(memberId)
let uri = parts.join('/')
Expand Down
91 changes: 23 additions & 68 deletions kyyhky/lib/send-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,24 @@ const sendgrid = require('./sendgrid')
const TEMPLATES_DIR = '/message-templates'
const WRAP_WIDTH = 78;

function nominationsString(data) {
return data.map(({ category, nominations }) => {
const ct = category.charAt(0) + category.slice(1).replace(/[A-Z]/g, ' $&');
const cn = nominations.map(n => {
const ns = Object.keys(n).map(k => n[k]).join('; ');
return ' - ' + wrap(WRAP_WIDTH - 4)(ns).replace(/\n/g, '\n ');
});
return `${ct}:\n${cn.join('\n')}`;
}).join('\n\n');
function wrapIndented(prefix, str) {
return prefix
? wrap(WRAP_WIDTH - prefix.length)(str).replace(/\n/g, '\n' + prefix)
: wrap(WRAP_WIDTH)(str)
}

function paymentDataString(data, shape, ignored) {
if (!data) return '';
const label = (key) => shape && (shape.find(s => s.key === key) || {}).label || key;
return Object.keys(data)
.filter(key => key && data[key] && !ignored[key])
.map(key => `${label(key)}: ${data[key]}`)
.join('\n');
}

function votesString(data) {
return data
.map(({ category, finalists }) => ({
title: category.charAt(0) + category.slice(1).replace(/[A-Z]/g, ' $&'),
votes: finalists && finalists.filter(finalist => finalist).map((finalist, i) => {
return ` ${i+1}. ` + wrap(WRAP_WIDTH - 4)(finalist).replace(/\n/g, '\n ');
})
}))
.filter(({ votes }) => votes && votes.length > 0)
.map(({ title, votes }) => `${title}:\n${votes.join('\n')}`)
.join('\n\n');
function applyCustomConfig(name, data) {
const fn = path.resolve(process.cwd(), TEMPLATES_DIR, name)
return new Promise((resolve, reject) => {
try {
const customFunction = require(fn)
const customName = customFunction(data, wrapIndented)
resolve(customName || name)
} catch (error) {
if (error && error.code === 'MODULE_NOT_FOUND') resolve(name)
else reject(error)
}
})
}

function getTemplate(name) {
Expand Down Expand Up @@ -74,45 +61,13 @@ function sgRequest(msgTemplate, data) {
});
}

function sendEmail(tmplName, data) {
let tmplData = Object.assign({
barcode_uri: barcodeUri(data),
login_uri: loginUri(data)
}, data);
switch (tmplName) {

case 'hugo-packet-series-extra':
tmplData = {
email: '[email protected]',
voter_email: data.email
}
break;

case 'hugo-update-nominations':
tmplData.nominations = nominationsString(data.nominations);
break;

case 'hugo-update-votes':
tmplData.votes = votesString(data.votes);
break;

case 'kansa-new-payment':
case 'kansa-update-payment':
if (data.type === 'ss-token' && data.status === 'succeeded') {
tmplName = 'kansa-new-siteselection-token';
}
tmplData.data = paymentDataString(data.data, data.shape, { mandate_url: true });
tmplData.strAmount = data.currency.toUpperCase() + ' ' + (data.amount / 100).toFixed(2);
break;

case 'kansa-upgrade-person':
if (data.paper_pubs) tmplData.membership += ' with paper pubs';
break;

}
return getTemplate(tmplName)
.then(msgTemplate => {
const request = sgRequest(msgTemplate, tmplData);
function sendEmail(name, data) {
data.barcode_uri = barcodeUri(data)
data.login_uri = loginUri(data)
return applyCustomConfig(name, data)
.then(getTemplate)
.then(template => {
const request = sgRequest(template, data);
return sendgrid.API(request)
})
.then(() => data.email)
Expand Down

0 comments on commit 27cc47d

Please sign in to comment.