ref #5914 refactor createTicketRefund

This commit is contained in:
Jorge Penadés 2023-09-26 12:42:45 +02:00
parent 474399b383
commit 50543963c1
1 changed files with 26 additions and 31 deletions

View File

@ -3,6 +3,7 @@ module.exports = Self => {
const models = Self.app.models;
const myOptions = {};
let tx;
const refundTickets = [];
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -25,22 +26,19 @@ module.exports = Self => {
const sales = await models.Sale.find(salesFilter, myOptions);
let ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const refundTickets = [];
const mappedTickets = new Map();
const now = Date.vnNew();
if (group) ticketsIds = [ticketsIds[0]];
for (let ticketId in ticketsIds) {
await createTicketRefund(
for (let ticketId of ticketsIds) {
const ticketRefund = await createTicketRefund(
ctx,
ticketsIds[ticketId],
ticketId,
withWarehouse,
refundTickets,
now,
myOptions
);
mappedTickets.set(ticketsIds[ticketId], refundTickets[ticketId].id);
refundTickets.push(ticketRefund);
mappedTickets.set(ticketId, ticketRefund.id);
}
for (const sale of sales) {
@ -89,28 +87,25 @@ module.exports = Self => {
if (tx) await tx.rollback();
throw e;
}
async function createTicketRefund(
ctx,
ticketId,
withWarehouse,
myOptions
) {
const models = Self.app.models;
const now = Date.vnNew();
const ticket = await models.Ticket.findById(ticketId, null, myOptions);
ctx.args.clientId = ticket.clientFk;
ctx.args.shipped = now;
ctx.args.landed = now;
ctx.args.warehouseId = withWarehouse ? ticket.warehouseFk : null;
ctx.args.companyId = ticket.companyFk;
ctx.args.addressId = ticket.addressFk;
return models.Ticket.new(ctx, myOptions);
}
};
async function createTicketRefund(
ctx,
ticketId,
withWarehouse,
refundTickets,
now,
myOptions
) {
const models = Self.app.models;
const ticket = await models.Ticket.findById(ticketId, myOptions);
ctx.args.clientId = ticket.clientFk;
ctx.args.shipped = now;
ctx.args.landed = now;
ctx.args.warehouseId = withWarehouse ? ticket.warehouseFk : null;
ctx.args.companyId = ticket.companyFk;
ctx.args.addressId = ticket.addressFk;
const refundTicket = await models.Ticket.new(ctx, myOptions);
refundTickets.push(refundTicket);
}
};