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 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);
});

View File

@ -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
);