salix/print/core/email.js

97 lines
2.8 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');
2020-05-20 14:23:53 +00:00
// const db = require('./database');
2019-10-31 11:43:04 +00:00
const config = require('./config');
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}`;
}
2019-10-31 11:43:04 +00:00
async getSubject() {
2020-05-20 14:23:53 +00:00
const lang = this.args.auth.lang;
2019-10-31 11:43:04 +00:00
const locale = this.locale.messages;
2020-05-20 14:23:53 +00:00
const userLocale = locale[lang];
2019-10-31 11:43:04 +00:00
if (!userLocale) {
const fallbackLocale = config.i18n.fallbackLocale;
return locale[fallbackLocale].subject;
}
return userLocale.subject;
}
2020-05-20 14:23:53 +00:00
/* async getLang() {
2019-10-31 11:43:04 +00:00
const clientId = this.args.clientId;
const lang = await db.findOne(`
2020-05-20 14:23:53 +00:00
SELECT lang FROM account.user
2019-10-31 11:43:04 +00:00
WHERE id = ?`, [clientId]).then(rows => {
return rows.lang;
});
this.lang = lang;
2020-05-20 14:23:53 +00:00
} */
2019-10-29 06:46:44 +00:00
async send() {
const instance = this.build();
2020-05-20 14:23:53 +00:00
const component = this.component();
2019-10-29 06:46:44 +00:00
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];
2019-10-31 11:43:04 +00:00
const componentPath = `./components/${componentName}`;
2019-10-29 06:46:44 +00:00
await getAttachments(componentPath, component.attachments);
}
}
if (this.attachments)
await getAttachments(this.path, this.attachments);
2020-05-20 14:23:53 +00:00
console.log(await component.getLang(101));
2019-10-31 11:43:04 +00:00
const localeSubject = await this.getSubject();
2019-10-29 06:46:44 +00:00
const options = {
to: this.args.recipient,
2020-05-20 14:23:53 +00:00
replyTo: this.args.auth.email,
2019-10-31 11:43:04 +00:00
subject: localeSubject,
2019-10-29 06:46:44 +00:00
html: rendered,
attachments: attachments
};
return smtp.send(options);
}
}
module.exports = Email;