This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/server/routes/sources.js

37 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-10-31 13:33:59 -07:00
import { Router } from 'express';
import mailer from "../../src/mailer";
2020-11-09 06:39:18 -08:00
import {camelCase, capitalise} from "../../src/helpers";
const generateId = title => {
return camelCase(title.split(' ').slice(0, 2));
}
2020-10-31 13:33:59 -07:00
const buildEmail = (data, user) => {
const human = [
`<li><strong>user:</strong> ${user ? user.username : ''}</li>`,
`<li><strong>templates:</strong> ${data.templates}</li>`,
];
2020-11-09 06:39:18 -08:00
const tsv = [generateId(data.title) || '???'];
2020-10-31 13:33:59 -07:00
for (let field of ['type','author','title','extra','year','fragments','comment','link']) {
human.push(`<li><strong>${field}:</strong> ${field === 'fragments' ? `<pre>${data[field]}</pre>`: data[field]}</li>`);
tsv.push(field === 'fragments' ? (data[field].join('@').replace(/\n/g, '|')) : data[field]);
}
return `<ul>${human.join('')}</ul><pre>${tsv.join('\t')}</pre>`;
}
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;