All files / utils/emailTemplates apiKeyCreated.js

100% Statements 14/14
100% Branches 6/6
100% Functions 3/3
100% Lines 14/14

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          2x   2x                                                                                             14x         14x                   9x 9x 9x 9x       9x                       9x 27x   9x                             9x     2x  
/**
 * "A new API key was created" email, sent to the key's creator so a key they
 * did not create themselves (a hijacked session) does not go unnoticed.
 */
 
const { escapeHtml } = require('../email')
 
const TRANSLATIONS = {
  en: {
    subject: 'A new API key was created on Browsway Flows',
    title: 'New API key created',
    intro: 'An API key was just created from your account.',
    name: 'Name',
    createdAt: 'Created',
    expiresAt: 'Expires',
    never: 'Never',
    manage: 'Manage your API keys',
    notYou: 'If you did not create this key, revoke it right away and change your password.',
    locale: 'en-GB'
  },
  fr: {
    subject: 'Une nouvelle clé API a été créée sur Browsway Flows',
    title: 'Nouvelle clé API créée',
    intro: 'Une clé API vient d’être créée depuis votre compte.',
    name: 'Nom',
    createdAt: 'Créée le',
    expiresAt: 'Expire le',
    never: 'Jamais',
    manage: 'Gérer vos clés API',
    notYou:
      'Si vous n’êtes pas à l’origine de cette clé, révoquez-la immédiatement et changez votre mot de passe.',
    locale: 'fr-FR'
  },
  es: {
    subject: 'Se ha creado una nueva clave API en Browsway Flows',
    title: 'Nueva clave API creada',
    intro: 'Se acaba de crear una clave API desde tu cuenta.',
    name: 'Nombre',
    createdAt: 'Creada el',
    expiresAt: 'Caduca el',
    never: 'Nunca',
    manage: 'Gestionar tus claves API',
    notYou: 'Si no has creado esta clave, revócala de inmediato y cambia tu contraseña.',
    locale: 'es-ES'
  }
}
 
/**
 * @param {string} iso
 * @param {string} locale
 * @returns {string}
 */
function formatDate(iso, locale) {
  // The recipient's timezone is unknown server-side, so the date says UTC explicitly
  const formatted = new Date(iso).toLocaleString(locale, {
    dateStyle: 'long',
    timeStyle: 'short',
    timeZone: 'UTC'
  })
  return `${formatted} UTC`
}
 
/**
 * @param {{ lang: 'en'|'fr'|'es', keyName: string, createdAt: string, expiresAt?: string|null, appUrl: string, flowSlug?: string }} params
 *   `flowSlug`: the key's flow, whose API keys page the email links to
 *   (the flows list when unknown)
 * @returns {{ subject: string, html: string, text: string }}
 */
function renderApiKeyCreatedEmail({ lang, keyName, createdAt, expiresAt, appUrl, flowSlug }) {
  const t = TRANSLATIONS[lang] || TRANSLATIONS.en
  const created = formatDate(createdAt, t.locale)
  const expires = expiresAt ? formatDate(expiresAt, t.locale) : t.never
  const manageUrl = flowSlug
    ? `${appUrl}/flows/${encodeURIComponent(flowSlug)}/api-keys`
    : `${appUrl}/flows`
 
  const text = [
    t.intro,
    '',
    `${t.name}: ${keyName}`,
    `${t.createdAt}: ${created}`,
    `${t.expiresAt}: ${expires}`,
    '',
    `${t.manage}: ${manageUrl}`,
    '',
    t.notYou
  ].join('\n')
 
  const row = (label, value) =>
    `<tr><td style="color: #64748b; padding: 4px 12px 4px 0;">${label}</td><td style="color: #1e293b; font-weight: bold;">${escapeHtml(value)}</td></tr>`
 
  const html = `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; background-color: #f9f9f9;">
  <div style="background-color: #ffffff; border-radius: 10px; padding: 30px 20px;">
    <h1 style="color: #689909; margin: 0 0 20px; font-size: 24px;">${t.title}</h1>
    <p style="color: #475569; line-height: 1.6;">${t.intro}</p>
    <table style="margin: 20px 0; font-size: 14px;">
      ${row(t.name, keyName)}
      ${row(t.createdAt, created)}
      ${row(t.expiresAt, expires)}
    </table>
    <p><a href="${manageUrl}" style="color: #689909;">${t.manage}</a></p>
    <p style="color: #64748b; font-size: 13px; line-height: 1.6; border-top: 1px solid #e2e8f0; padding-top: 20px;">${t.notYou}</p>
  </div>
</div>`
 
  return { subject: t.subject, html, text }
}
 
module.exports = { renderApiKeyCreatedEmail }