All files / utils/emailTemplates membershipRequest.js

100% Statements 20/20
83.33% Branches 10/12
100% Functions 5/5
100% Lines 16/16

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            2x   54x   2x                                                                                                                     12x 12x 12x 12x 12x 12x   12x 12x               12x 12x       6x     6x   2x  
// @implements UC-SPACE-007.1
/**
 * The membership request emails, in the flow's look (./flowLayout.js): a new
 * request to the approvers, then the decision to the requester.
 */
 
const { renderFlowEmailHtml } = require('./flowLayout')
 
const fill = (text, values) => text.replace(/\{(\w+)\}/g, (_match, key) => values[key] ?? '')
 
const TRANSLATIONS = {
  en: {
    newSubject: 'New membership request from {name}',
    newTitle: 'New membership request',
    newIntro: '{name} asks to join {flow}.',
    newAction: 'Review the request',
    newFooter:
      'You receive this email because your role in {flow} can approve membership requests.',
    approvedSubject: 'Welcome to {flow}',
    approvedTitle: 'Your request was approved',
    approvedIntro: 'You are now a member of {flow}.',
    approvedAction: 'Open {flow}',
    refusedSubject: 'Your request to join {flow}',
    refusedTitle: 'Your request was not accepted',
    refusedIntro: '{flow} did not accept your membership request. You may ask again later.',
    refusedAction: 'Visit {flow}'
  },
  fr: {
    newSubject: 'Nouvelle demande d’adhésion de {name}',
    newTitle: 'Nouvelle demande d’adhésion',
    newIntro: '{name} demande à rejoindre {flow}.',
    newAction: 'Voir la demande',
    newFooter:
      'Vous recevez cet email car votre rôle dans {flow} permet d’approuver les demandes d’adhésion.',
    approvedSubject: 'Bienvenue dans {flow}',
    approvedTitle: 'Votre demande a été acceptée',
    approvedIntro: 'Vous êtes maintenant membre de {flow}.',
    approvedAction: 'Ouvrir {flow}',
    refusedSubject: 'Votre demande pour rejoindre {flow}',
    refusedTitle: 'Votre demande n’a pas été acceptée',
    refusedIntro:
      '{flow} n’a pas accepté votre demande d’adhésion. Vous pourrez en faire une nouvelle plus tard.',
    refusedAction: 'Voir {flow}'
  },
  es: {
    newSubject: 'Nueva solicitud de adhesión de {name}',
    newTitle: 'Nueva solicitud de adhesión',
    newIntro: '{name} pide unirse a {flow}.',
    newAction: 'Ver la solicitud',
    newFooter:
      'Recibes este email porque tu rol en {flow} permite aprobar las solicitudes de adhesión.',
    approvedSubject: 'Bienvenido a {flow}',
    approvedTitle: 'Tu solicitud ha sido aceptada',
    approvedIntro: 'Ahora eres miembro de {flow}.',
    approvedAction: 'Abrir {flow}',
    refusedSubject: 'Tu solicitud para unirte a {flow}',
    refusedTitle: 'Tu solicitud no ha sido aceptada',
    refusedIntro:
      '{flow} no ha aceptado tu solicitud de adhesión. Podrás hacer una nueva más adelante.',
    refusedAction: 'Ver {flow}'
  }
}
 
/**
 * @param {'new'|'approved'|'refused'} kind
 * @param {{ lang: string, flow: object, link: string, requesterName?: string }} params
 * @returns {{ subject: string, html: string, text: string }}
 */
function render(kind, { lang, flow, link, requesterName }) {
  const t = TRANSLATIONS[lang] || TRANSLATIONS.en
  const values = { flow: flow.name || '', name: requesterName || '' }
  const title = fill(t[`${kind}Title`], values)
  const intro = fill(t[`${kind}Intro`], values)
  const actionLabel = fill(t[`${kind}Action`], values)
  const footer = t[`${kind}Footer`] ? fill(t[`${kind}Footer`], values) : undefined
 
  const text = [intro, '', `${actionLabel}: ${link}`, ...(footer ? ['', footer] : [])].join('\n')
  const html = renderFlowEmailHtml({
    flow,
    title,
    paragraphs: [intro],
    action: { label: actionLabel, url: link },
    footer
  })
  // A name is the user's own text: no line break may reach the Subject header
  const subject = fill(t[`${kind}Subject`], values).replace(/[\r\n]+/g, ' ')
  return { subject, html, text }
}
 
/** To an approver: someone asks to join */
const renderNewRequestEmail = (params) => render('new', params)
 
/** To the requester: the decision ('approved' or 'refused') */
const renderDecisionEmail = (decision, params) => render(decision, params)
 
module.exports = { renderNewRequestEmail, renderDecisionEmail }