salix/print/methods/email.js

33 lines
921 B
JavaScript
Raw Normal View History

const Email = require('../core/email');
module.exports = app => {
2019-11-19 09:32:09 +00:00
app.get(`/api/email/:name`, async(req, res, next) => {
const args = req.query;
2019-11-19 09:32:09 +00:00
const requiredArgs = ['clientId', 'recipient'];
const argList = requiredArgs.join(',');
const hasRequiredArgs = requiredArgs.every(arg => {
return args[arg];
});
try {
2019-11-19 09:32:09 +00:00
if (!hasRequiredArgs)
throw new Error(`Required properties not found [${argList}]`);
const email = new Email(req.params.name, args);
if (args.isPreview === 'true') {
const rendered = await email.render();
res.send(rendered);
2019-11-19 09:32:09 +00:00
} else {
await email.send();
2019-11-19 09:32:09 +00:00
res.status(200).json({
message: 'Sent'
});
}
} catch (e) {
next(e);
}
});
};