hotfix ticket refund #1930

Merged
jorgep merged 2 commits from hotfix-ticketRefund into master 2024-01-15 08:40:26 +00:00
3 changed files with 79 additions and 50 deletions

View File

@ -14,17 +14,30 @@ module.exports = Self => {
} }
try { try {
const salesFilter = { let sales;
where: {id: {inq: salesIds}}, let services;
include: {
relation: 'components', if (salesIds && salesIds.length) {
scope: { sales = await models.Sale.find({
fields: ['saleFk', 'componentFk', 'value'] where: {id: {inq: salesIds}},
include: {
relation: 'components',
scope: {
fields: ['saleFk', 'componentFk', 'value']
}
} }
} }, myOptions);
}; }
const sales = await models.Sale.find(salesFilter, myOptions);
let ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; 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(); const mappedTickets = new Map();
@ -41,32 +54,28 @@ module.exports = Self => {
newTickets.push(newTicket); newTickets.push(newTicket);
mappedTickets.set(ticketId, newTicket.id); mappedTickets.set(ticketId, newTicket.id);
} }
if (sales) {
for (const sale of sales) {
const newTicketId = mappedTickets.get(sale.ticketFk);
for (const sale of sales) { const createdSale = await models.Sale.create({
const newTicketId = mappedTickets.get(sale.ticketFk); 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({ const components = sale.components();
ticketFk: newTicketId, for (const component of components)
itemFk: sale.itemFk, component.saleFk = createdSale.id;
quantity: negative ? - sale.quantity : sale.quantity,
concept: sale.concept,
price: sale.price,
discount: sale.discount,
}, myOptions);
const components = sale.components(); await models.SaleComponent.create(components, myOptions);
for (const component of components) }
component.saleFk = createdSale.id;
await models.SaleComponent.create(components, myOptions);
} }
if (servicesIds && servicesIds.length) { if (services) {
const servicesFilter = {
where: {id: {inq: servicesIds}}
};
const services = await models.TicketService.find(servicesFilter, myOptions);
for (const service of services) { for (const service of services) {
const newTicketId = mappedTickets.get(service.ticketFk); const newTicketId = mappedTickets.get(service.ticketFk);

View File

@ -6,7 +6,6 @@ module.exports = Self => {
{ {
arg: 'salesIds', arg: 'salesIds',
type: ['number'], type: ['number'],
required: true
}, },
{ {
arg: 'servicesIds', arg: 'servicesIds',

View File

@ -44,24 +44,7 @@ describe('Sale refund()', () => {
const tickets = await models.Sale.refund(ctx, salesIds, servicesIds, withWarehouse, options); const tickets = await models.Sale.refund(ctx, salesIds, servicesIds, withWarehouse, options);
const refundedTicket = await models.Ticket.findOne({ const refundedTicket = await getTicketRefund(tickets[0].id, options);
where: {
id: tickets[0].id
},
include: [
{
relation: 'ticketSales',
scope: {
include: {
relation: 'components'
}
}
},
{
relation: 'ticketServices',
}
]
}, options);
const ticketsAfter = await models.Ticket.find({}, options); const ticketsAfter = await models.Ticket.find({}, options);
const salesLength = refundedTicket.ticketSales().length; const salesLength = refundedTicket.ticketSales().length;
const componentsLength = refundedTicket.ticketSales()[0].components().length; const componentsLength = refundedTicket.ticketSales()[0].components().length;
@ -77,4 +60,42 @@ describe('Sale refund()', () => {
throw e; 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);
}