From 0372d44415a7015d0a9bed40b8cc37146db3a6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Andr=C3=A9s?= Date: Tue, 4 Apr 2023 15:00:28 +0200 Subject: [PATCH] fixes #5529 Fixes: missing awaits, buffer-to-file --- .../back/methods/invoiceOut/createPdf.js | 4 +- .../front/global-invoicing/index.js | 23 ++++--- .../front/global-invoicing/locale/es.yml | 3 +- modules/ticket/back/methods/ticket/closure.js | 2 +- print/core/cluster.js | 60 +++++++++---------- print/core/report.js | 39 ++++++------ print/core/storage.js | 16 +---- 7 files changed, 72 insertions(+), 75 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/createPdf.js b/modules/invoiceOut/back/methods/invoiceOut/createPdf.js index e56516237..23b6c9e04 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/createPdf.js +++ b/modules/invoiceOut/back/methods/invoiceOut/createPdf.js @@ -56,7 +56,7 @@ module.exports = Self => { reference: invoiceOut.ref, recipientId: invoiceOut.clientFk }); - const stream = await invoiceReport.toPdfStream(); + const buffer = await invoiceReport.toPdfStream(); const issued = invoiceOut.issued; const year = issued.getFullYear().toString(); @@ -66,7 +66,7 @@ module.exports = Self => { const fileName = `${year}${invoiceOut.ref}.pdf`; // Store invoice - print.storage.write(stream, { + await print.storage.write(buffer, { type: 'invoice', path: `${year}/${month}/${day}`, fileName: fileName diff --git a/modules/invoiceOut/front/global-invoicing/index.js b/modules/invoiceOut/front/global-invoicing/index.js index 0ac0223b4..0aa6a4a24 100644 --- a/modules/invoiceOut/front/global-invoicing/index.js +++ b/modules/invoiceOut/front/global-invoicing/index.js @@ -100,16 +100,23 @@ class Controller extends Section { }; this.$http.post(`InvoiceOuts/invoiceClient`, params) + .then(() => this.invoiceNext()) .catch(res => { - this.errors.unshift({ - address, - message: res.data.error.message - }); + const message = res.data?.error?.message || res.message; + if (res.status >= 400 && res.status < 500) { + this.errors.unshift({address, message}); + this.invoiceNext(); + } else { + this.invoicing = false; + this.status = 'done'; + throw new UserError(`Critical invoicing error, proccess stopped`); + } }) - .finally(() => { - this.addressIndex++; - this.invoiceOut(); - }); + } + + invoiceNext() { + this.addressIndex++; + this.invoiceOut(); } get nAddresses() { diff --git a/modules/invoiceOut/front/global-invoicing/locale/es.yml b/modules/invoiceOut/front/global-invoicing/locale/es.yml index 242b5a93f..5b1f7e883 100644 --- a/modules/invoiceOut/front/global-invoicing/locale/es.yml +++ b/modules/invoiceOut/front/global-invoicing/locale/es.yml @@ -17,4 +17,5 @@ Ended process: Proceso finalizado Invoice out: Facturar One client: Un solo cliente Choose a valid client: Selecciona un cliente válido -Stop: Parar \ No newline at end of file +Stop: Parar +Critical invoicing error, proccess stopped: Error crítico al facturar, proceso detenido \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket/closure.js b/modules/ticket/back/methods/ticket/closure.js index d5fa58e7b..9b3355d6c 100644 --- a/modules/ticket/back/methods/ticket/closure.js +++ b/modules/ticket/back/methods/ticket/closure.js @@ -46,7 +46,7 @@ module.exports = async function(Self, tickets, reqArgs = {}) { const fileName = `${year}${invoiceOut.ref}.pdf`; // Store invoice - storage.write(stream, { + await storage.write(stream, { type: 'invoice', path: `${year}/${month}/${day}`, fileName: fileName diff --git a/print/core/cluster.js b/print/core/cluster.js index 23b3d88e9..d54044fa2 100644 --- a/print/core/cluster.js +++ b/print/core/cluster.js @@ -4,40 +4,38 @@ const {cpus} = require('os'); module.exports = { init() { - if (!this.pool) { - Cluster.launch({ - concurrency: Cluster.CONCURRENCY_CONTEXT, - maxConcurrency: cpus().length, - puppeteerOptions: { - headless: true, - args: [ - '--no-sandbox', - '--disable-setuid-sandbox', - '--no-zygote' - ] - } - }) - .then(cluster => { - this.pool = cluster; + if (this.pool) return; + Cluster.launch({ + concurrency: Cluster.CONCURRENCY_CONTEXT, + maxConcurrency: cpus().length, + puppeteerOptions: { + headless: true, + args: [ + '--no-sandbox', + '--disable-setuid-sandbox', + '--no-zygote' + ] + } + }).then(cluster => { + this.pool = cluster; - log4js.configure({ - appenders: { - out: {type: 'stdout'} - }, - categories: {default: {appenders: ['out'], level: 'info'}}, - }); + log4js.configure({ + appenders: { + out: {type: 'stdout'} + }, + categories: {default: {appenders: ['out'], level: 'info'}}, + }); - const logger = log4js.getLogger(); + const logger = log4js.getLogger(); - cluster.on('taskerror', (err, data, willRetry) => { - if (willRetry) - logger.warn(`[Print] => ${err.message}\nThis job will be retried`); - else - logger.error(`[Print] => ${err.message}`); - }); + cluster.on('taskerror', (err, data, willRetry) => { + if (willRetry) + logger.warn(`[Print] => ${err.message}\nThis job will be retried`); + else + logger.error(`[Print] => ${err.message}`); + }); - cluster.on('queue', () => logger.info('Printing task initialized by pool')); - }); - } + cluster.on('queue', () => logger.info('Printing task initialized by pool')); + }); } }; diff --git a/print/core/report.js b/print/core/report.js index c5182d1a8..23cffac2c 100644 --- a/print/core/report.js +++ b/print/core/report.js @@ -32,28 +32,31 @@ class Report extends Component { if (fs.existsSync(fullPath)) options = require(optionsPath); - return new Promise(resolve => { + return new Promise((resolve, reject) => { Cluster.pool.queue({}, async({page}) => { - await page.emulateMediaType('screen'); - await page.setContent(template); + try { + await page.emulateMediaType('screen'); + await page.setContent(template); - const element = await page.$('#pageFooter'); + const element = await page.$('#pageFooter'); - let footer = '\n'; - if (element) { - footer = await page.evaluate(el => { - const html = el.innerHTML; - el.remove(); - return html; - }, element); + let footer = '\n'; + if (element) { + footer = await page.evaluate(el => { + const html = el.innerHTML; + el.remove(); + return html; + }, element); + } + + options.headerTemplate = '\n'; + options.footerTemplate = footer; + + const buffer = await page.pdf(options); + resolve(buffer); + } catch (err) { + reject(err); } - - options.headerTemplate = '\n'; - options.footerTemplate = footer; - - const stream = await page.pdf(options); - - resolve(stream); }); }); } diff --git a/print/core/storage.js b/print/core/storage.js index 063a2fbec..66f7ce98a 100644 --- a/print/core/storage.js +++ b/print/core/storage.js @@ -3,26 +3,14 @@ const path = require('path'); const fs = require('fs-extra'); module.exports = { - async write(stream, options) { + async write(buffer, options) { const storage = config.storage[options.type]; - if (!storage) return; const src = path.join(storage.root, options.path); const fileSrc = path.join(src, options.fileName); await fs.mkdir(src, {recursive: true}); - - const writeStream = fs.createWriteStream(fileSrc); - writeStream.on('open', () => writeStream.write(stream)); - writeStream.on('finish', () => writeStream.end()); - - return new Promise(resolve => { - writeStream.on('close', () => resolve()); - }); - }, - - load(type, data) { - + await fs.writeFile(fileSrc, buffer); } };