From f52b398596a2c1b65728f5eea9d4530f6e48ac91 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 21 Jul 2023 09:50:27 +0200 Subject: [PATCH] refs #5561 refactor: sustituidos sql por loopback --- .../back/methods/sale-tracking/delete.js | 25 ++++++++--- .../ticket/back/methods/sale-tracking/new.js | 44 ++++++++++++++----- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/modules/ticket/back/methods/sale-tracking/delete.js b/modules/ticket/back/methods/sale-tracking/delete.js index 000c52621..30956228b 100644 --- a/modules/ticket/back/methods/sale-tracking/delete.js +++ b/modules/ticket/back/methods/sale-tracking/delete.js @@ -25,6 +25,7 @@ module.exports = Self => { }); Self.delete = async(saleFk, stateCode, options) => { + const models = Self.app.models; const myOptions = {}; let tx; @@ -37,15 +38,27 @@ module.exports = Self => { } try { - const result = await Self.rawSql(`CALL vn.saleTracking_del(?, ?)`, - [ - saleFk, - stateCode, - ], myOptions); + const itemShelvingSales = await models.ItemShelvingSale.find({where: {saleFk: saleFk}}, myOptions); + for (let itemShelvingSale of itemShelvingSales) + await itemShelvingSale.destroy(myOptions); + + const state = await models.State.findOne({ + where: {code: stateCode} + }, myOptions); + + const filter = { + where: { + saleFk: saleFk, + stateFk: state.id + } + }; + const saleTrackings = await models.SaleTracking.find(filter, myOptions); + for (let saleTracking of saleTrackings) + await saleTracking.destroy(myOptions); if (tx) await tx.commit(); - return result; + return true; } catch (e) { if (tx) await tx.rollback(); throw e; diff --git a/modules/ticket/back/methods/sale-tracking/new.js b/modules/ticket/back/methods/sale-tracking/new.js index ec5951d7d..2be5bd86e 100644 --- a/modules/ticket/back/methods/sale-tracking/new.js +++ b/modules/ticket/back/methods/sale-tracking/new.js @@ -33,6 +33,7 @@ module.exports = Self => { }); Self.new = async(ctx, saleFk, isChecked, quantity, stateCode, options) => { + const models = Self.app.models; const userId = ctx.req.accessToken.userId; const myOptions = {}; let tx; @@ -45,19 +46,42 @@ module.exports = Self => { myOptions.transaction = tx; } try { - const result = await Self.rawSql(`CALL vn.saleTracking_new(?, ?, ?, ?, ?, ?)`, - [ - saleFk, - isChecked, - quantity, - userId, - stateCode, - null - ], myOptions); + const state = await models.State.findOne({ + where: {code: stateCode} + }, myOptions); + + const saleTracking = await models.SaleTracking.findOne({ + where: { + saleFk: saleFk, + stateFk: state.id, + workerFk: userId + } + }, myOptions); + + let newSaleTracking; + if (saleTracking) { + newSaleTracking = await saleTracking.updateAttributes({ + saleFk: saleFk, + stateFk: state.id, + workerFk: userId, + isChecked: isChecked, + originalQuantity: quantity, + isScanned: null + }, myOptions); + } else { + newSaleTracking = await models.SaleTracking.create({ + saleFk: saleFk, + stateFk: state.id, + workerFk: userId, + isChecked: isChecked, + originalQuantity: quantity, + isScanned: null + }); + } if (tx) await tx.commit(); - return result; + return newSaleTracking; } catch (e) { if (tx) await tx.rollback(); throw e;