import { Router } from 'express'; import mailer from "../../src/mailer"; import {camelCase, capitalise} from "../../src/helpers"; const generateId = title => { return camelCase(title.split(' ').slice(0, 2)); } const buildEmail = (data, user) => { const human = [ `
  • user: ${user ? user.username : ''}
  • `, `
  • templates: ${data.templates}
  • `, ]; const tsv = [generateId(data.title) || '???']; for (let field of ['type','author','title','extra','year','fragments','comment','link']) { human.push(`
  • ${field}: ${field === 'fragments' ? `
    ${data[field]}
    `: data[field]}
  • `); tsv.push(field === 'fragments' ? (data[field].join('@').replace(/\n/g, '|')) : data[field]); } return `
    ${tsv.join('\t')}
    `; } const router = Router(); router.post('/sources/submit/:locale', async (req, res) => { const emailBody = buildEmail(req.body, req.user); for (let admin of process.env.MAILER_ADMINS.split(',')) { mailer(admin, `[Pronouns][${req.params.locale}] Source submission`, undefined, emailBody); } return res.json({ result: 'ok' }); }); export default router;