diff --git a/.gitignore b/.gitignore index 9a4c04b956..50d3f3f91a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ dist/* npm-debug.log .eslintcache datasources.*.json +print.*.json db.json \ No newline at end of file diff --git a/print/lib/config.js b/print/lib/config.js index c14538f3e0..47e58ec777 100644 --- a/print/lib/config.js +++ b/print/lib/config.js @@ -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; diff --git a/print/lib/emailEngine.js b/print/lib/emailEngine.js index 0ae947137a..cf4eceba19 100644 --- a/print/lib/emailEngine.js +++ b/print/lib/emailEngine.js @@ -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) { diff --git a/print/lib/errorHandler.js b/print/lib/errorHandler.js deleted file mode 100644 index ec9bc98c26..0000000000 --- a/print/lib/errorHandler.js +++ /dev/null @@ -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, - }); - }); -}; diff --git a/print/lib/reportEngine.js b/print/lib/reportEngine.js index 23b1e199ee..0a818acd34 100644 --- a/print/lib/reportEngine.js +++ b/print/lib/reportEngine.js @@ -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; diff --git a/print/reports/delivery-note/index.js b/print/reports/delivery-note/index.js index 07f77ffd0e..34c80b1f82 100755 --- a/print/reports/delivery-note/index.js +++ b/print/reports/delivery-note/index.js @@ -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; diff --git a/print/reports/email-footer/index.js b/print/reports/email-footer/index.js index 07fc857dde..893e7276ee 100755 --- a/print/reports/email-footer/index.js +++ b/print/reports/email-footer/index.js @@ -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', diff --git a/print/reports/email-header/index.js b/print/reports/email-header/index.js index 87b2d1aa9d..93768e86fd 100755 --- a/print/reports/email-header/index.js +++ b/print/reports/email-header/index.js @@ -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'], }; }, }; diff --git a/print/reports/letter-debtor-nd/assets/files/model.ezp b/print/reports/letter-debtor-nd/assets/files/model.ezp deleted file mode 100755 index 297df3d214..0000000000 Binary files a/print/reports/letter-debtor-nd/assets/files/model.ezp and /dev/null differ diff --git a/print/reports/letter-debtor-nd/assets/files/model2.ezp b/print/reports/letter-debtor-nd/assets/files/model2.ezp deleted file mode 100755 index 297df3d214..0000000000 Binary files a/print/reports/letter-debtor-nd/assets/files/model2.ezp and /dev/null differ diff --git a/print/reports/letter-debtor-nd/index.js b/print/reports/letter-debtor-nd/index.js index bcf7880544..77eec93512 100755 --- a/print/reports/letter-debtor-nd/index.js +++ b/print/reports/letter-debtor-nd/index.js @@ -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]); diff --git a/print/reports/letter-debtor-st/assets/files/model.ezp b/print/reports/letter-debtor-st/assets/files/model.ezp deleted file mode 100755 index 297df3d214..0000000000 Binary files a/print/reports/letter-debtor-st/assets/files/model.ezp and /dev/null differ diff --git a/print/reports/letter-debtor-st/assets/files/model2.ezp b/print/reports/letter-debtor-st/assets/files/model2.ezp deleted file mode 100755 index 297df3d214..0000000000 Binary files a/print/reports/letter-debtor-st/assets/files/model2.ezp and /dev/null differ diff --git a/print/reports/letter-debtor-st/index.js b/print/reports/letter-debtor-st/index.js index 7fb41092ed..8fb51e408b 100755 --- a/print/reports/letter-debtor-st/index.js +++ b/print/reports/letter-debtor-st/index.js @@ -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() { diff --git a/print/reports/printer-setup/index.js b/print/reports/printer-setup/index.js index d32ea9c18b..b15542fe42 100755 --- a/print/reports/printer-setup/index.js +++ b/print/reports/printer-setup/index.js @@ -23,7 +23,7 @@ module.exports = { }, data() { return { - attachments: ['/assets/files/model.ezp'], + files: ['/assets/files/model.ezp'], }; }, methods: { diff --git a/print/server.js b/print/server.js index 61eeb32978..31830009df 100644 --- a/print/server.js +++ b/print/server.js @@ -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); };