From 652395bee135481a829f7e519ad21150f5bd4188 Mon Sep 17 00:00:00 2001 From: jgallego Date: Tue, 3 Sep 2024 15:14:33 +0200 Subject: [PATCH] feat: refs #7277 transfer addressFk --- .../methods/invoiceOut/specs/transfer.spec.js | 61 ++++++++----------- .../back/methods/invoiceOut/transfer.js | 10 ++- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/transfer.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/transfer.spec.js index f8a43dc2f..dd1932d55 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/transfer.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/transfer.spec.js @@ -2,7 +2,7 @@ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); const UserError = require('vn-loopback/util/user-error'); -fdescribe('InvoiceOut transfer()', () => { +describe('InvoiceOut transfer()', () => { const userId = 5; let options; let tx; @@ -37,36 +37,32 @@ fdescribe('InvoiceOut transfer()', () => { it('should transfer an invoice to a new client and return the new invoice ID', async() => { const makeInvoice = true; + const makePdfListMock = spyOn(models.InvoiceOut, 'makePdfList').and.returnValue(); - try { - const result = await models.InvoiceOut.transfer( - ctx, - id, - newClientFk, - cplusRectificationTypeFk, - siiTypeInvoiceOutFk, - invoiceCorrectionTypeFk, - makeInvoice - ); + const [result] = await models.InvoiceOut.transfer( + ctx, + id, + newClientFk, + cplusRectificationTypeFk, + siiTypeInvoiceOutFk, + invoiceCorrectionTypeFk, + makeInvoice, + options + ); - const newInvoice = await models.InvoiceOut.findById(result, options); + const newInvoice = await models.InvoiceOut.findById(result, null, options); - expect(newInvoice.clientFk).toBe(newClientFk); + expect(newInvoice.clientFk).toBe(newClientFk); - const transferredTickets = await models.Ticket.find({ - where: { - refFk: result, - clientFk: newClientFk - } - }, options); + const transferredTickets = await models.Ticket.find({ + where: { + refFk: newInvoice.ref, + clientFk: newClientFk + } + }, options); - expect(transferredTickets.length).toBeGreaterThan(0); - - if (tx) await tx.rollback(); - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } + expect(transferredTickets.length).toBeGreaterThan(0); + expect(makePdfListMock).toHaveBeenCalledWith(ctx, [result]); }); it('should throw an error if original invoice is not found', async() => { @@ -86,13 +82,12 @@ fdescribe('InvoiceOut transfer()', () => { } catch (e) { expect(e).toBeInstanceOf(UserError); expect(e.message).toBe('Original invoice not found'); - await tx.rollback(); } }); it('should throw an error if the new client is the same as the original client', async() => { const makeInvoice = true; - const originalInvoice = await models.InvoiceOut.findById(id); + const originalInvoice = await models.InvoiceOut.findById(id, options); try { await models.InvoiceOut.transfer( @@ -109,11 +104,10 @@ fdescribe('InvoiceOut transfer()', () => { } catch (e) { expect(e).toBeInstanceOf(UserError); expect(e.message).toBe('Select a different client'); - await tx.rollback(); } }); - fit('should not create a new invoice if makeInvoice is false', async() => { + it('should not create a new invoice if makeInvoice is false', async() => { const originalTickets = await models.Ticket.find({ where: {clientFk: newClientFk, refFk: null}, options @@ -132,11 +126,10 @@ fdescribe('InvoiceOut transfer()', () => { expect(result).toBeUndefined(); - // await tx.commit(); const transferredTickets = await models.Ticket.find({ - where: {clientFk: newClientFk, refFk: null}, - options - }); + where: {clientFk: newClientFk, refFk: null} + + }, options); expect(transferredTickets.length).toBeGreaterThan(originalTickets.length); }); diff --git a/modules/invoiceOut/back/methods/invoiceOut/transfer.js b/modules/invoiceOut/back/methods/invoiceOut/transfer.js index 8f6e9dfee..08ed69c8a 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/transfer.js +++ b/modules/invoiceOut/back/methods/invoiceOut/transfer.js @@ -84,11 +84,19 @@ module.exports = Self => { const tickets = await models.Ticket.find({where: {refFk: originalInvoice.ref}}, myOptions); const ticketIds = tickets.map(ticket => ticket.id); const transferredTickets = await models.Ticket.cloneAll(ctx, ticketIds, false, false, myOptions); + const client = await models.Client.findById(newClientFk, + {fields: ['id', 'defaultAddressFk']}, myOptions); + const address = await models.Address.findById(client.defaultAddressFk, + {fields: ['id', 'nickname']}, myOptions); const transferredTicketIds = transferredTickets.map(ticket => ticket.id); await models.Ticket.updateAll( {id: {inq: transferredTicketIds}}, - {clientFk: newClientFk}, + { + clientFk: newClientFk, + addressFk: client.defaultAddressFk, + nickname: address.nickname + }, myOptions );