#6147 - Refactor Invoice make PDF and notify #1933

Merged
jsegarra merged 13 commits from 6174_refactor_invoicePdfNotify into dev 2024-01-29 14:10:33 +00:00
7 changed files with 85 additions and 15 deletions
Showing only changes of commit 21ce57d217 - Show all commits

View File

@ -0,0 +1,30 @@
module.exports = Self => {
Self.remoteMethodCtx('makePdfList', {
description: 'Handle a list of invoices to generate PDF and send it to client',
accessType: 'WRITE',
accepts: [
{
arg: 'ids',
type: ['number'],
description: 'The invoice id',
required: true,
http: {source: 'path'}
}, {
arg: 'printerFk',
type: 'number',
description: 'The printer to print'
}
],
http: {
path: '/:id/makePdfList',
verb: 'POST'
}
});
Self.makePdfList = async function(ctx, ids, printerFk, options) {
ids.forEach(async id => {
jsegarra marked this conversation as resolved Outdated
Outdated
Review

Jo crec que forEach no funciona en async await

Jo crec que forEach no funciona en async await

He buscado lo que comentas y si que hay alguna limitación comos e refleja aquí(https://gist.github.com/joeytwiddle/37d2085425c049629b80956d3c618971). Sin embargo, a nosotros no nos hace falta controlar si ha fallado la generación del PDF, o si?

Es verdad que de normal hacemos for...of, pero no he visto que crasheaba.

He buscado lo que comentas y si que hay alguna limitación comos e refleja aquí(https://gist.github.com/joeytwiddle/37d2085425c049629b80956d3c618971). Sin embargo, a nosotros no nos hace falta controlar si ha fallado la generación del PDF, o si? Es verdad que de normal hacemos for...of, pero no he visto que crasheaba.
Outdated
Review

Esq dejarlo con foreach con async await, si no funciona asi no lo veo.
Aparte al no manejarlo bien si fallase mas tarde podria romper otra parte y no es facil de debuggear despues

Esq dejarlo con foreach con async await, si no funciona asi no lo veo. Aparte al no manejarlo bien si fallase mas tarde podria romper otra parte y no es facil de debuggear despues
await Self.makePdfAndNotify(ctx, id, printerFk, options);
});
};
};

View File

@ -50,7 +50,6 @@ module.exports = Self => {
Self.transferInvoice = async(ctx, options) => {
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
let makePdf = false;
const {id, refFk, newClientFk, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk} = ctx.args;
let tx;
if (typeof options == 'object')
@ -64,7 +63,6 @@ module.exports = Self => {
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
makePdf = true;
}
try {
const filterRef = {where: {refFk: refFk}};
@ -88,16 +86,18 @@ module.exports = Self => {
clonedTicketIds.push(clonedTicket.id);
}
const invoiceCorrection =
{correctedFk: id, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk};
const invoiceCorrection = {
correctedFk: id,
cplusRectificationTypeFk,
siiTypeInvoiceOutFk,
invoiceCorrectionTypeFk
};
const refundTicketIds = refundTickets.map(ticket => ticket.id);
await models.Ticket.invoiceTickets(ctx, refundTicketIds, invoiceCorrection, myOptions);
const [invoiceId] = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, null, myOptions);
tx && await tx.commit();
const [invoiceId] = await models.Ticket.invoiceTicketsAndPdf(ctx, clonedTicketIds, null, myOptions);
makePdf && await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null);
return invoiceId;
} catch (e) {
if (tx) await tx.rollback();

View File

@ -13,6 +13,7 @@ module.exports = Self => {
require('../methods/invoiceOut/createManualInvoice')(Self);
require('../methods/invoiceOut/clientsToInvoice')(Self);
require('../methods/invoiceOut/invoiceClient')(Self);
require('../methods/invoiceOut/makePdfList')(Self);
require('../methods/invoiceOut/makePdfAndNotify')(Self);
require('../methods/invoiceOut/refund')(Self);
require('../methods/invoiceOut/invoiceEmail')(Self);

View File

@ -32,7 +32,6 @@ module.exports = function(Self) {
const models = Self.app.models;
const date = Date.vnNew();
date.setHours(0, 0, 0, 0);
let makePdf = false;
const myOptions = {userId: ctx.req.accessToken.userId};
let tx;
@ -43,7 +42,6 @@ module.exports = function(Self) {
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
makePdf = true;
}
let invoicesIds = [];
@ -78,15 +76,11 @@ module.exports = function(Self) {
for (const ticketIds of ticketsByAddress)
invoicesIds.push(await createInvoice(ctx, companyId, ticketIds, invoiceCorrection, myOptions));
if (tx) await tx.commit();
tx && await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
if (makePdf) {
for (const invoiceId of invoicesIds)
await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null);
}
return invoicesIds;
};

View File

@ -0,0 +1,44 @@
module.exports = function(Self) {
Self.remoteMethodCtx('invoiceTicketsAndPdf', {
description: 'Make out an invoice from one or more tickets',
accessType: 'WRITE',
accepts: [
{
arg: 'ticketsIds',
description: 'The tickets id',
type: ['number'],
required: true
},
{
arg: 'invoiceCorrection',
description: 'The invoice correction',
type: 'object',
}
],
returns: {
type: ['object'],
root: true
},
http: {
path: `/invoiceTicketsAndPdf`,
verb: 'POST'
}
});
Self.invoiceTicketsAndPdf = async(ctx, ticketsIds, invoiceCorrection, options) => {
let invoiceIds = null;
try {
invoiceIds = await Self.invoiceTickets(ctx, ticketsIds, invoiceCorrection, options);
if (!invoiceIds) return [];
jsegarra marked this conversation as resolved Outdated
Outdated
Review

Jo diria que no cal el if, pq si estas cridant aci es pq si o si vols el pdf. Si no cridaries al altre

Jo diria que no cal el if, pq si estas cridant aci es pq si o si vols el pdf. Si no cridaries al altre
if (!options.transaction)
await Self.app.models.InvoiceOut.makePdfList(ctx, invoiceIds, null, options);
return invoiceIds;
} catch (error) {
return [];
}
};
};

View File

@ -102,7 +102,7 @@ describe('ticket invoiceTickets()', () => {
const options = {transaction: tx};
const ticketsIds = [11];
const invoicesIds = await models.Ticket.invoiceTickets(ctx, ticketsIds, null, options);
const invoicesIds = await models.Ticket.invoiceTicketsAndPdf(ctx, ticketsIds, null, options);
jsegarra marked this conversation as resolved
Review

invoiceTickets.spec.js solo deberia hacer tests de invoiceTickets

invoiceTickets.spec.js solo deberia hacer tests de invoiceTickets
Review

Entonces deberíamos hacer un test para invoiceTicketsAndPdf.
En realidad he modificado el test porque así comprobamos 2 remoteMethod, ya que invoiceTicketsAndPdf llama a InvoiceTickets

Entonces deberíamos hacer un test para invoiceTicketsAndPdf. En realidad he modificado el test porque así comprobamos 2 remoteMethod, ya que invoiceTicketsAndPdf llama a InvoiceTickets
Review

Deberia haber uno para invoiceTickets especifico y si se puede testear invoiceTicketsAndPdf tambien

Deberia haber uno para invoiceTickets especifico y si se puede testear invoiceTicketsAndPdf tambien
expect(invoicesIds.length).toBeGreaterThan(0);

View File

@ -42,5 +42,6 @@ module.exports = function(Self) {
require('../methods/ticket/expeditionPalletLabel')(Self);
require('../methods/ticket/saveSign')(Self);
require('../methods/ticket/invoiceTickets')(Self);
require('../methods/ticket/invoiceTicketsAndPdf')(Self);
require('../methods/ticket/docuwareDownload')(Self);
};