salix/print/core/router.js

134 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-01-22 08:55:35 +00:00
const express = require('express');
2019-10-24 05:41:54 +00:00
const path = require('path');
const fs = require('fs');
2019-10-29 06:46:44 +00:00
const Report = require('./report');
const Email = require('./email');
2019-10-24 05:41:54 +00:00
const templatesPath = path.resolve(__dirname, '../templates');
2019-10-29 06:46:44 +00:00
const componentsPath = path.resolve(__dirname, './components');
2019-01-22 08:55:35 +00:00
module.exports = app => {
2019-10-29 06:46:44 +00:00
/**
* Serve component static files
*/
const componentsDir = fs.readdirSync(componentsPath);
componentsDir.forEach(componentName => {
const componentDir = path.join(componentsPath, '/', componentName);
const assetsDir = `${componentDir}/assets`;
app.use(`/api/assets/${componentName}`, express.static(assetsDir));
});
2019-10-24 05:41:54 +00:00
/**
* Serve static files
*/
const templatesDir = fs.readdirSync(templatesPath);
templatesDir.forEach(directory => {
const templateTypeDir = path.join(templatesPath, '/', directory);
const templates = fs.readdirSync(templateTypeDir);
templates.forEach(templateName => {
const templateDir = path.join(templatesPath, '/', directory, '/', templateName);
const assetsDir = `${templateDir}/assets`;
2019-10-29 06:46:44 +00:00
app.use(`/api/assets/${templateName}`, express.static(assetsDir));
2019-10-24 05:41:54 +00:00
});
});
app.get(`/api/report/:name`, async(req, res) => {
const args = Object.assign({}, req.body, req.query);
const report = new Report(req.params.name, args);
const stream = await report.toPdfStream();
res.setHeader('Content-type', 'application/pdf');
stream.pipe(res);
});
2019-10-29 06:46:44 +00:00
app.get(`/api/email/:name`, async(req, res) => {
const args = req.query;
const requiredArgs = ['userId'];
const hasRequiredArgs = requiredArgs.every(arg => {
return args[arg];
2019-10-24 05:41:54 +00:00
});
2019-10-29 06:46:44 +00:00
if (!hasRequiredArgs)
res.json({message: 'userId'});
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'});
}
2019-10-24 05:41:54 +00:00
});
2019-10-29 06:46:44 +00:00
/*
2019-10-24 05:41:54 +00:00
app.post(`/api/email/${name}`, (request, response, next) => {
emailEngine.toEmail(name, request).then(() => {
response.status(200).json({status: 200});
}).catch(e => {
next(e);
});
}); */
/* routes.forEach(route => {
if (route.type === 'email')
registerEmail(route.name);
else if (route.type === 'report')
registerReport(route.name);
const staticPath = this.path + `/${route.name}/assets`;
}); */
2019-01-22 08:55:35 +00:00
/**
* Enables a report
*
* @param {String} name - Report state path
*/
function registerReport(name) {
if (!name) throw new Error('Report name required');
2019-10-24 05:41:54 +00:00
app.get(`/api/report/${name}`, async(request, response) => {
const args = Object.assign({}, request.body, request.query);
const report = new Report(name, args);
const stream = await report.toPdf();
response.setHeader('Content-type', 'application/pdf');
stream.pipe(response);
2019-01-22 08:55:35 +00:00
});
}
/**
* Enables a email
*
* @param {String} name - Report state path
*/
function registerEmail(name) {
if (!name) throw new Error('Email name required');
2019-01-28 11:28:22 +00:00
app.get(`/api/email/${name}`, (request, response, next) => {
2019-01-22 08:55:35 +00:00
emailEngine.render(name, request).then(rendered => {
response.send(rendered.html);
}).catch(e => {
next(e);
});
});
2019-01-28 11:28:22 +00:00
app.post(`/api/email/${name}`, (request, response, next) => {
2019-01-22 08:55:35 +00:00
emailEngine.toEmail(name, request).then(() => {
response.status(200).json({status: 200});
}).catch(e => {
next(e);
});
});
}
};