From a8dfe8f3bfb9dc4ef8f7d1965b8b628fc81eeb2c Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 11 Dec 2023 14:53:29 +0100 Subject: [PATCH 01/19] refs #2687 feat: modify vn.travel_cloneWithEntries --- ...pdate_procedure_TravelCloneWithEntries.sql | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql diff --git a/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql b/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql new file mode 100644 index 000000000..85be8ca93 --- /dev/null +++ b/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql @@ -0,0 +1,88 @@ +DROP PROCEDURE IF EXISTS vn.travel_cloneWithEntries; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_cloneWithEntries`( + IN vTravelFk INT, + IN vDateStart DATE, + IN vDateEnd DATE, + IN vWarehouseOutFk INT, + IN vWarehouseInFk INT, + IN vRef VARCHAR(255), + IN vAgencyModeFk INT, + IN vTx BOOL, + OUT vNewTravelFk INT) +BEGIN +/** + * Clona un travel junto con sus entradas y compras + * @param vTravelFk travel plantilla a clonar + * @param vDateStart fecha del shipment del nuevo travel + * @param vDateEnd fecha del landing del nuevo travel + * @param vWarehouseOutFk fecha del salida del nuevo travel + * @param vWarehouseInFk warehouse de landing del nuevo travel + * @param vRef referencia del nuevo travel + * @param vAgencyModeFk del nuevo travel + * @param vNewTravelFk id del nuevo travel + */ + DECLARE vNewEntryFk INT; + DECLARE vEvaNotes VARCHAR(255); + DECLARE vDone BOOL; + DECLARE vAuxEntryFk INT; + DECLARE vTx BOOLEAN DEFAULT !@@in_transaction; + DECLARE vRsEntry CURSOR FOR + SELECT e.id + FROM entry e + JOIN travel t ON t.id = e.travelFk + WHERE e.travelFk = vTravelFk; + + DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; + + DECLARE EXIT HANDLER FOR SQLEXCEPTION + BEGIN + ROLLBACK; + RESIGNAL; + END; + + START TRANSACTION; + + INSERT INTO travel (shipped, landed, warehouseInFk, warehouseOutFk, agencyModeFk, `ref`, isDelivered, isReceived, m3, cargoSupplierFk, kg,clonedFrom) + SELECT vDateStart, vDateEnd, vWarehouseInFk, vWarehouseOutFk, vAgencyModeFk, vRef, isDelivered, isReceived, m3,cargoSupplierFk, kg,vTravelFk + FROM travel + WHERE id = vTravelFk; + + SET vNewTravelFk = LAST_INSERT_ID(); + + SET vDone = FALSE; + SET @isModeInventory = TRUE; + + OPEN vRsEntry; + + l: LOOP + SET vDone = FALSE; + FETCH vRsEntry INTO vAuxEntryFk; + + IF vDone THEN + LEAVE l; + END IF; + + CALL util.tx_start(vTx); + CALL entry_cloneHeader(vAuxEntryFk, vNewEntryFk, vNewTravelFk); + CALL entry_copyBuys(vAuxEntryFk, vNewEntryFk); + CALL util.tx_commit(vTx); + + SELECT evaNotes INTO vEvaNotes + FROM entry + WHERE id = vAuxEntryFk; + + UPDATE entry + SET evaNotes = vEvaNotes + WHERE id = vNewEntryFk; + + END LOOP; + + SET @isModeInventory = FALSE; + CLOSE vRsEntry; + + COMMIT; +END$$ +DELIMITER ; From 64fbbd5dda94c2f6a94dc1f7f79403b24014696f Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 11 Dec 2023 14:53:55 +0100 Subject: [PATCH 02/19] refs #2687 feat: new transaction procedure utils --- db/changes/235201/00-util_tx_commit.sql | 12 ++++++++++++ db/changes/235201/00-util_tx_rollback.sql | 12 ++++++++++++ db/changes/235201/00-util_tx_start.sql | 12 ++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 db/changes/235201/00-util_tx_commit.sql create mode 100644 db/changes/235201/00-util_tx_rollback.sql create mode 100644 db/changes/235201/00-util_tx_start.sql diff --git a/db/changes/235201/00-util_tx_commit.sql b/db/changes/235201/00-util_tx_commit.sql new file mode 100644 index 000000000..036909ed5 --- /dev/null +++ b/db/changes/235201/00-util_tx_commit.sql @@ -0,0 +1,12 @@ +DROP PROCEDURE IF EXISTS util.tx_commit; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_commit`(IN tx BOOL) +BEGIN + IF tx THEN + COMMIT; + END IF; +END +$$ +DELIMITER ; diff --git a/db/changes/235201/00-util_tx_rollback.sql b/db/changes/235201/00-util_tx_rollback.sql new file mode 100644 index 000000000..ff27601b8 --- /dev/null +++ b/db/changes/235201/00-util_tx_rollback.sql @@ -0,0 +1,12 @@ +DROP PROCEDURE IF EXISTS util.tx_rollback; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_rollback`(tx BOOL) +BEGIN + IF tx THEN + ROLLBACK; + END IF; +END +$$ +DELIMITER ; diff --git a/db/changes/235201/00-util_tx_start.sql b/db/changes/235201/00-util_tx_start.sql new file mode 100644 index 000000000..ac7c94f27 --- /dev/null +++ b/db/changes/235201/00-util_tx_start.sql @@ -0,0 +1,12 @@ +DROP PROCEDURE IF EXISTS util.tx_start; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_start`(tx BOOL) +BEGIN + IF tx THEN + START TRANSACTION; + END IF; +END +$$ +DELIMITER ; From c0d2e0511d7baf426927a9657b8d642e51771ead Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 11 Dec 2023 14:54:12 +0100 Subject: [PATCH 03/19] refs #2687 feat: modify back method --- modules/travel/back/methods/travel/cloneWithEntries.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/travel/back/methods/travel/cloneWithEntries.js b/modules/travel/back/methods/travel/cloneWithEntries.js index 45ebef283..8d0f638cc 100644 --- a/modules/travel/back/methods/travel/cloneWithEntries.js +++ b/modules/travel/back/methods/travel/cloneWithEntries.js @@ -45,16 +45,17 @@ module.exports = Self => { let stmts = []; let stmt; - + let tx = await Self.beginTransaction({}); stmt = new ParameterizedSQL( - `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [ + `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [ id, started, ended, travel.warehouseOutFk, travel.warehouseInFk, travel.ref, - travel.agencyModeFk + travel.agencyModeFk, + !!tx.id ] ); stmts.push(stmt); From 30dbc8a5871bdf27617af4d7d984ccbaeae321c6 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 12 Dec 2023 07:33:08 +0100 Subject: [PATCH 04/19] refs #2687 feat: fix test suite --- .../travel/specs/cloneWithEntries.spec.js | 80 ++++++++++++++----- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js index 0e434e048..ae3ec8c68 100644 --- a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js +++ b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js @@ -46,32 +46,72 @@ describe('Travel cloneWithEntries()', () => { // }); it(`should clone the travel and the containing entries`, async() => { - pending('#2687 - Cannot make a data rollback because of the triggers'); + const tx = await models.Travel.beginTransaction({}); const warehouseThree = 3; const agencyModeOne = 1; - const yesterday = Date.vnNew(); - yesterday.setDate(yesterday.getDate() - 1); + try { + // pending('#2687 - Cannot make a data rollback because of the triggers'); - travelBefore = await models.Travel.findById(travelId); - await travelBefore.updateAttributes({ - shipped: yesterday, - landed: yesterday - }); + console.info('TRAVEL_CLONE_WITH_ENTRIES'); + // const yesterday = Date.vnNew(); + // yesterday.setDate(yesterday.getDate() - 1); - newTravelId = await models.Travel.cloneWithEntries(ctx, travelId); - const travelEntries = await models.Entry.find({ - where: { + // travelBefore = await models.Travel.findById(travelId); + // await travelBefore.updateAttributes({ + // shipped: yesterday, + // landed: yesterday + // }); + // const filter = { + // order: 'landed ASC, shipped ASC, travelFk, loadPriority, agencyModeFk, evaNotes', + // }; + // const agencyModeFk_BEFORE = await app.models.Travel.extraCommunityFilter({ + // args: { + // agencyModeFk: 1 + // } + // }, filter); + // const cargoSupplierFk_BEFORE = await app.models.Travel.extraCommunityFilter({ + // args: { + // cargoSupplierFk: 1 + // } + // }, filter); + + const options = {transaction: tx}; + newTravelId = await models.Travel.cloneWithEntries(ctx, travelId, options); + const travelEntries = await models.Entry.find({ + where: { + travelFk: newTravelId + } + }, options); + const newTravel = await models.Travel.findById(travelId, options); + + expect(newTravelId).not.toEqual(travelId); + expect(newTravel.ref).toEqual('fifth travel'); + expect(newTravel.warehouseInFk).toEqual(warehouseThree); + expect(newTravel.warehouseOutFk).toEqual(warehouseThree); + expect(newTravel.agencyModeFk).toEqual(agencyModeOne); + expect(travelEntries.length).toBeGreaterThan(0); + await models.Entry.destroyAll({ travelFk: newTravelId - } - }); + }, options); - const newTravel = await models.Travel.findById(travelId); + // Destroy new travel + await models.Travel.destroyById(newTravelId, options); + // const agencyModeFk_AFTER = await app.models.Travel.extraCommunityFilter({ + // args: { + // agencyModeFk: 1 + // } + // }, filter); + // const cargoSupplierFk_AFTER = await app.models.Travel.extraCommunityFilter({ + // args: { + // cargoSupplierFk: 1 + // } + // }, filter); - expect(newTravelId).not.toEqual(travelId); - expect(newTravel.ref).toEqual('fifth travel'); - expect(newTravel.warehouseInFk).toEqual(warehouseThree); - expect(newTravel.warehouseOutFk).toEqual(warehouseThree); - expect(newTravel.agencyModeFk).toEqual(agencyModeOne); - expect(travelEntries.length).toBeGreaterThan(0); + // expect(agencyModeFk_BEFORE.length).toEqual(agencyModeFk_AFTER.length); + // expect(cargoSupplierFk_BEFORE.length).toEqual(cargoSupplierFk_AFTER.length); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }); }); From 8bd9becb74a08132f3724476bf3a3dfb2024266e Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 12 Dec 2023 07:34:20 +0100 Subject: [PATCH 05/19] refs #2687 feat: remove procedure argument --- ...pdate_procedure_TravelCloneWithEntries.sql | 1 - .../back/methods/travel/cloneWithEntries.js | 115 ++++++++++-------- .../travel/specs/cloneWithEntries.spec.js | 78 ------------ 3 files changed, 62 insertions(+), 132 deletions(-) diff --git a/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql b/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql index 85be8ca93..fa47881cc 100644 --- a/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql +++ b/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql @@ -10,7 +10,6 @@ CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_cloneWithEntries`( IN vWarehouseInFk INT, IN vRef VARCHAR(255), IN vAgencyModeFk INT, - IN vTx BOOL, OUT vNewTravelFk INT) BEGIN /** diff --git a/modules/travel/back/methods/travel/cloneWithEntries.js b/modules/travel/back/methods/travel/cloneWithEntries.js index 8d0f638cc..1591fb65c 100644 --- a/modules/travel/back/methods/travel/cloneWithEntries.js +++ b/modules/travel/back/methods/travel/cloneWithEntries.js @@ -11,8 +11,9 @@ module.exports = Self => { type: 'number', required: true, description: 'The original travel id', - http: {source: 'path'} - }], + http: {source: 'path'}, + }, + ], returns: { type: 'object', description: 'The new cloned travel id', @@ -24,62 +25,70 @@ module.exports = Self => { } }); - Self.cloneWithEntries = async(ctx, id) => { - const conn = Self.dataSource.connector; - const travel = await Self.findById(id, { - fields: [ - 'id', - 'shipped', - 'landed', - 'warehouseInFk', - 'warehouseOutFk', - 'agencyModeFk', - 'ref' - ] - }); - const started = Date.vnNew(); - const ended = Date.vnNew(); + Self.cloneWithEntries = async(ctx, id, options) => { + try { + const conn = Self.dataSource.connector; + const myOptions = {}; - if (!travel) - throw new UserError('Travel not found'); + if (typeof options == 'object') + Object.assign(myOptions, options); + const travel = await Self.findById(id, { + fields: [ + 'id', + 'shipped', + 'landed', + 'warehouseInFk', + 'warehouseOutFk', + 'agencyModeFk', + 'ref' + ] + }); + const started = Date.vnNew(); + const ended = Date.vnNew(); - let stmts = []; - let stmt; - let tx = await Self.beginTransaction({}); - stmt = new ParameterizedSQL( - `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [ - id, - started, - ended, - travel.warehouseOutFk, - travel.warehouseInFk, - travel.ref, - travel.agencyModeFk, - !!tx.id - ] - ); - stmts.push(stmt); - const newTravelIndex = stmts.push('SELECT @vTravelFk AS id') - 1; + if (!travel) + throw new UserError('Travel not found'); - const sql = ParameterizedSQL.join(stmts, ';'); - const result = await conn.executeStmt(sql); - const [lastInsert] = result[newTravelIndex]; + let stmts = []; + let stmt; + let tx = options.transaction; + stmt = new ParameterizedSQL( + `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [ + id, + started, + ended, + travel.warehouseOutFk, + travel.warehouseInFk, + travel.ref, + travel.agencyModeFk + ] + ); + stmts.push(stmt); + const newTravelIndex = stmts.push('SELECT @vTravelFk AS id') - 1; - if (!lastInsert.id) - throw new UserError('Unable to clone this travel'); + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql, myOptions); + const [lastInsert] = result[newTravelIndex]; - const newTravel = await Self.findById(lastInsert.id, { - fields: [ - 'id', - 'shipped', - 'landed', - 'warehouseInFk', - 'warehouseOutFk', - 'agencyModeFk', - 'ref' - ] - }); + if (!lastInsert.id) + throw new UserError('Unable to clone this travel'); - return newTravel.id; + const newTravel = await Self.findById(lastInsert.id, { + fields: [ + 'id', + 'shipped', + 'landed', + 'warehouseInFk', + 'warehouseOutFk', + 'agencyModeFk', + 'ref' + ] + }); + + return newTravel.id; + } catch (e) { + if (myOptions.transaction) await myOptions.transaction.rollback(); + throw e; + } }; }; diff --git a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js index ae3ec8c68..fb8bf3897 100644 --- a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js +++ b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js @@ -5,76 +5,12 @@ describe('Travel cloneWithEntries()', () => { const travelId = 5; const currentUserId = 1102; const ctx = {req: {accessToken: {userId: currentUserId}}}; - let travelBefore; let newTravelId; - - // afterAll(async() => { - // try { - // const entries = await models.Entry.find({ - // where: { - // travelFk: newTravelId - // } - // }); - // const entriesId = entries.map(entry => entry.id); - - // // Destroy all entries buys - // await models.Buy.destroyAll({ - // where: { - // entryFk: {inq: entriesId} - // } - // }); - - // // Destroy travel entries - // await models.Entry.destroyAll({ - // where: { - // travelFk: newTravelId - // } - // }); - - // // Destroy new travel - // await models.Travel.destroyById(newTravelId); - - // // Restore original travel shipped & landed - // const travel = await models.Travel.findById(travelId); - // await travel.updateAttributes({ - // shipped: travelBefore.shipped, - // landed: travelBefore.landed - // }); - // } catch (error) { - // console.error(error); - // } - // }); - it(`should clone the travel and the containing entries`, async() => { const tx = await models.Travel.beginTransaction({}); const warehouseThree = 3; const agencyModeOne = 1; try { - // pending('#2687 - Cannot make a data rollback because of the triggers'); - - console.info('TRAVEL_CLONE_WITH_ENTRIES'); - // const yesterday = Date.vnNew(); - // yesterday.setDate(yesterday.getDate() - 1); - - // travelBefore = await models.Travel.findById(travelId); - // await travelBefore.updateAttributes({ - // shipped: yesterday, - // landed: yesterday - // }); - // const filter = { - // order: 'landed ASC, shipped ASC, travelFk, loadPriority, agencyModeFk, evaNotes', - // }; - // const agencyModeFk_BEFORE = await app.models.Travel.extraCommunityFilter({ - // args: { - // agencyModeFk: 1 - // } - // }, filter); - // const cargoSupplierFk_BEFORE = await app.models.Travel.extraCommunityFilter({ - // args: { - // cargoSupplierFk: 1 - // } - // }, filter); - const options = {transaction: tx}; newTravelId = await models.Travel.cloneWithEntries(ctx, travelId, options); const travelEntries = await models.Entry.find({ @@ -94,21 +30,7 @@ describe('Travel cloneWithEntries()', () => { travelFk: newTravelId }, options); - // Destroy new travel await models.Travel.destroyById(newTravelId, options); - // const agencyModeFk_AFTER = await app.models.Travel.extraCommunityFilter({ - // args: { - // agencyModeFk: 1 - // } - // }, filter); - // const cargoSupplierFk_AFTER = await app.models.Travel.extraCommunityFilter({ - // args: { - // cargoSupplierFk: 1 - // } - // }, filter); - - // expect(agencyModeFk_BEFORE.length).toEqual(agencyModeFk_AFTER.length); - // expect(cargoSupplierFk_BEFORE.length).toEqual(cargoSupplierFk_AFTER.length); } catch (e) { if (tx) await tx.rollback(); throw e; From d9ffdaf855891aaa3228b693703b90a10bf0f9ee Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 14 Dec 2023 16:00:29 +0100 Subject: [PATCH 06/19] refs #2687 test: use tx.rollback() --- ...pdate_procedure_TravelCloneWithEntries.sql | 19 ++++++++++----- .../back/methods/travel/cloneWithEntries.js | 24 ++++++++++++------- .../travel/specs/cloneWithEntries.spec.js | 24 ++++++++++++------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql b/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql index fa47881cc..54772d768 100644 --- a/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql +++ b/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql @@ -10,7 +10,8 @@ CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_cloneWithEntries`( IN vWarehouseInFk INT, IN vRef VARCHAR(255), IN vAgencyModeFk INT, - OUT vNewTravelFk INT) + OUT vNewTravelFk INT, + OUT vXTx BOOLEAN ) BEGIN /** * Clona un travel junto con sus entradas y compras @@ -38,11 +39,12 @@ BEGIN DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN - ROLLBACK; + CALL util.tx_rollback(vTx); RESIGNAL; END; - START TRANSACTION; + -- START TRANSACTION; + CALL util.tx_start(vTx); INSERT INTO travel (shipped, landed, warehouseInFk, warehouseOutFk, agencyModeFk, `ref`, isDelivered, isReceived, m3, cargoSupplierFk, kg,clonedFrom) SELECT vDateStart, vDateEnd, vWarehouseInFk, vWarehouseOutFk, vAgencyModeFk, vRef, isDelivered, isReceived, m3,cargoSupplierFk, kg,vTravelFk @@ -64,10 +66,13 @@ BEGIN LEAVE l; END IF; - CALL util.tx_start(vTx); + -- CALL util.tx_start(vTx); CALL entry_cloneHeader(vAuxEntryFk, vNewEntryFk, vNewTravelFk); + -- CALL util.tx_commit(vTx); + -- CALL util.tx_start(vTx); CALL entry_copyBuys(vAuxEntryFk, vNewEntryFk); - CALL util.tx_commit(vTx); +-- CALL util.tx_commit(vTx); + SELECT evaNotes INTO vEvaNotes FROM entry @@ -82,6 +87,8 @@ BEGIN SET @isModeInventory = FALSE; CLOSE vRsEntry; - COMMIT; + -- COMMIT; + SET vXTx = vTx; + CALL util.tx_commit(vTx); END$$ DELIMITER ; diff --git a/modules/travel/back/methods/travel/cloneWithEntries.js b/modules/travel/back/methods/travel/cloneWithEntries.js index 1591fb65c..998e8e3d4 100644 --- a/modules/travel/back/methods/travel/cloneWithEntries.js +++ b/modules/travel/back/methods/travel/cloneWithEntries.js @@ -1,6 +1,5 @@ const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; const UserError = require('vn-loopback/util/user-error'); -const loggable = require('vn-loopback/util/log'); module.exports = Self => { Self.remoteMethodCtx('cloneWithEntries', { @@ -26,12 +25,19 @@ module.exports = Self => { }); Self.cloneWithEntries = async(ctx, id, options) => { + const conn = Self.dataSource.connector; + const myOptions = {}; + let tx = options?.transaction; try { - const conn = Self.dataSource.connector; - const myOptions = {}; - if (typeof options == 'object') Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + if (myOptions.transaction !== tx) + throw new Error('transaction ID not match'); const travel = await Self.findById(id, { fields: [ 'id', @@ -51,9 +57,8 @@ module.exports = Self => { let stmts = []; let stmt; - let tx = options.transaction; stmt = new ParameterizedSQL( - `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [ + `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk, @vNewTx)`, [ id, started, ended, @@ -68,6 +73,8 @@ module.exports = Self => { const sql = ParameterizedSQL.join(stmts, ';'); const result = await conn.executeStmt(sql, myOptions); + // if (tx) await tx.commit(); + const [lastInsert] = result[newTravelIndex]; if (!lastInsert.id) @@ -83,11 +90,10 @@ module.exports = Self => { 'agencyModeFk', 'ref' ] - }); - + }, myOptions); return newTravel.id; } catch (e) { - if (myOptions.transaction) await myOptions.transaction.rollback(); + if (tx) await tx.rollback(); throw e; } }; diff --git a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js index fb8bf3897..82ea588e7 100644 --- a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js +++ b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js @@ -1,13 +1,14 @@ const app = require('vn-loopback/server/server'); -describe('Travel cloneWithEntries()', () => { +fdescribe('Travel cloneWithEntries()', () => { const models = app.models; const travelId = 5; const currentUserId = 1102; const ctx = {req: {accessToken: {userId: currentUserId}}}; let newTravelId; it(`should clone the travel and the containing entries`, async() => { - const tx = await models.Travel.beginTransaction({}); + const tx = await models.Travel.beginTransaction({ + }); const warehouseThree = 3; const agencyModeOne = 1; try { @@ -18,19 +19,26 @@ describe('Travel cloneWithEntries()', () => { travelFk: newTravelId } }, options); - const newTravel = await models.Travel.findById(travelId, options); + const newTravel = await models.Travel.findById(travelId); expect(newTravelId).not.toEqual(travelId); + expect(newTravel.ref).toEqual('fifth travel'); expect(newTravel.warehouseInFk).toEqual(warehouseThree); expect(newTravel.warehouseOutFk).toEqual(warehouseThree); expect(newTravel.agencyModeFk).toEqual(agencyModeOne); - expect(travelEntries.length).toBeGreaterThan(0); - await models.Entry.destroyAll({ - travelFk: newTravelId - }, options); - await models.Travel.destroyById(newTravelId, options); + expect(travelEntries.length).toBeGreaterThan(0); + await tx.rollback(); + const travelRemoved = await models.Travel.findById(newTravelId, options); + + expect(travelRemoved).toBeNull(); + + // await models.Entry.destroyAll({ + // travelFk: newTravelId + // }, options); + + // await models.Travel.destroyById(newTravelId, options); } catch (e) { if (tx) await tx.rollback(); throw e; From b5b58faee4a58d14ee2ae4509fa9a401433692d1 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 15 Dec 2023 07:34:37 +0100 Subject: [PATCH 07/19] refs #2687 perf: remove comments --- ...-update_procedure_TravelCloneWithEntries.sql | 17 ++++------------- .../back/methods/travel/cloneWithEntries.js | 8 +++----- .../travel/specs/cloneWithEntries.spec.js | 9 +-------- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql b/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql index 54772d768..7d04096e3 100644 --- a/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql +++ b/db/changes/235201/00-update_procedure_TravelCloneWithEntries.sql @@ -10,8 +10,7 @@ CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_cloneWithEntries`( IN vWarehouseInFk INT, IN vRef VARCHAR(255), IN vAgencyModeFk INT, - OUT vNewTravelFk INT, - OUT vXTx BOOLEAN ) + OUT vNewTravelFk INT) BEGIN /** * Clona un travel junto con sus entradas y compras @@ -43,8 +42,7 @@ BEGIN RESIGNAL; END; - -- START TRANSACTION; - CALL util.tx_start(vTx); + CALL util.tx_start(vTx); INSERT INTO travel (shipped, landed, warehouseInFk, warehouseOutFk, agencyModeFk, `ref`, isDelivered, isReceived, m3, cargoSupplierFk, kg,clonedFrom) SELECT vDateStart, vDateEnd, vWarehouseInFk, vWarehouseOutFk, vAgencyModeFk, vRef, isDelivered, isReceived, m3,cargoSupplierFk, kg,vTravelFk @@ -66,13 +64,8 @@ BEGIN LEAVE l; END IF; - -- CALL util.tx_start(vTx); - CALL entry_cloneHeader(vAuxEntryFk, vNewEntryFk, vNewTravelFk); - -- CALL util.tx_commit(vTx); - -- CALL util.tx_start(vTx); - CALL entry_copyBuys(vAuxEntryFk, vNewEntryFk); --- CALL util.tx_commit(vTx); - + CALL entry_cloneHeader(vAuxEntryFk, vNewEntryFk, vNewTravelFk); + CALL entry_copyBuys(vAuxEntryFk, vNewEntryFk); SELECT evaNotes INTO vEvaNotes FROM entry @@ -87,8 +80,6 @@ BEGIN SET @isModeInventory = FALSE; CLOSE vRsEntry; - -- COMMIT; - SET vXTx = vTx; CALL util.tx_commit(vTx); END$$ DELIMITER ; diff --git a/modules/travel/back/methods/travel/cloneWithEntries.js b/modules/travel/back/methods/travel/cloneWithEntries.js index 998e8e3d4..e5eb0b06c 100644 --- a/modules/travel/back/methods/travel/cloneWithEntries.js +++ b/modules/travel/back/methods/travel/cloneWithEntries.js @@ -28,6 +28,7 @@ module.exports = Self => { const conn = Self.dataSource.connector; const myOptions = {}; let tx = options?.transaction; + try { if (typeof options == 'object') Object.assign(myOptions, options); @@ -36,8 +37,7 @@ module.exports = Self => { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } - if (myOptions.transaction !== tx) - throw new Error('transaction ID not match'); + const travel = await Self.findById(id, { fields: [ 'id', @@ -58,7 +58,7 @@ module.exports = Self => { let stmts = []; let stmt; stmt = new ParameterizedSQL( - `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk, @vNewTx)`, [ + `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [ id, started, ended, @@ -73,8 +73,6 @@ module.exports = Self => { const sql = ParameterizedSQL.join(stmts, ';'); const result = await conn.executeStmt(sql, myOptions); - // if (tx) await tx.commit(); - const [lastInsert] = result[newTravelIndex]; if (!lastInsert.id) diff --git a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js index 82ea588e7..9e14bdd3b 100644 --- a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js +++ b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js @@ -22,23 +22,16 @@ fdescribe('Travel cloneWithEntries()', () => { const newTravel = await models.Travel.findById(travelId); expect(newTravelId).not.toEqual(travelId); - expect(newTravel.ref).toEqual('fifth travel'); expect(newTravel.warehouseInFk).toEqual(warehouseThree); expect(newTravel.warehouseOutFk).toEqual(warehouseThree); expect(newTravel.agencyModeFk).toEqual(agencyModeOne); - expect(travelEntries.length).toBeGreaterThan(0); + await tx.rollback(); const travelRemoved = await models.Travel.findById(newTravelId, options); expect(travelRemoved).toBeNull(); - - // await models.Entry.destroyAll({ - // travelFk: newTravelId - // }, options); - - // await models.Travel.destroyById(newTravelId, options); } catch (e) { if (tx) await tx.rollback(); throw e; From 98a98e0d9ef46a278ea203ab336f01fccf4bb9b9 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 15 Dec 2023 07:40:31 +0100 Subject: [PATCH 08/19] refs #2687 perf: remove force test --- .../travel/back/methods/travel/specs/cloneWithEntries.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js index 9e14bdd3b..74ec10155 100644 --- a/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js +++ b/modules/travel/back/methods/travel/specs/cloneWithEntries.spec.js @@ -1,6 +1,6 @@ const app = require('vn-loopback/server/server'); -fdescribe('Travel cloneWithEntries()', () => { +describe('Travel cloneWithEntries()', () => { const models = app.models; const travelId = 5; const currentUserId = 1102; From 9165b921ca3ad360e8a1c3a9751e9bbe22d36782 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 2 Jan 2024 13:00:37 +0100 Subject: [PATCH 09/19] refs #2687 perf: change db/changes folder --- .../00-update_procedure_TravelCloneWithEntries.sql | 0 db/changes/{240001 => 240201}/00-util_tx_commit.sql | 0 db/changes/{240001 => 240201}/00-util_tx_rollback.sql | 0 db/changes/{240001 => 240201}/00-util_tx_start.sql | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename db/changes/{240001 => 240201}/00-update_procedure_TravelCloneWithEntries.sql (100%) rename db/changes/{240001 => 240201}/00-util_tx_commit.sql (100%) rename db/changes/{240001 => 240201}/00-util_tx_rollback.sql (100%) rename db/changes/{240001 => 240201}/00-util_tx_start.sql (100%) diff --git a/db/changes/240001/00-update_procedure_TravelCloneWithEntries.sql b/db/changes/240201/00-update_procedure_TravelCloneWithEntries.sql similarity index 100% rename from db/changes/240001/00-update_procedure_TravelCloneWithEntries.sql rename to db/changes/240201/00-update_procedure_TravelCloneWithEntries.sql diff --git a/db/changes/240001/00-util_tx_commit.sql b/db/changes/240201/00-util_tx_commit.sql similarity index 100% rename from db/changes/240001/00-util_tx_commit.sql rename to db/changes/240201/00-util_tx_commit.sql diff --git a/db/changes/240001/00-util_tx_rollback.sql b/db/changes/240201/00-util_tx_rollback.sql similarity index 100% rename from db/changes/240001/00-util_tx_rollback.sql rename to db/changes/240201/00-util_tx_rollback.sql diff --git a/db/changes/240001/00-util_tx_start.sql b/db/changes/240201/00-util_tx_start.sql similarity index 100% rename from db/changes/240001/00-util_tx_start.sql rename to db/changes/240201/00-util_tx_start.sql From b3e1c58918dd63769515b08dd6a022dd6ad78cbf Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 15 Jan 2024 14:50:08 +0100 Subject: [PATCH 10/19] refs #6313 remove restriction --- modules/ticket/back/models/sale.js | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/ticket/back/models/sale.js b/modules/ticket/back/models/sale.js index 3589eac4b..dd092573d 100644 --- a/modules/ticket/back/models/sale.js +++ b/modules/ticket/back/models/sale.js @@ -28,7 +28,6 @@ module.exports = Self => { const loopBackContext = LoopBackContext.getCurrentContext(); ctx.req = loopBackContext.active; - if (await models.ACL.checkAccessAcl(ctx, 'Sale', 'canForceQuantity', 'WRITE')) return; const ticketId = changes?.ticketFk || instance?.ticketFk; const itemId = changes?.itemFk || instance?.itemFk; From 39453492a0a55a3f880adeb1bbf2503dd0e94489 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 22 Jan 2024 10:22:21 +0100 Subject: [PATCH 11/19] refs #2687 feat update db/change version --- .../00-update_procedure_TravelCloneWithEntries.sql | 0 db/changes/{240201 => 240601}/00-util_tx_commit.sql | 0 db/changes/{240201 => 240601}/00-util_tx_rollback.sql | 0 db/changes/{240201 => 240601}/00-util_tx_start.sql | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename db/changes/{240201 => 240601}/00-update_procedure_TravelCloneWithEntries.sql (100%) rename db/changes/{240201 => 240601}/00-util_tx_commit.sql (100%) rename db/changes/{240201 => 240601}/00-util_tx_rollback.sql (100%) rename db/changes/{240201 => 240601}/00-util_tx_start.sql (100%) diff --git a/db/changes/240201/00-update_procedure_TravelCloneWithEntries.sql b/db/changes/240601/00-update_procedure_TravelCloneWithEntries.sql similarity index 100% rename from db/changes/240201/00-update_procedure_TravelCloneWithEntries.sql rename to db/changes/240601/00-update_procedure_TravelCloneWithEntries.sql diff --git a/db/changes/240201/00-util_tx_commit.sql b/db/changes/240601/00-util_tx_commit.sql similarity index 100% rename from db/changes/240201/00-util_tx_commit.sql rename to db/changes/240601/00-util_tx_commit.sql diff --git a/db/changes/240201/00-util_tx_rollback.sql b/db/changes/240601/00-util_tx_rollback.sql similarity index 100% rename from db/changes/240201/00-util_tx_rollback.sql rename to db/changes/240601/00-util_tx_rollback.sql diff --git a/db/changes/240201/00-util_tx_start.sql b/db/changes/240601/00-util_tx_start.sql similarity index 100% rename from db/changes/240201/00-util_tx_start.sql rename to db/changes/240601/00-util_tx_start.sql From c1bc2c94a6ae782aa969e8bb00e5b8e26e1afdd9 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 23 Jan 2024 08:33:15 +0100 Subject: [PATCH 12/19] refs #2687 perf: generate comments --- db/changes/240601/00-util_tx_commit.sql | 5 +++++ db/changes/240601/00-util_tx_rollback.sql | 5 +++++ db/changes/240601/00-util_tx_start.sql | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/db/changes/240601/00-util_tx_commit.sql b/db/changes/240601/00-util_tx_commit.sql index 036909ed5..ebf3ffdb4 100644 --- a/db/changes/240601/00-util_tx_commit.sql +++ b/db/changes/240601/00-util_tx_commit.sql @@ -2,6 +2,11 @@ DROP PROCEDURE IF EXISTS util.tx_commit; DELIMITER $$ $$ +/* +Procedimiento para confirmar los cambios asociados a una transacción + * @param tx BOOL es true si existe transacción asociada +*/ + CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_commit`(IN tx BOOL) BEGIN IF tx THEN diff --git a/db/changes/240601/00-util_tx_rollback.sql b/db/changes/240601/00-util_tx_rollback.sql index ff27601b8..7be817ebc 100644 --- a/db/changes/240601/00-util_tx_rollback.sql +++ b/db/changes/240601/00-util_tx_rollback.sql @@ -2,6 +2,11 @@ DROP PROCEDURE IF EXISTS util.tx_rollback; DELIMITER $$ $$ +/* +Procedimiento para deshacer los cambios asociados a una transacción + * @param tx BOOL es true si existe transacción asociada +*/ + CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_rollback`(tx BOOL) BEGIN IF tx THEN diff --git a/db/changes/240601/00-util_tx_start.sql b/db/changes/240601/00-util_tx_start.sql index ac7c94f27..7d2c8a2b5 100644 --- a/db/changes/240601/00-util_tx_start.sql +++ b/db/changes/240601/00-util_tx_start.sql @@ -2,6 +2,11 @@ DROP PROCEDURE IF EXISTS util.tx_start; DELIMITER $$ $$ +/* +Procedimiento para iniciar una transacción + * @param tx BOOL es true si existe transacción asociada +*/ + CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_start`(tx BOOL) BEGIN IF tx THEN From fe714aef220bff5b10a06631cf131a1e7e5119ac Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 23 Jan 2024 11:20:27 +0100 Subject: [PATCH 13/19] refs #2687 perf: change variable description --- .../240601/00-update_procedure_TravelCloneWithEntries.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/changes/240601/00-update_procedure_TravelCloneWithEntries.sql b/db/changes/240601/00-update_procedure_TravelCloneWithEntries.sql index 7d04096e3..b0c177b9a 100644 --- a/db/changes/240601/00-update_procedure_TravelCloneWithEntries.sql +++ b/db/changes/240601/00-update_procedure_TravelCloneWithEntries.sql @@ -17,7 +17,7 @@ BEGIN * @param vTravelFk travel plantilla a clonar * @param vDateStart fecha del shipment del nuevo travel * @param vDateEnd fecha del landing del nuevo travel - * @param vWarehouseOutFk fecha del salida del nuevo travel + * @param vWarehouseOutFk warehouse del salida del nuevo travel * @param vWarehouseInFk warehouse de landing del nuevo travel * @param vRef referencia del nuevo travel * @param vAgencyModeFk del nuevo travel From 056dddd21229f2c8911c0917555e24bea5a254ad Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 23 Jan 2024 11:22:59 +0100 Subject: [PATCH 14/19] refs #2687 perf: generate comments --- db/changes/240601/00-util_tx_commit.sql | 5 +++-- db/changes/240601/00-util_tx_rollback.sql | 5 +++-- db/changes/240601/00-util_tx_start.sql | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/db/changes/240601/00-util_tx_commit.sql b/db/changes/240601/00-util_tx_commit.sql index ebf3ffdb4..47e782987 100644 --- a/db/changes/240601/00-util_tx_commit.sql +++ b/db/changes/240601/00-util_tx_commit.sql @@ -2,8 +2,9 @@ DROP PROCEDURE IF EXISTS util.tx_commit; DELIMITER $$ $$ -/* -Procedimiento para confirmar los cambios asociados a una transacción +/** + * Procedimiento para confirmar los cambios asociados a una transacción + * * @param tx BOOL es true si existe transacción asociada */ diff --git a/db/changes/240601/00-util_tx_rollback.sql b/db/changes/240601/00-util_tx_rollback.sql index 7be817ebc..1a79479be 100644 --- a/db/changes/240601/00-util_tx_rollback.sql +++ b/db/changes/240601/00-util_tx_rollback.sql @@ -2,8 +2,9 @@ DROP PROCEDURE IF EXISTS util.tx_rollback; DELIMITER $$ $$ -/* -Procedimiento para deshacer los cambios asociados a una transacción +/** + * Procedimiento para deshacer los cambios asociados a una transacción + * * @param tx BOOL es true si existe transacción asociada */ diff --git a/db/changes/240601/00-util_tx_start.sql b/db/changes/240601/00-util_tx_start.sql index 7d2c8a2b5..22d156642 100644 --- a/db/changes/240601/00-util_tx_start.sql +++ b/db/changes/240601/00-util_tx_start.sql @@ -2,8 +2,9 @@ DROP PROCEDURE IF EXISTS util.tx_start; DELIMITER $$ $$ -/* -Procedimiento para iniciar una transacción +/** + * Procedimiento para iniciar una transacción + * * @param tx BOOL es true si existe transacción asociada */ From b5d883cdbeb742187a923ca70ca12f1923cb4714 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 23 Jan 2024 11:53:12 +0100 Subject: [PATCH 15/19] refs #2687 feat: add grant privileges for guest --- db/changes/240601/00-grant_privileges_util_tx.sql | 3 +++ db/changes/240601/00-util_tx_commit.sql | 4 +--- db/changes/240601/00-util_tx_rollback.sql | 4 +--- db/changes/240601/00-util_tx_start.sql | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) create mode 100644 db/changes/240601/00-grant_privileges_util_tx.sql diff --git a/db/changes/240601/00-grant_privileges_util_tx.sql b/db/changes/240601/00-grant_privileges_util_tx.sql new file mode 100644 index 000000000..bd49f6cb2 --- /dev/null +++ b/db/changes/240601/00-grant_privileges_util_tx.sql @@ -0,0 +1,3 @@ +GRANT EXECUTE ON PROCEDURE util.tx_commit TO guest; +GRANT EXECUTE ON PROCEDURE util.tx_rollback TO guest; +GRANT EXECUTE ON PROCEDURE util.tx_start TO guest; diff --git a/db/changes/240601/00-util_tx_commit.sql b/db/changes/240601/00-util_tx_commit.sql index 47e782987..f288e5fe3 100644 --- a/db/changes/240601/00-util_tx_commit.sql +++ b/db/changes/240601/00-util_tx_commit.sql @@ -1,5 +1,3 @@ -DROP PROCEDURE IF EXISTS util.tx_commit; - DELIMITER $$ $$ /** @@ -8,7 +6,7 @@ $$ * @param tx BOOL es true si existe transacción asociada */ -CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_commit`(IN tx BOOL) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_commit`(IN tx BOOL) BEGIN IF tx THEN COMMIT; diff --git a/db/changes/240601/00-util_tx_rollback.sql b/db/changes/240601/00-util_tx_rollback.sql index 1a79479be..3ec738fa7 100644 --- a/db/changes/240601/00-util_tx_rollback.sql +++ b/db/changes/240601/00-util_tx_rollback.sql @@ -1,5 +1,3 @@ -DROP PROCEDURE IF EXISTS util.tx_rollback; - DELIMITER $$ $$ /** @@ -8,7 +6,7 @@ $$ * @param tx BOOL es true si existe transacción asociada */ -CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_rollback`(tx BOOL) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_rollback`(tx BOOL) BEGIN IF tx THEN ROLLBACK; diff --git a/db/changes/240601/00-util_tx_start.sql b/db/changes/240601/00-util_tx_start.sql index 22d156642..f4a970c4f 100644 --- a/db/changes/240601/00-util_tx_start.sql +++ b/db/changes/240601/00-util_tx_start.sql @@ -1,4 +1,4 @@ -DROP PROCEDURE IF EXISTS util.tx_start; + DELIMITER $$ $$ @@ -8,7 +8,7 @@ $$ * @param tx BOOL es true si existe transacción asociada */ -CREATE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_start`(tx BOOL) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_start`(tx BOOL) BEGIN IF tx THEN START TRANSACTION; From 5d53d69754aa5a1c36570ae1302428d65acdabee Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 23 Jan 2024 12:52:35 +0100 Subject: [PATCH 16/19] refs #6694 perf: update postCode/filter method --- back/methods/postcode/filter.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/back/methods/postcode/filter.js b/back/methods/postcode/filter.js index 9986a16c9..8dbe2965f 100644 --- a/back/methods/postcode/filter.js +++ b/back/methods/postcode/filter.js @@ -7,10 +7,6 @@ module.exports = Self => { description: 'Find all postcodes of the model matched by postcode, town, province or country.', accessType: 'READ', - returns: { - type: ['object'], - root: true, - }, accepts: [ { arg: 'filter', @@ -25,6 +21,10 @@ module.exports = Self => { http: {source: 'query'} }, ], + returns: { + type: ['object'], + root: true, + }, http: { path: `/filter`, verb: 'GET', @@ -32,12 +32,11 @@ module.exports = Self => { }); Self.filter = async(ctx, filter, options) => { const myOptions = {}; - if (typeof options == 'object') Object.assign(myOptions, options); const conn = Self.dataSource.connector; - const where = buildFilter(ctx.args, (param, value) => { + const where = buildFilter(ctx.where, (param, value) => { switch (param) { case 'search': return {or: [ @@ -50,7 +49,7 @@ module.exports = Self => { } }) ?? {}; - filter = mergeFilters(ctx.args?.filter ?? {}, {where}); + filter = mergeFilters(ctx?.where ?? {}, where); const stmts = []; let stmt; @@ -67,7 +66,7 @@ module.exports = Self => { JOIN country c on c.id = p.countryFk `); - stmt.merge(conn.makeSuffix(filter)); + stmt.merge(conn.makeSuffix({where: filter, limit: ctx?.limit ?? 30})); const itemsIndex = stmts.push(stmt) - 1; const sql = ParameterizedSQL.join(stmts, ';'); From cddd8f7d94713f985c624ae9dfa1d839310dacf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Segarra=20Mart=C3=ADnez?= Date: Tue, 23 Jan 2024 16:36:53 +0100 Subject: [PATCH 17/19] refs #2687 perf: improve comments --- db/changes/240601/00-util_tx_commit.sql | 9 ++++----- db/changes/240601/00-util_tx_rollback.sql | 9 ++++----- db/changes/240601/00-util_tx_start.sql | 9 ++++----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/db/changes/240601/00-util_tx_commit.sql b/db/changes/240601/00-util_tx_commit.sql index f288e5fe3..11be92c2e 100644 --- a/db/changes/240601/00-util_tx_commit.sql +++ b/db/changes/240601/00-util_tx_commit.sql @@ -1,16 +1,15 @@ DELIMITER $$ $$ + +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_commit`(IN tx BOOL) +BEGIN /** * Procedimiento para confirmar los cambios asociados a una transacción * * @param tx BOOL es true si existe transacción asociada */ - -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_commit`(IN tx BOOL) -BEGIN IF tx THEN COMMIT; END IF; -END -$$ +END$$ DELIMITER ; diff --git a/db/changes/240601/00-util_tx_rollback.sql b/db/changes/240601/00-util_tx_rollback.sql index 3ec738fa7..b6ac2cd12 100644 --- a/db/changes/240601/00-util_tx_rollback.sql +++ b/db/changes/240601/00-util_tx_rollback.sql @@ -1,16 +1,15 @@ DELIMITER $$ $$ + +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_rollback`(tx BOOL) +BEGIN /** * Procedimiento para deshacer los cambios asociados a una transacción * * @param tx BOOL es true si existe transacción asociada */ - -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_rollback`(tx BOOL) -BEGIN IF tx THEN ROLLBACK; END IF; -END -$$ +END$$ DELIMITER ; diff --git a/db/changes/240601/00-util_tx_start.sql b/db/changes/240601/00-util_tx_start.sql index f4a970c4f..6e513790b 100644 --- a/db/changes/240601/00-util_tx_start.sql +++ b/db/changes/240601/00-util_tx_start.sql @@ -2,17 +2,16 @@ DELIMITER $$ $$ + +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_start`(tx BOOL) +BEGIN /** * Procedimiento para iniciar una transacción * * @param tx BOOL es true si existe transacción asociada */ - -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`tx_start`(tx BOOL) -BEGIN IF tx THEN START TRANSACTION; END IF; -END -$$ +END$$ DELIMITER ; From 5b2b6a15ad704a67f357b7dcc8577bc2151e7cd5 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 24 Jan 2024 08:36:08 +0100 Subject: [PATCH 18/19] refs #6694 perf: improve filter --- back/methods/postcode/filter.js | 12 ++++++--- back/methods/postcode/specs/filter.spec.js | 30 ++++++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/back/methods/postcode/filter.js b/back/methods/postcode/filter.js index 8dbe2965f..2ea70e15a 100644 --- a/back/methods/postcode/filter.js +++ b/back/methods/postcode/filter.js @@ -35,8 +35,10 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); + filter = ctx?.filter ?? {}; + const conn = Self.dataSource.connector; - const where = buildFilter(ctx.where, (param, value) => { + const where = buildFilter(filter?.where, (param, value) => { switch (param) { case 'search': return {or: [ @@ -48,13 +50,15 @@ module.exports = Self => { }; } }) ?? {}; - - filter = mergeFilters(ctx?.where ?? {}, where); + delete ctx.filter.where; const stmts = []; let stmt; stmt = new ParameterizedSQL(` SELECT + pc.townFk, + t.provinceFk, + p.countryFk, pc.code, t.name as town, p.name as province, @@ -66,7 +70,7 @@ module.exports = Self => { JOIN country c on c.id = p.countryFk `); - stmt.merge(conn.makeSuffix({where: filter, limit: ctx?.limit ?? 30})); + stmt.merge(conn.makeSuffix({where, ...ctx})); const itemsIndex = stmts.push(stmt) - 1; const sql = ParameterizedSQL.join(stmts, ';'); diff --git a/back/methods/postcode/specs/filter.spec.js b/back/methods/postcode/specs/filter.spec.js index c393b629a..60ac24809 100644 --- a/back/methods/postcode/specs/filter.spec.js +++ b/back/methods/postcode/specs/filter.spec.js @@ -7,13 +7,13 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - + filter: { }, + limit: 1 }; const results = await models.Postcode.filter(ctx, options); - expect(results.length).toBeGreaterThan(0); + expect(results.length).toEqual(1); await tx.rollback(); } catch (e) { await tx.rollback(); @@ -27,8 +27,10 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - search: 46, + filter: { + where: { + search: 46, + } }, }; const results = await models.Postcode.filter(ctx, options); @@ -47,8 +49,10 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - search: 'Alz', + filter: { + where: { + search: 'Alz', + } }, }; const results = await models.Postcode.filter(ctx, options); @@ -67,8 +71,10 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - search: 'one', + filter: { + where: { + search: 'one', + } }, }; const results = await models.Postcode.filter(ctx, options); @@ -87,8 +93,10 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - search: 'Ec', + filter: { + where: { + search: 'Ec', + } }, }; const results = await models.Postcode.filter(ctx, options); From 40d5e8662b501eae200f913545a61e70005885d8 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 24 Jan 2024 08:38:34 +0100 Subject: [PATCH 19/19] refs #6694 perf: remove unused imports --- back/methods/postcode/filter.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/back/methods/postcode/filter.js b/back/methods/postcode/filter.js index 2ea70e15a..4c3fc0cd3 100644 --- a/back/methods/postcode/filter.js +++ b/back/methods/postcode/filter.js @@ -1,6 +1,5 @@ const {ParameterizedSQL} = require('loopback-connector'); -const {buildFilter, mergeFilters} = require('vn-loopback/util/filter'); -// const {models} = require('vn-loopback/server/server'); +const {buildFilter} = require('vn-loopback/util/filter'); module.exports = Self => { Self.remoteMethod('filter', { @@ -41,12 +40,13 @@ module.exports = Self => { const where = buildFilter(filter?.where, (param, value) => { switch (param) { case 'search': - return {or: [ - {'pc.code': {like: `%${value}%`}}, - {'t.name': {like: `%${value}%`}}, - {'p.name': {like: `%${value}%`}}, - {'c.country': {like: `%${value}%`}} - ] + return { + or: [ + {'pc.code': {like: `%${value}%`}}, + {'t.name': {like: `%${value}%`}}, + {'p.name': {like: `%${value}%`}}, + {'c.country': {like: `%${value}%`}} + ] }; } }) ?? {};