salix/print/core/email.js

214 lines
6.3 KiB
JavaScript
Raw Normal View History

2019-10-29 06:46:44 +00:00
const path = require('path');
2019-01-22 08:55:35 +00:00
const smtp = require('./smtp');
2019-10-29 06:46:44 +00:00
const Component = require('./component');
const Report = require('./report');
2019-01-22 08:55:35 +00:00
if (!process.env.OPENSSL_CONF)
process.env.OPENSSL_CONF = '/etc/ssl/';
2019-10-29 06:46:44 +00:00
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;
2019-01-25 14:08:11 +00:00
2019-10-29 06:46:44 +00:00
/* module.exports = {
2019-01-22 08:55:35 +00:00
2019-01-25 14:08:11 +00:00
path: `${appPath}/report`,
2019-01-22 08:55:35 +00:00
async render(name, ctx) {
const component = require(`${this.path}/${name}`);
2019-01-25 14:08:11 +00:00
const result = await this.preFetch(component, ctx);
const i18n = new VueI18n({
locale: 'es',
fallbackLocale
2019-01-22 08:55:35 +00:00
});
2019-01-25 14:08:11 +00:00
const app = new Vue({i18n,
render: h => h(result.component)});
2019-01-22 08:55:35 +00:00
return renderer.renderToString(app).then(renderedHtml => {
return {
html: renderedHtml,
2019-01-25 14:08:11 +00:00
data: result.mergedData,
2019-01-22 08:55:35 +00:00
};
});
},
2019-01-25 14:08:11 +00:00
async preFetch(orgComponent, ctx) {
let component = Object.create(orgComponent);
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);
2019-01-25 14:08:11 +00:00
if (orgComponent.hasOwnProperty('data'))
data = orgComponent.data();
2019-01-22 08:55:35 +00:00
2019-01-25 14:08:11 +00:00
if (orgComponent.hasOwnProperty('asyncData')) {
asyncData = await orgComponent.asyncData(ctx, params);
2019-01-22 08:55:35 +00:00
if (asyncData.locale) {
2019-01-25 14:08:11 +00:00
let locale = component.i18n.messages[asyncData.locale];
if (!locale)
locale = component.i18n.messages[fallbackLocale];
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-25 14:08:11 +00:00
const componentPath = `${this.path}/${orgComponent.name}`;
2019-01-23 08:33:58 +00:00
let fileSrc = componentPath + file;
2019-01-22 08:55:35 +00:00
2019-01-28 11:28:22 +00:00
if (file.slice(0, 4) === 'http' || file.slice(0, 5) === 'https')
2019-01-23 08:33:58 +00:00
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
});
});
}
2019-01-25 14:08:11 +00:00
const components = orgComponent.components;
if (components) {
2019-01-22 08:55:35 +00:00
const promises = [];
2019-01-25 14:08:11 +00:00
const childNames = [];
component.components = {};
2019-01-22 08:55:35 +00:00
2019-01-25 14:08:11 +00:00
Object.keys(components).forEach(childName => {
childNames.push(childName);
promises.push(this.preFetch(components[childName], ctx));
2019-01-22 08:55:35 +00:00
});
2019-01-25 14:08:11 +00:00
await Promise.all(promises).then(results => {
results.forEach((result, i) => {
result.mergedData.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-25 14:08:11 +00:00
component.components[childNames[i]] = result.component;
2019-01-22 08:55:35 +00:00
});
});
}
2019-01-25 14:08:11 +00:00
return {component, 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/index.js`;
2019-01-22 08:55:35 +00:00
const template = await fs.readFile(templatePath, 'utf8');
const css = require(stylePath);
2019-01-22 08:55:35 +00:00
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);
},
};
2019-10-29 06:46:44 +00:00
*/