Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 2x 2x 2x 2x 11x 11x 11x 33x 31x 28x 8x 13x 8x 3x 40x 39x 39x 2x 2x 8x 5x 5x 11x 14x 13x 14x 14x 7x 7x 7x 23x 57x 25x 13x 13x 16x 7x 22x 78x 26x 11x 11x 11x 11x 11x 11x 2x | // @implements UC-WORKFLOW-010.1
/**
* Renders an entity's email template for one recipient: picks the language,
* replaces each `{fieldId}` with the item's value as a reader sees it, and
* appends the link to the item's entity. Plain text in, plain text and escaped
* HTML out — a template never carries markup (utils/emailTemplates.js).
*/
const { escapeHtml } = require('../utils/email')
const { isDeletedPlaceholder } = require('../utils/deletedPlaceholder')
const BOOLEAN_LABELS = {
en: ['Yes', 'No'],
fr: ['Oui', 'Non'],
es: ['Sí', 'No']
}
const LINK_LABELS = {
en: 'Open in Browsway Flows',
fr: 'Ouvrir dans Browsway Flows',
es: 'Abrir en Browsway Flows'
}
/**
* The language a recipient reads a template in: their first UI language the
* template has, else the entity's (then the flow's) default language, else the
* template's first language.
*
* @param {object} template - { subject: { lang: text }, body: { lang: text } }
* @param {unknown} languagePreferences - The recipient's ordered language codes
* @param {string[]} defaults - Fallback languages, in order
* @returns {string}
*/
function pickTemplateLanguage(template, languagePreferences, defaults) {
const available = Object.keys(template?.subject || {})
const candidates = [
...(Array.isArray(languagePreferences) ? languagePreferences : []),
...defaults
]
return candidates.find((lang) => available.includes(lang)) ?? available[0]
}
function localized(value, lang) {
if (value === undefined || value === null) return ''
if (typeof value !== 'object') return String(value)
return value[lang] ?? Object.values(value)[0] ?? ''
}
function optionLabel(field, value, lang) {
const option = (field.options || []).find((o) =>
typeof o === 'object' && o !== null ? o.value === value : o === value
)
if (option && typeof option === 'object') return localized(option.label ?? option.value, lang)
return String(value)
}
/**
* One field's value as text.
*
* @param {object} field
* @param {unknown} value
* @param {string} lang
* @param {Map<string, string>} userNames - Names of the users the item references
* @returns {string}
*/
function formatValue(field, value, lang, userNames) {
if (value === undefined || value === null || value === '') return ''
const values = Array.isArray(value) ? value : [value]
switch (field.type) {
case 'boolean': {
const [yes, no] = BOOLEAN_LABELS[lang] || BOOLEAN_LABELS.en
return value === true ? yes : no
}
case 'select':
case 'multiselect':
return values.map((v) => optionLabel(field, v, lang)).join(', ')
case 'workflow': {
const state = (field.workflowDefinition?.states || []).find((s) => s.id === value)
return state ? localized(state.label, lang) : String(value)
}
case 'user':
return values
.filter((v) => !isDeletedPlaceholder(v))
.map((v) => userNames.get(v) || '')
.filter(Boolean)
.join(', ')
default:
// Text fields are stored per language ({ en, fr }): the recipient's, else the first
return values
.map((v) => (v && typeof v === 'object' ? localized(v, lang) : String(v)))
.join(', ')
}
}
/**
* The user ids a template inserts through `user` fields, whose names the
* rendering needs.
*
* @param {object} template
* @param {object[]} fields
* @param {object} item
* @returns {string[]}
*/
function insertedUserIds(template, fields, item) {
const texts = [...Object.values(template.subject || {}), ...Object.values(template.body || {})]
const ids = new Set()
for (const text of texts) {
for (const [, fieldId] of String(text).matchAll(/\{([^{}]*)\}/g)) {
const field = fields.find((f) => f.fieldId === fieldId.trim())
if (field?.type !== 'user') continue
const value = item[field.fieldId]
for (const id of Array.isArray(value) ? value : [value]) {
if (typeof id === 'string' && id && !isDeletedPlaceholder(id)) ids.add(id)
}
}
}
return [...ids]
}
function fillPlaceholders(text, fields, item, lang, userNames) {
return String(text || '').replace(/\{([^{}]*)\}/g, (match, rawId) => {
const field = fields.find((f) => f.fieldId === rawId.trim())
return field ? formatValue(field, item[field.fieldId], lang, userNames) : match
})
}
/**
* @param {object} params
* @param {object} params.template
* @param {string} params.lang
* @param {object[]} params.fields
* @param {object} params.item
* @param {Map<string, string>} params.userNames
* @param {string} params.link - The item's page in the app
* @returns {{ subject: string, text: string, html: string }}
*/
function renderEmail({ template, lang, fields, item, userNames, link }) {
// A subject is one line: a value with a line break must not split it
const subject = fillPlaceholders(localized(template.subject, lang), fields, item, lang, userNames)
.replace(/\s*[\r\n]+\s*/g, ' ')
.trim()
const body = fillPlaceholders(localized(template.body, lang), fields, item, lang, userNames)
const linkLabel = LINK_LABELS[lang] || LINK_LABELS.en
const text = `${body}\n\n${linkLabel}: ${link}`
const html =
`<div style="font-family:sans-serif;white-space:pre-wrap">${escapeHtml(body)}</div>` +
`<p style="font-family:sans-serif"><a href="${escapeHtml(link)}">${escapeHtml(linkLabel)}</a></p>`
return { subject, text, html }
}
module.exports = { pickTemplateLanguage, formatValue, insertedUserIds, renderEmail }
|