Printer: PDF stream attachments #1001

This commit is contained in:
Joan Sanchez 2019-01-23 09:33:58 +01:00
parent 9ebd7a2922
commit 664b2f1531
16 changed files with 72 additions and 83 deletions

1
.gitignore vendored
View File

@ -3,4 +3,5 @@ dist/*
npm-debug.log
.eslintcache
datasources.*.json
print.*.json
db.json

View File

@ -4,6 +4,7 @@ let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
let configPath = `/etc/salix`;
let config = require('../config/print.json');
let configFiles = [
`${appPath}/config/print.${env}.json`,
`${configPath}/print.json`,
`${configPath}/print.${env}.json`
];
@ -28,5 +29,4 @@ for (let proxyFile of proxyFiles) {
// config.proxy = proxyConf;
config.env = env;
module.exports = config;

View File

@ -47,9 +47,10 @@ module.exports = {
* @param {Object} ctx - Request context
*/
async preFetch(component, ctx) {
const preFetchData = {attachments: []};
let params = {};
let mergedData = {attachments: []};
let asyncData = {};
let data = {};
let params = {};
if (Object.keys(ctx.body).length > 0)
params = ctx.body;
@ -63,36 +64,34 @@ module.exports = {
data = component.data();
if (component.hasOwnProperty('asyncData')) {
const asyncData = await component.asyncData(ctx, params);
asyncData = await component.asyncData(ctx, params);
if (asyncData.locale) {
const locale = component.i18n.messages[asyncData.locale];
preFetchData.subject = locale.subject;
mergedData.subject = locale.subject;
}
if (asyncData.recipient)
preFetchData.recipient = asyncData.recipient;
const mergedData = {...data, ...asyncData};
component.data = function data() {
return mergedData;
};
}
if (data && data.hasOwnProperty('attachments')) {
const fileNames = data.attachments;
fileNames.forEach(attachment => {
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}/${component.name}`;
let fileSrc = componentPath + attachment;
let fileSrc = componentPath + file;
if (attachment.slice(0, 4) === 'http' || attachment.slice(0, 4) === 'https')
fileSrc = attachment;
if (file.slice(0, 4) === 'http' || file.slice(0, 4) === 'https')
fileSrc = file;
const fileName = attachment.split('/').pop();
preFetchData.attachments.push({
const fileName = file.split('/').pop();
mergedData.attachments.push({
filename: fileName,
path: fileSrc,
cid: attachment,
cid: file,
});
});
}
@ -108,14 +107,14 @@ module.exports = {
return Promise.all(promises).then(results => {
results.forEach(result => {
result.attachments.forEach(atth => {
preFetchData.attachments.push(atth);
mergedData.attachments.push(atth);
});
});
return preFetchData;
return mergedData;
});
}
return preFetchData;
return mergedData;
},
async attachAssets(component) {

View File

@ -1,21 +0,0 @@
module.exports = app => {
process.on('uncaughtException', err => {
console.error(`Caught exception: ${err}`);
});
process.on('warning', () => {
console.error(`My warning err`);
});
app.use(function(error, request, response, next) {
if (!error.httpStatusCode)
return next(error);
response.status(error.httpStatusCode);
response.json({
httpStatusCode: error.httpStatusCode,
name: error.name,
message: error.message,
});
});
};

View File

@ -43,21 +43,29 @@ module.exports = {
* @param {Object} ctx - Request context
*/
async preFetch(component, ctx) {
let mergedData = {};
let asyncData = {};
let data = {};
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')) {
const fetch = await component.asyncData(ctx, ctx.body);
const mergedData = {...data, ...fetch};
if (component.hasOwnProperty('asyncData'))
asyncData = await component.asyncData(ctx, params);
component.data = function data() {
return mergedData;
};
}
mergedData = Object.assign(mergedData, data, asyncData);
component.data = function data() {
return mergedData;
};
if (component.components) {
const components = component.components;

View File

@ -4,7 +4,6 @@ const UserException = require(`${appPath}/lib/exceptions/userException`);
module.exports = {
name: 'delivery-note',
async asyncData(ctx, params) {
console.log(params);
const promises = [];
const dataIndex = promises.push(this.methods.fetchData()) - 1;
const itemsIndex = promises.push(this.methods.fetchItems()) - 1;

View File

@ -1,21 +1,21 @@
module.exports = {
name: 'email-footer',
asyncData(ctx, params) {
asyncData(ctx) {
return {
isPreview: ctx.method === 'GET',
};
},
created() {
const embeded = [];
this.attachments.map((attachment) => {
const src = this.isPreview ? attachment : `cid:${attachment}`;
embeded[attachment] = src;
this.files.map(file => {
const src = this.isPreview ? file : `cid:${file}`;
embeded[file] = src;
});
this.embeded = embeded;
},
data() {
return {
attachments: [
files: [
'/assets/images/facebook.png',
'/assets/images/twitter.png',
'/assets/images/youtube.png',

View File

@ -1,22 +1,22 @@
module.exports = {
name: 'email-header',
asyncData(ctx, params) {
asyncData(ctx) {
return {
isPreview: ctx.method === 'GET',
};
},
created() {
const embeded = [];
this.attachments.map((attachment) => {
const src = this.isPreview ? attachment : `cid:${attachment}`;
embeded[attachment] = src;
this.files.map(file => {
const src = this.isPreview ? file : `cid:${file}`;
embeded[file] = src;
});
this.embeded = embeded;
},
data() {
return {
attachments: ['/assets/images/logo.png'],
files: ['/assets/images/logo.png'],
};
},
};

View File

@ -13,6 +13,9 @@ module.exports = {
if (!params.clientFk)
throw new UserException('No client id specified');
if (!params.companyFk)
throw new UserException('No company id specified');
return this.methods.fetchClientData(params.clientFk, params.companyFk)
.then(([result]) => {
return Object.assign(data, result[0]);

View File

@ -1,11 +1,14 @@
const database = require(`${appPath}/lib/database`);
const reportEngine = require(`${appPath}/lib/reportEngine.js`);
const emailHeader = require('../email-header');
const emailFooter = require('../email-footer');
const UserException = require(`${appPath}/lib/exceptions/userException`);
module.exports = {
name: 'letter-debtor-st',
async asyncData(ctx, params) {
const promises = [];
const data = {
isPreview: ctx.method === 'GET',
};
@ -16,23 +19,24 @@ module.exports = {
if (!params.companyFk)
throw new UserException('No company id specified');
return this.methods.fetchClientData(params.clientFk, params.companyFk)
.then(([[result]]) => {
if (!result) throw new UserException('Client data not found');
promises.push(reportEngine.toPdf('delivery-note', ctx));
promises.push(this.methods.fetchClient(params.clientFk, params.companyFk));
return Object.assign(data, result);
});
return Promise.all(promises).then(result => {
const stream = result[0];
const [[client]] = result[1];
Object.assign(data, client);
Object.assign(data, {attachments: [{filename: 'delivery-note.pdf', content: stream}]});
return data;
});
},
created() {
this.$i18n.locale = this.locale;
},
data() {
return {
attachments: ['http://localhost:5000/report/delivery-note'],
};
},
methods: {
fetchClientData(clientFk, companyFk) {
fetchClient(clientFk, companyFk) {
return database.pool.query(`
SELECT
u.lang locale,
@ -47,7 +51,7 @@ module.exports = {
JOIN bankEntity be ON be.id = sa.bankEntityFk
JOIN account.user u ON u.id = c.id
WHERE c.id = ? AND cny.id = ?`, [clientFk, companyFk]);
},
}
},
computed: {
accountAddress: function() {

View File

@ -23,7 +23,7 @@ module.exports = {
},
data() {
return {
attachments: ['/assets/files/model.ezp'],
files: ['/assets/files/model.ezp'],
};
},
methods: {

View File

@ -1,18 +1,14 @@
const database = require('./lib/database');
const smtp = require('./lib/smtp');
module.exports = app => {
global.appPath = __dirname;
process.env.OPENSSL_CONF = '/etc/ssl/';
// Init database instance
database.init();
require('./lib/database').init();
// Init SMTP Instance
smtp.init();
require('./lib/smtp').init();
require('./lib/router')(app);
// require('./lib/errorHandler')(app);
};