From 6236cf062143bc893b04d5f98dfe5b2ec440b6bf Mon Sep 17 00:00:00 2001 From: jorgep Date: Fri, 12 Jan 2024 16:18:26 +0100 Subject: [PATCH] hotfix ticketRefund --- modules/ticket/back/methods/sale/clone.js | 71 +++++++++++-------- modules/ticket/back/methods/sale/refund.js | 1 - .../back/methods/sale/specs/refund.spec.js | 57 ++++++++++----- 3 files changed, 79 insertions(+), 50 deletions(-) diff --git a/modules/ticket/back/methods/sale/clone.js b/modules/ticket/back/methods/sale/clone.js index a5ccb6de4..fd8e31e82 100644 --- a/modules/ticket/back/methods/sale/clone.js +++ b/modules/ticket/back/methods/sale/clone.js @@ -14,17 +14,30 @@ module.exports = Self => { } try { - const salesFilter = { - where: {id: {inq: salesIds}}, - include: { - relation: 'components', - scope: { - fields: ['saleFk', 'componentFk', 'value'] + let sales; + let services; + + if (salesIds && salesIds.length) { + sales = await models.Sale.find({ + where: {id: {inq: salesIds}}, + include: { + relation: 'components', + scope: { + fields: ['saleFk', 'componentFk', 'value'] + } } - } - }; - const sales = await models.Sale.find(salesFilter, myOptions); - let ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; + }, myOptions); + } + + if (servicesIds && servicesIds.length) { + services = await models.TicketService.find({ + where: {id: {inq: servicesIds}} + }, myOptions); + } + + let ticketsIds = sales ? + [...new Set(sales.map(sale => sale.ticketFk))] : + [...new Set(services.map(service => service.ticketFk))]; const mappedTickets = new Map(); @@ -41,32 +54,28 @@ module.exports = Self => { newTickets.push(newTicket); mappedTickets.set(ticketId, newTicket.id); } + if (sales) { + for (const sale of sales) { + const newTicketId = mappedTickets.get(sale.ticketFk); - for (const sale of sales) { - const newTicketId = mappedTickets.get(sale.ticketFk); + const createdSale = await models.Sale.create({ + ticketFk: newTicketId, + itemFk: sale.itemFk, + quantity: negative ? - sale.quantity : sale.quantity, + concept: sale.concept, + price: sale.price, + discount: sale.discount, + }, myOptions); - const createdSale = await models.Sale.create({ - ticketFk: newTicketId, - itemFk: sale.itemFk, - quantity: negative ? - sale.quantity : sale.quantity, - concept: sale.concept, - price: sale.price, - discount: sale.discount, - }, myOptions); + const components = sale.components(); + for (const component of components) + component.saleFk = createdSale.id; - const components = sale.components(); - for (const component of components) - component.saleFk = createdSale.id; - - await models.SaleComponent.create(components, myOptions); + await models.SaleComponent.create(components, myOptions); + } } - if (servicesIds && servicesIds.length) { - const servicesFilter = { - where: {id: {inq: servicesIds}} - }; - const services = await models.TicketService.find(servicesFilter, myOptions); - + if (services) { for (const service of services) { const newTicketId = mappedTickets.get(service.ticketFk); diff --git a/modules/ticket/back/methods/sale/refund.js b/modules/ticket/back/methods/sale/refund.js index a7831e7e3..e1d23001f 100644 --- a/modules/ticket/back/methods/sale/refund.js +++ b/modules/ticket/back/methods/sale/refund.js @@ -6,7 +6,6 @@ module.exports = Self => { { arg: 'salesIds', type: ['number'], - required: true }, { arg: 'servicesIds', diff --git a/modules/ticket/back/methods/sale/specs/refund.spec.js b/modules/ticket/back/methods/sale/specs/refund.spec.js index 60f77e90c..ab5e1e281 100644 --- a/modules/ticket/back/methods/sale/specs/refund.spec.js +++ b/modules/ticket/back/methods/sale/specs/refund.spec.js @@ -44,24 +44,7 @@ describe('Sale refund()', () => { const tickets = await models.Sale.refund(ctx, salesIds, servicesIds, withWarehouse, options); - const refundedTicket = await models.Ticket.findOne({ - where: { - id: tickets[0].id - }, - include: [ - { - relation: 'ticketSales', - scope: { - include: { - relation: 'components' - } - } - }, - { - relation: 'ticketServices', - } - ] - }, options); + const refundedTicket = await getTicketRefund(tickets[0].id, options); const ticketsAfter = await models.Ticket.find({}, options); const salesLength = refundedTicket.ticketSales().length; const componentsLength = refundedTicket.ticketSales()[0].components().length; @@ -77,4 +60,42 @@ describe('Sale refund()', () => { throw e; } }); + + it('should create a ticket without sales', async() => { + const servicesIds = [4]; + const tx = await models.Sale.beginTransaction({}); + const options = {transaction: tx}; + try { + const tickets = await models.Sale.refund(ctx, null, servicesIds, withWarehouse, options); + const refundedTicket = await getTicketRefund(tickets[0].id, options); + + expect(refundedTicket).toBeDefined(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); }); + +async function getTicketRefund(id, options) { + return models.Ticket.findOne({ + where: { + id + }, + include: [ + { + relation: 'ticketSales', + scope: { + include: { + relation: 'components' + } + } + }, + { + relation: 'ticketServices', + } + ] + }, options); +}