const Email = require('../core/email'); module.exports = app => { app.get(`/api/email/:name`, async(req, res, next) => { const args = req.query; const requiredArgs = ['clientId', 'recipient']; const argList = requiredArgs.join(','); const hasRequiredArgs = requiredArgs.every(arg => { return args[arg]; }); try { 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); } else { await email.send(); res.status(200).json({ message: 'Sent' }); } } catch (e) { next(e); } }); };