refs #6174 test: new test for new method
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Javier Segarra 2024-01-23 23:52:45 +01:00
parent dcb2907d49
commit bc6f9bc074
3 changed files with 124 additions and 12 deletions

View File

@ -23,8 +23,9 @@ module.exports = Self => {
}); });
Self.makePdfList = async function(ctx, ids, printerFk, options) { Self.makePdfList = async function(ctx, ids, printerFk, options) {
ids.forEach(async id => { const pdfs = ids.map(id =>
await Self.makePdfAndNotify(ctx, id, printerFk, options); Self.makePdfAndNotify(ctx, id, printerFk, options)
}); );
await Promise.all(pdfs);
}; };
}; };

View File

@ -30,15 +30,11 @@ module.exports = function(Self) {
Self.invoiceTicketsAndPdf = async(ctx, ticketsIds, invoiceCorrection, options) => { Self.invoiceTicketsAndPdf = async(ctx, ticketsIds, invoiceCorrection, options) => {
let invoiceIds = null; let invoiceIds = null;
try {
invoiceIds = await Self.invoiceTickets(ctx, ticketsIds, invoiceCorrection, options); invoiceIds = await Self.invoiceTickets(ctx, ticketsIds, invoiceCorrection, options);
if (!invoiceIds) return []; if (!invoiceIds) return [];
if (!options.transaction) if (!options.transaction)
await Self.app.models.InvoiceOut.makePdfList(ctx, invoiceIds, null, options); await Self.app.models.InvoiceOut.makePdfList(ctx, invoiceIds, null, options);
return invoiceIds; return invoiceIds;
} catch (error) {
return [];
}
}; };
}; };

View File

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