2019-01-22 08:55:35 +00:00
|
|
|
const Vue = require('vue');
|
|
|
|
const VueI18n = require('vue-i18n');
|
|
|
|
const renderer = require('vue-server-renderer').createRenderer();
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const juice = require('juice');
|
|
|
|
const smtp = require('./smtp');
|
|
|
|
const i18n = new VueI18n({
|
|
|
|
locale: 'es',
|
|
|
|
});
|
|
|
|
|
|
|
|
Vue.use(VueI18n);
|
|
|
|
|
|
|
|
if (!process.env.OPENSSL_CONF)
|
|
|
|
process.env.OPENSSL_CONF = '/etc/ssl/';
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
path: `${appPath}/reports`,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a report component
|
|
|
|
*
|
|
|
|
* @param {String} name - Report name
|
|
|
|
* @param {Object} ctx - Request context
|
|
|
|
*/
|
|
|
|
async render(name, ctx) {
|
|
|
|
const component = require(`${this.path}/${name}`);
|
|
|
|
const prefetchedData = await this.preFetch(component, ctx);
|
|
|
|
|
|
|
|
const app = new Vue({
|
|
|
|
i18n,
|
|
|
|
render: h => h(component),
|
|
|
|
});
|
|
|
|
|
|
|
|
return renderer.renderToString(app).then(renderedHtml => {
|
|
|
|
return {
|
|
|
|
html: renderedHtml,
|
|
|
|
data: prefetchedData,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefetch all component data from asyncData method
|
|
|
|
*
|
|
|
|
* @param {Object} component - Component object
|
|
|
|
* @param {Object} ctx - Request context
|
|
|
|
*/
|
|
|
|
async preFetch(component, ctx) {
|
2019-01-23 08:33:58 +00:00
|
|
|
let mergedData = {attachments: []};
|
|
|
|
let asyncData = {};
|
2019-01-22 08:55:35 +00:00
|
|
|
let data = {};
|
2019-01-23 08:33:58 +00:00
|
|
|
let params = {};
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
if (Object.keys(ctx.body).length > 0)
|
|
|
|
params = ctx.body;
|
|
|
|
|
|
|
|
if (Object.keys(ctx.query).length > 0)
|
|
|
|
params = ctx.query;
|
|
|
|
|
|
|
|
await this.attachAssets(component);
|
|
|
|
|
|
|
|
if (component.hasOwnProperty('data'))
|
|
|
|
data = component.data();
|
|
|
|
|
|
|
|
if (component.hasOwnProperty('asyncData')) {
|
2019-01-23 08:33:58 +00:00
|
|
|
asyncData = await component.asyncData(ctx, params);
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
if (asyncData.locale) {
|
|
|
|
const locale = component.i18n.messages[asyncData.locale];
|
2019-01-23 08:33:58 +00:00
|
|
|
mergedData.subject = locale.subject;
|
2019-01-22 08:55:35 +00:00
|
|
|
}
|
2019-01-23 08:33:58 +00:00
|
|
|
}
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-23 08:33:58 +00:00
|
|
|
mergedData = Object.assign(mergedData, data, asyncData);
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-23 08:33:58 +00:00
|
|
|
component.data = function data() {
|
|
|
|
return mergedData;
|
|
|
|
};
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-23 08:33:58 +00:00
|
|
|
if (data.hasOwnProperty('files')) {
|
|
|
|
const files = data.files;
|
|
|
|
files.forEach(file => {
|
2019-01-22 08:55:35 +00:00
|
|
|
const componentPath = `${this.path}/${component.name}`;
|
2019-01-23 08:33:58 +00:00
|
|
|
let fileSrc = componentPath + file;
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-23 08:33:58 +00:00
|
|
|
if (file.slice(0, 4) === 'http' || file.slice(0, 4) === 'https')
|
|
|
|
fileSrc = file;
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-23 08:33:58 +00:00
|
|
|
const fileName = file.split('/').pop();
|
|
|
|
mergedData.attachments.push({
|
2019-01-22 08:55:35 +00:00
|
|
|
filename: fileName,
|
|
|
|
path: fileSrc,
|
2019-01-23 08:33:58 +00:00
|
|
|
cid: file,
|
2019-01-22 08:55:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (component.components) {
|
|
|
|
const components = component.components;
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
Object.keys(components).forEach(component => {
|
|
|
|
promises.push(this.preFetch(components[component], ctx));
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(promises).then(results => {
|
|
|
|
results.forEach(result => {
|
|
|
|
result.attachments.forEach(atth => {
|
2019-01-23 08:33:58 +00:00
|
|
|
mergedData.attachments.push(atth);
|
2019-01-22 08:55:35 +00:00
|
|
|
});
|
|
|
|
});
|
2019-01-23 08:33:58 +00:00
|
|
|
return mergedData;
|
2019-01-22 08:55:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-23 08:33:58 +00:00
|
|
|
return mergedData;
|
2019-01-22 08:55:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async attachAssets(component) {
|
|
|
|
const localePath = `${this.path}/${component.name}/locale.js`;
|
|
|
|
const templatePath = `${this.path}/${component.name}/index.html`;
|
|
|
|
const stylePath = `${this.path}/${component.name}/assets/css/style.css`;
|
|
|
|
|
|
|
|
const template = await fs.readFile(templatePath, 'utf8');
|
|
|
|
const css = await fs.readFile(stylePath, 'utf8');
|
|
|
|
|
|
|
|
component.i18n = require(localePath);
|
|
|
|
component.template = juice.inlineContent(template, css);
|
|
|
|
},
|
|
|
|
|
|
|
|
async toEmail(name, ctx) {
|
|
|
|
const rendered = await this.render(name, ctx);
|
|
|
|
const data = rendered.data;
|
|
|
|
const options = {
|
|
|
|
to: data.recipient,
|
|
|
|
subject: data.subject,
|
|
|
|
html: rendered.html,
|
|
|
|
attachments: data.attachments,
|
|
|
|
};
|
|
|
|
return smtp.send(options);
|
|
|
|
},
|
|
|
|
};
|