From bc6f9bc0744d13d90f2a93746a40f53838aa0602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Segarra=20Mart=C3=ADnez?= Date: Tue, 23 Jan 2024 23:52:45 +0100 Subject: [PATCH] refs #6174 test: new test for new method --- .../back/methods/invoiceOut/makePdfList.js | 7 +- .../methods/ticket/invoiceTicketsAndPdf.js | 14 +-- .../ticket/specs/invoiceTicketsAndPdf.spec.js | 115 ++++++++++++++++++ 3 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 modules/ticket/back/methods/ticket/specs/invoiceTicketsAndPdf.spec.js diff --git a/modules/invoiceOut/back/methods/invoiceOut/makePdfList.js b/modules/invoiceOut/back/methods/invoiceOut/makePdfList.js index cf041ee17..a0449aeda 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/makePdfList.js +++ b/modules/invoiceOut/back/methods/invoiceOut/makePdfList.js @@ -23,8 +23,9 @@ module.exports = Self => { }); Self.makePdfList = async function(ctx, ids, printerFk, options) { - ids.forEach(async id => { - await Self.makePdfAndNotify(ctx, id, printerFk, options); - }); + const pdfs = ids.map(id => + Self.makePdfAndNotify(ctx, id, printerFk, options) + ); + await Promise.all(pdfs); }; }; diff --git a/modules/ticket/back/methods/ticket/invoiceTicketsAndPdf.js b/modules/ticket/back/methods/ticket/invoiceTicketsAndPdf.js index 3831854a7..5529a3ae3 100644 --- a/modules/ticket/back/methods/ticket/invoiceTicketsAndPdf.js +++ b/modules/ticket/back/methods/ticket/invoiceTicketsAndPdf.js @@ -30,15 +30,11 @@ module.exports = function(Self) { Self.invoiceTicketsAndPdf = async(ctx, ticketsIds, invoiceCorrection, options) => { let invoiceIds = null; - try { - invoiceIds = await Self.invoiceTickets(ctx, ticketsIds, invoiceCorrection, options); - if (!invoiceIds) return []; - if (!options.transaction) - await Self.app.models.InvoiceOut.makePdfList(ctx, invoiceIds, null, options); - return invoiceIds; - } catch (error) { - return []; - } + invoiceIds = await Self.invoiceTickets(ctx, ticketsIds, invoiceCorrection, options); + if (!invoiceIds) return []; + if (!options.transaction) + await Self.app.models.InvoiceOut.makePdfList(ctx, invoiceIds, null, options); + return invoiceIds; }; }; diff --git a/modules/ticket/back/methods/ticket/specs/invoiceTicketsAndPdf.spec.js b/modules/ticket/back/methods/ticket/specs/invoiceTicketsAndPdf.spec.js new file mode 100644 index 000000000..086d1117f --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/invoiceTicketsAndPdf.spec.js @@ -0,0 +1,115 @@ +const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); + +describe('ticket invoiceTicketsAndPdf()', () => { + const userId = 19; + const clientId = 1102; + const activeCtx = { + getLocale: () => { + return 'en'; + }, + accessToken: {userId: userId}, + headers: {origin: 'http://localhost:5000'}, + }; + const ctx = {req: activeCtx}; + + beforeAll(async() => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + }); + + it('should throw an error when invoicing tickets from multiple clients', async() => { + const invoiceOutModel = models.InvoiceOut; + spyOn(invoiceOutModel, 'makePdfAndNotify'); + + const tx = await models.Ticket.beginTransaction({}); + + let error; + + try { + const options = {transaction: tx}; + + const ticketsIds = [11, 16]; + await models.Ticket.invoiceTicketsAndPdf(ctx, ticketsIds, null, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toEqual(`You can't invoice tickets from multiple clients`); + }); + + it(`should throw an error when invoicing a client without tax data checked`, async() => { + const invoiceOutModel = models.InvoiceOut; + spyOn(invoiceOutModel, 'makePdfAndNotify'); + + const tx = await models.Ticket.beginTransaction({}); + + let error; + + try { + const options = {transaction: tx}; + + const client = await models.Client.findById(clientId, null, options); + await client.updateAttribute('isTaxDataChecked', false, options); + + const ticketsIds = [11]; + await models.Ticket.invoiceTicketsAndPdf(ctx, ticketsIds, null, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toEqual(`This client can't be invoiced`); + }); + + it('should invoice a ticket, then try again to fail', async() => { + const invoiceOutModel = models.InvoiceOut; + spyOn(invoiceOutModel, 'makePdfAndNotify'); + + const tx = await models.Ticket.beginTransaction({}); + + let error; + + try { + const options = {transaction: tx}; + + const ticketsIds = [11]; + await models.Ticket.invoiceTicketsAndPdf(ctx, ticketsIds, null, options); + await models.Ticket.invoiceTicketsAndPdf(ctx, ticketsIds, null, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toEqual(`This ticket is already invoiced`); + }); + + it('should success to invoice a ticket', async() => { + const invoiceOutModel = models.InvoiceOut; + spyOn(invoiceOutModel, 'makePdfAndNotify'); + + const tx = await models.Ticket.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const ticketsIds = [11]; + const invoicesIds = await models.Ticket.invoiceTicketsAndPdf(ctx, ticketsIds, null, options); + + expect(invoicesIds.length).toBeGreaterThan(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +});