214 lines
6.3 KiB
JavaScript
214 lines
6.3 KiB
JavaScript
const path = require('path');
|
|
const smtp = require('./smtp');
|
|
const Component = require('./component');
|
|
const Report = require('./report');
|
|
|
|
if (!process.env.OPENSSL_CONF)
|
|
process.env.OPENSSL_CONF = '/etc/ssl/';
|
|
|
|
class Email extends Component {
|
|
constructor(name, args) {
|
|
super(name);
|
|
|
|
this.args = args;
|
|
}
|
|
|
|
get path() {
|
|
return `../templates/email/${this.name}`;
|
|
}
|
|
|
|
|
|
get subject() {
|
|
return null;
|
|
}
|
|
|
|
async send() {
|
|
const instance = this.build();
|
|
const rendered = await this.render();
|
|
const attachments = [];
|
|
const getAttachments = async(componentPath, files) => {
|
|
for (file of files) {
|
|
const fileCopy = Object.assign({}, file);
|
|
if (fileCopy.cid) {
|
|
const templatePath = `${componentPath}/${file.path}`;
|
|
const fullFilePath = path.resolve(__dirname, templatePath);
|
|
|
|
fileCopy.path = path.resolve(__dirname, fullFilePath);
|
|
} else {
|
|
const reportName = fileCopy.filename.replace('.pdf', '');
|
|
const report = new Report(reportName, this.args);
|
|
fileCopy.content = await report.toPdfStream();
|
|
}
|
|
|
|
attachments.push(fileCopy);
|
|
}
|
|
};
|
|
|
|
if (instance.components) {
|
|
const components = instance.components;
|
|
for (let componentName in components) {
|
|
const component = components[componentName];
|
|
const componentPath = `../components/${componentName}`;
|
|
await getAttachments(componentPath, component.attachments);
|
|
}
|
|
}
|
|
|
|
|
|
if (this.attachments)
|
|
await getAttachments(this.path, this.attachments);
|
|
/*
|
|
this.attachments.forEach(file => {
|
|
const fileCopy = Object.assign({}, file);
|
|
if (fileCopy.path) {
|
|
const templatePath = `${this.path}/${file.path}`;
|
|
const fullFilePath = path.resolve(__dirname, templatePath);
|
|
|
|
fileCopy.path = path.resolve(__dirname, fullFilePath);
|
|
}
|
|
|
|
attachments.push(fileCopy);
|
|
}); */
|
|
|
|
const options = {
|
|
to: this.args.recipient,
|
|
subject: 'Test',
|
|
html: rendered,
|
|
attachments: attachments
|
|
};
|
|
|
|
return smtp.send(options);
|
|
}
|
|
}
|
|
|
|
module.exports = Email;
|
|
|
|
|
|
/* module.exports = {
|
|
|
|
path: `${appPath}/report`,
|
|
|
|
async render(name, ctx) {
|
|
const component = require(`${this.path}/${name}`);
|
|
const result = await this.preFetch(component, ctx);
|
|
const i18n = new VueI18n({
|
|
locale: 'es',
|
|
fallbackLocale
|
|
});
|
|
const app = new Vue({i18n,
|
|
render: h => h(result.component)});
|
|
|
|
return renderer.renderToString(app).then(renderedHtml => {
|
|
return {
|
|
html: renderedHtml,
|
|
data: result.mergedData,
|
|
};
|
|
});
|
|
},
|
|
|
|
async preFetch(orgComponent, ctx) {
|
|
let component = Object.create(orgComponent);
|
|
let mergedData = {attachments: []};
|
|
let asyncData = {};
|
|
let data = {};
|
|
let params = {};
|
|
|
|
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 (orgComponent.hasOwnProperty('data'))
|
|
data = orgComponent.data();
|
|
|
|
if (orgComponent.hasOwnProperty('asyncData')) {
|
|
asyncData = await orgComponent.asyncData(ctx, params);
|
|
|
|
if (asyncData.locale) {
|
|
let locale = component.i18n.messages[asyncData.locale];
|
|
|
|
if (!locale)
|
|
locale = component.i18n.messages[fallbackLocale];
|
|
|
|
mergedData.subject = locale.subject;
|
|
}
|
|
}
|
|
|
|
mergedData = Object.assign(mergedData, data, asyncData);
|
|
|
|
component.data = function data() {
|
|
return mergedData;
|
|
};
|
|
|
|
if (data.hasOwnProperty('files')) {
|
|
const files = data.files;
|
|
files.forEach(file => {
|
|
const componentPath = `${this.path}/${orgComponent.name}`;
|
|
let fileSrc = componentPath + file;
|
|
|
|
if (file.slice(0, 4) === 'http' || file.slice(0, 5) === 'https')
|
|
fileSrc = file;
|
|
|
|
const fileName = file.split('/').pop();
|
|
mergedData.attachments.push({
|
|
filename: fileName,
|
|
path: fileSrc,
|
|
cid: file,
|
|
});
|
|
});
|
|
}
|
|
|
|
const components = orgComponent.components;
|
|
|
|
if (components) {
|
|
const promises = [];
|
|
const childNames = [];
|
|
component.components = {};
|
|
|
|
Object.keys(components).forEach(childName => {
|
|
childNames.push(childName);
|
|
promises.push(this.preFetch(components[childName], ctx));
|
|
});
|
|
|
|
await Promise.all(promises).then(results => {
|
|
results.forEach((result, i) => {
|
|
result.mergedData.attachments.forEach(atth => {
|
|
mergedData.attachments.push(atth);
|
|
});
|
|
|
|
component.components[childNames[i]] = result.component;
|
|
});
|
|
});
|
|
}
|
|
|
|
return {component, mergedData};
|
|
},
|
|
|
|
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/index.js`;
|
|
|
|
const template = await fs.readFile(templatePath, 'utf8');
|
|
const css = require(stylePath);
|
|
|
|
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);
|
|
},
|
|
};
|
|
*/
|