feat: refs #7277 transfer addressFk

This commit is contained in:
Javi Gallego 2024-09-03 15:14:33 +02:00
parent e2f1fea6be
commit 652395bee1
2 changed files with 36 additions and 35 deletions

View File

@ -2,7 +2,7 @@ const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
fdescribe('InvoiceOut transfer()', () => { describe('InvoiceOut transfer()', () => {
const userId = 5; const userId = 5;
let options; let options;
let tx; 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() => { it('should transfer an invoice to a new client and return the new invoice ID', async() => {
const makeInvoice = true; const makeInvoice = true;
const makePdfListMock = spyOn(models.InvoiceOut, 'makePdfList').and.returnValue();
try { const [result] = await models.InvoiceOut.transfer(
const result = await models.InvoiceOut.transfer( ctx,
ctx, id,
id, newClientFk,
newClientFk, cplusRectificationTypeFk,
cplusRectificationTypeFk, siiTypeInvoiceOutFk,
siiTypeInvoiceOutFk, invoiceCorrectionTypeFk,
invoiceCorrectionTypeFk, makeInvoice,
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({ const transferredTickets = await models.Ticket.find({
where: { where: {
refFk: result, refFk: newInvoice.ref,
clientFk: newClientFk clientFk: newClientFk
} }
}, options); }, options);
expect(transferredTickets.length).toBeGreaterThan(0); expect(transferredTickets.length).toBeGreaterThan(0);
expect(makePdfListMock).toHaveBeenCalledWith(ctx, [result]);
if (tx) await tx.rollback();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}); });
it('should throw an error if original invoice is not found', async() => { it('should throw an error if original invoice is not found', async() => {
@ -86,13 +82,12 @@ fdescribe('InvoiceOut transfer()', () => {
} catch (e) { } catch (e) {
expect(e).toBeInstanceOf(UserError); expect(e).toBeInstanceOf(UserError);
expect(e.message).toBe('Original invoice not found'); 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() => { it('should throw an error if the new client is the same as the original client', async() => {
const makeInvoice = true; const makeInvoice = true;
const originalInvoice = await models.InvoiceOut.findById(id); const originalInvoice = await models.InvoiceOut.findById(id, options);
try { try {
await models.InvoiceOut.transfer( await models.InvoiceOut.transfer(
@ -109,11 +104,10 @@ fdescribe('InvoiceOut transfer()', () => {
} catch (e) { } catch (e) {
expect(e).toBeInstanceOf(UserError); expect(e).toBeInstanceOf(UserError);
expect(e.message).toBe('Select a different client'); 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({ const originalTickets = await models.Ticket.find({
where: {clientFk: newClientFk, refFk: null}, where: {clientFk: newClientFk, refFk: null},
options options
@ -132,11 +126,10 @@ fdescribe('InvoiceOut transfer()', () => {
expect(result).toBeUndefined(); expect(result).toBeUndefined();
// await tx.commit();
const transferredTickets = await models.Ticket.find({ const transferredTickets = await models.Ticket.find({
where: {clientFk: newClientFk, refFk: null}, where: {clientFk: newClientFk, refFk: null}
options
}); }, options);
expect(transferredTickets.length).toBeGreaterThan(originalTickets.length); expect(transferredTickets.length).toBeGreaterThan(originalTickets.length);
}); });

View File

@ -84,11 +84,19 @@ module.exports = Self => {
const tickets = await models.Ticket.find({where: {refFk: originalInvoice.ref}}, myOptions); const tickets = await models.Ticket.find({where: {refFk: originalInvoice.ref}}, myOptions);
const ticketIds = tickets.map(ticket => ticket.id); const ticketIds = tickets.map(ticket => ticket.id);
const transferredTickets = await models.Ticket.cloneAll(ctx, ticketIds, false, false, myOptions); 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); const transferredTicketIds = transferredTickets.map(ticket => ticket.id);
await models.Ticket.updateAll( await models.Ticket.updateAll(
{id: {inq: transferredTicketIds}}, {id: {inq: transferredTicketIds}},
{clientFk: newClientFk}, {
clientFk: newClientFk,
addressFk: client.defaultAddressFk,
nickname: address.nickname
},
myOptions myOptions
); );