From ace1c9c84bbfacc4ef9bba9a8717833fb2bb0466 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Fri, 15 Oct 2021 15:59:59 +0200 Subject: [PATCH] fix(transaction): priceDifference now commits its created transaction --- .../back/methods/ticket/priceDifference.js | 125 +++++++++--------- 1 file changed, 66 insertions(+), 59 deletions(-) diff --git a/modules/ticket/back/methods/ticket/priceDifference.js b/modules/ticket/back/methods/ticket/priceDifference.js index 202a619a50..2456d3de05 100644 --- a/modules/ticket/back/methods/ticket/priceDifference.js +++ b/modules/ticket/back/methods/ticket/priceDifference.js @@ -66,70 +66,77 @@ module.exports = Self => { myOptions.transaction = tx; } - const isEditable = await Self.isEditable(ctx, args.id, myOptions); + try { + const isEditable = await Self.isEditable(ctx, args.id, myOptions); - if (!isEditable) - throw new UserError(`The sales of this ticket can't be modified`); + if (!isEditable) + throw new UserError(`The sales of this ticket can't be modified`); - const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss', myOptions); - if (!isProductionBoss) { - const zoneShipped = await models.Agency.getShipped( - args.landed, - args.addressId, - args.agencyModeId, - args.warehouseId, - myOptions); + const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss', myOptions); + if (!isProductionBoss) { + const zoneShipped = await models.Agency.getShipped( + args.landed, + args.addressId, + args.agencyModeId, + args.warehouseId, + myOptions); - if (!zoneShipped || zoneShipped.zoneFk != args.zoneId) - throw new UserError(`You don't have privileges to change the zone`); - } - - const items = await models.Sale.find({ - where: { - ticketFk: args.id - }, - order: 'concept ASC', - include: 'item' - }, myOptions); - - const salesObj = { - items: items, - totalUnitPrice: 0.00, - totalNewPrice: 0.00, - totalDifference: 0.00, - }; - - const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`; - const params = [args.id, args.landed, args.addressId, args.zoneId, args.warehouseId]; - const [difComponents] = await Self.rawSql(query, params, myOptions); - - const map = new Map(); - - // Sale price component, one per sale - for (difComponent of difComponents) - map.set(difComponent.saleFk, difComponent); - - function round(value) { - return Math.round(value * 100) / 100; - } - - for (sale of salesObj.items) { - const difComponent = map.get(sale.id); - - if (difComponent) { - sale.component = difComponent; - - salesObj.totalDifference += difComponent.difference; - salesObj.totalDifference = round(salesObj.totalDifference); - - salesObj.totalNewPrice += difComponent.newPrice; - salesObj.totalNewPrice = round(salesObj.totalNewPrice); + if (!zoneShipped || zoneShipped.zoneFk != args.zoneId) + throw new UserError(`You don't have privileges to change the zone`); } - salesObj.totalUnitPrice += sale.price; - salesObj.totalUnitPrice = round(salesObj.totalUnitPrice); - } + const items = await models.Sale.find({ + where: { + ticketFk: args.id + }, + order: 'concept ASC', + include: 'item' + }, myOptions); - return salesObj; + const salesObj = { + items: items, + totalUnitPrice: 0.00, + totalNewPrice: 0.00, + totalDifference: 0.00, + }; + + const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`; + const params = [args.id, args.landed, args.addressId, args.zoneId, args.warehouseId]; + const [difComponents] = await Self.rawSql(query, params, myOptions); + + const map = new Map(); + + // Sale price component, one per sale + for (difComponent of difComponents) + map.set(difComponent.saleFk, difComponent); + + for (sale of salesObj.items) { + const difComponent = map.get(sale.id); + + if (difComponent) { + sale.component = difComponent; + + salesObj.totalDifference += difComponent.difference; + salesObj.totalDifference = round(salesObj.totalDifference); + + salesObj.totalNewPrice += difComponent.newPrice; + salesObj.totalNewPrice = round(salesObj.totalNewPrice); + } + + salesObj.totalUnitPrice += sale.price; + salesObj.totalUnitPrice = round(salesObj.totalUnitPrice); + } + + if (tx) await tx.commit(); + + return salesObj; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; + + function round(value) { + return Math.round(value * 100) / 100; + } };