From 5a448234d61ef486cb353a03ba5dc24db63b59fa Mon Sep 17 00:00:00 2001 From: carlosjr Date: Thu, 16 Dec 2021 10:56:10 +0100 Subject: [PATCH] transtaction commits on several endpoints --- .../back/methods/order-row/addToOrder.js | 37 ++++++++----- .../order/back/methods/order-row/removes.js | 27 ++++++--- modules/order/back/methods/order/new.js | 55 +++++++++++-------- .../order/back/methods/order/newFromTicket.js | 23 +++++--- .../back/methods/order/updateBasicData.js | 34 +++++++----- 5 files changed, 106 insertions(+), 70 deletions(-) diff --git a/modules/order/back/methods/order-row/addToOrder.js b/modules/order/back/methods/order-row/addToOrder.js index b4866e259..639fa8be7 100644 --- a/modules/order/back/methods/order-row/addToOrder.js +++ b/modules/order/back/methods/order-row/addToOrder.js @@ -33,23 +33,30 @@ module.exports = Self => { myOptions.transaction = tx; } - const isEditable = await Self.app.models.Order.isEditable(params.orderFk, myOptions); + try { + const isEditable = await Self.app.models.Order.isEditable(params.orderFk, myOptions); - if (!isEditable) - throw new UserError('This order is not editable'); + if (!isEditable) + throw new UserError('This order is not editable'); - const promises = []; - for (const item of params.items) { - promises.push( - Self.rawSql( - `CALL hedera.order_addItem(?, ?, ?, ?)`, - [params.orderFk, item.warehouseFk, item.itemFk, item.quantity], - myOptions - ) - ); + const promises = []; + for (const item of params.items) { + promises.push( + Self.rawSql( + `CALL hedera.order_addItem(?, ?, ?, ?)`, + [params.orderFk, item.warehouseFk, item.itemFk, item.quantity], + myOptions + ) + ); + } + await Promise.all(promises); + + if (tx) await tx.commit(); + + return true; + } catch (e) { + if (tx) await tx.rollback(); + throw e; } - await Promise.all(promises); - - return true; }; }; diff --git a/modules/order/back/methods/order-row/removes.js b/modules/order/back/methods/order-row/removes.js index 7d28cba22..7f35ec69d 100644 --- a/modules/order/back/methods/order-row/removes.js +++ b/modules/order/back/methods/order-row/removes.js @@ -33,18 +33,27 @@ module.exports = Self => { myOptions.transaction = tx; } - if (!params.rows || !params.rows.length) - throw new UserError('There is nothing to delete'); + try { + if (!params.rows || !params.rows.length) + throw new UserError('There is nothing to delete'); - const isEditable = await Self.app.models.Order.isEditable(params.actualOrderId, myOptions); + const isEditable = await Self.app.models.Order.isEditable(params.actualOrderId, myOptions); - if (!isEditable) - throw new UserError('This order is not editable'); + if (!isEditable) + throw new UserError('This order is not editable'); - const promises = []; - for (let i = 0; i < params.rows.length; i++) - promises.push(Self.app.models.OrderRow.destroyById(params.rows[i], myOptions)); + const promises = []; + for (let i = 0; i < params.rows.length; i++) + promises.push(Self.app.models.OrderRow.destroyById(params.rows[i], myOptions)); - return Promise.all(promises); + const deletions = await Promise.all(promises); + + if (tx) await tx.commit(); + + return deletions; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/order/back/methods/order/new.js b/modules/order/back/methods/order/new.js index b05e24e47..147859dcc 100644 --- a/modules/order/back/methods/order/new.js +++ b/modules/order/back/methods/order/new.js @@ -44,33 +44,40 @@ module.exports = Self => { myOptions.transaction = tx; } - const address = await Self.app.models.Address.findOne({ - where: {id: addressId}, - fields: ['clientFk'], - include: [ - {relation: 'client', - scope: { - include: { - relation: 'type' + try { + const address = await Self.app.models.Address.findOne({ + where: {id: addressId}, + fields: ['clientFk'], + include: [ + {relation: 'client', + scope: { + include: { + relation: 'type' + } } } - } - ] - }, myOptions); + ] + }, myOptions); - if (address.client().type().code === 'normal') { - if (!address.client().isActive) - throw new UserError(`You can't create an order for an inactive client`); + if (address.client().type().code === 'normal') { + if (!address.client().isActive) + throw new UserError(`You can't create an order for an inactive client`); + } + + query = `CALL vn.orderListCreate(?, ?, ?, ?);`; + [result] = await Self.rawSql(query, [ + landed, + agencyModeId, + addressId, + 'SALIX' + ], myOptions); + + if (tx) await tx.commit(); + + return result[0].vOrderId; + } catch (e) { + if (tx) await tx.rollback(); + throw e; } - - query = `CALL vn.orderListCreate(?, ?, ?, ?);`; - [result] = await Self.rawSql(query, [ - landed, - agencyModeId, - addressId, - 'SALIX' - ], myOptions); - - return result[0].vOrderId; }; }; diff --git a/modules/order/back/methods/order/newFromTicket.js b/modules/order/back/methods/order/newFromTicket.js index c21ae136d..e0578ff9a 100644 --- a/modules/order/back/methods/order/newFromTicket.js +++ b/modules/order/back/methods/order/newFromTicket.js @@ -30,16 +30,23 @@ module.exports = Self => { myOptions.transaction = tx; } - const ticket = await Self.app.models.Ticket.findOne({ - where: {id: ticketFk} - }, myOptions); + try { + const ticket = await Self.app.models.Ticket.findOne({ + where: {id: ticketFk} + }, myOptions); - const landed = ticket.landed; - const addressFk = ticket.addressFk; - const agencyModeFk = ticket.agencyModeFk; + const landed = ticket.landed; + const addressFk = ticket.addressFk; + const agencyModeFk = ticket.agencyModeFk; - const orderID = await Self.app.models.Order.new(landed, addressFk, agencyModeFk, myOptions); + const orderID = await Self.app.models.Order.new(landed, addressFk, agencyModeFk, myOptions); - return orderID; + if (tx) await tx.commit(); + + return orderID; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/order/back/methods/order/updateBasicData.js b/modules/order/back/methods/order/updateBasicData.js index 25cf9daa7..ae5e5b74a 100644 --- a/modules/order/back/methods/order/updateBasicData.js +++ b/modules/order/back/methods/order/updateBasicData.js @@ -43,23 +43,29 @@ module.exports = Self => { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } + try { + const order = await models.Order.findById(id, null, myOptions); + const orderRows = await models.OrderRow.find({where: {orderFk: id}}, myOptions); - const order = await models.Order.findById(id, null, myOptions); - const orderRows = await models.OrderRow.find({where: {orderFk: id}}, myOptions); + if (order.isConfirmed || orderRows.length != 0) + throw new UserError(`You can't make changes on the basic data of an confirmed order or with rows`); - if (order.isConfirmed || orderRows.length != 0) - throw new UserError(`You can't make changes on the basic data of an confirmed order or with rows`); + const updateParams = pick(params, [ + 'clientFk', + 'addressFk', + 'landed', + 'agencyModeFk', + 'note', + ]); + if (Object.keys(updateParams).length) + await order.updateAttributes(updateParams, myOptions); - const updateParams = pick(params, [ - 'clientFk', - 'addressFk', - 'landed', - 'agencyModeFk', - 'note', - ]); - if (Object.keys(updateParams).length) - await order.updateAttributes(updateParams, myOptions); + if (tx) await tx.commit(); - return order; + return order; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; };