From cf527dd8be096c0c2d828b6db3f08eec23a0debe Mon Sep 17 00:00:00 2001 From: alexandre Date: Fri, 23 Dec 2022 09:37:19 +0100 Subject: [PATCH 1/7] refs #4588 hook added --- db/changes/225201/00-lastUser.sql | 54 +++++++++++++++++++ .../entry/back/models/entry-observation.js | 26 +++++++++ modules/entry/back/models/entry.json | 8 +++ 3 files changed, 88 insertions(+) create mode 100644 db/changes/225201/00-lastUser.sql diff --git a/db/changes/225201/00-lastUser.sql b/db/changes/225201/00-lastUser.sql new file mode 100644 index 000000000..151038064 --- /dev/null +++ b/db/changes/225201/00-lastUser.sql @@ -0,0 +1,54 @@ +ALTER TABLE `vn`.`entry` ADD observationEditorFk INT(10) unsigned NULL COMMENT 'Último usuario que ha modificado el campo evaNotes'; +ALTER TABLE `vn`.`entry` ADD CONSTRAINT entry_observationEditorFk FOREIGN KEY (observationEditorFk) REFERENCES account.user(id) ON UPDATE CASCADE; + +CREATE OR REPLACE DEFINER=`root`@`localhost` +VIEW `vn2008`.`entrySource` AS +select + `e`.`gestDocFk` AS `gestdoc_id`, + `e`.`id` AS `Id_Entrada`, + `e`.`invoiceNumber` AS `invoiceNumber`, + `e`.`reference` AS `reference`, + `e`.`isExcludedFromAvailable` AS `Inventario`, + `e`.`notes` AS `observaciones`, + `e`.`isConfirmed` AS `Confirmada`, + `e`.`isOrdered` AS `Pedida`, + `e`.`isRaid` AS `Redada`, + `e`.`evaNotes` AS `notas`, + `e`.`supplierFk` AS `Id_Proveedor`, + `tr`.`shipped` AS `shipment`, + `tr`.`landed` AS `landing`, + `w2`.`name` AS `wh_in`, + `w1`.`name` AS `wh_out`, + `am`.`name` AS `Agencia`, + `e`.`commission` AS `comision`, + `tr`.`warehouseInFk` AS `warehouse_id`, + `w1`.`id` AS `warehouse_id_out`, + `e`.`isBooked` AS `anotadoencaja`, + `e`.`invoiceInFk` AS `invoiceInFk`, + `e`.`companyFk` AS `empresa_id`, + `e`.`currencyFk` AS `Id_Moneda`, + `tr`.`id` AS `TravelFk`, + `e`.`sub` AS `sub`, + `e`.`kop` AS `kop`, + `e`.`pro` AS `pro`, + `e`.`invoiceAmount` AS `invoiceAmount`, + `w`.`code` AS `buyerCode`, + `e`.`typeFk` AS `typeFk`, + `w3`.`code` AS `observationWorkerCode` +from + (((((((`vn`.`entry` `e` +left join `vn`.`travel` `tr` on + (`e`.`travelFk` = `tr`.`id`)) +left join `vn`.`agencyMode` `am` on + (`am`.`id` = `tr`.`agencyModeFk`)) +left join `vn`.`warehouse` `w1` on + (`tr`.`warehouseOutFk` = `w1`.`id`)) +left join `vn`.`warehouse` `w2` on + (`tr`.`warehouseInFk` = `w2`.`id`)) +left join `vn`.`supplier` `s` on + (`e`.`supplierFk` = `s`.`id`)) +left join `vn`.`worker` `w` on + (`w`.`id` = `e`.`buyerFk`)) +left join `vn`.`worker` `w3` on + (`w3`.`id` = `e`.`observationEditorFk`)); + diff --git a/modules/entry/back/models/entry-observation.js b/modules/entry/back/models/entry-observation.js index 77d15d85c..5a89a9e4b 100644 --- a/modules/entry/back/models/entry-observation.js +++ b/modules/entry/back/models/entry-observation.js @@ -1,4 +1,5 @@ const UserError = require('vn-loopback/util/user-error'); +const LoopBackContext = require('loopback-context'); module.exports = Self => { Self.rewriteDbError(function(err) { @@ -6,4 +7,29 @@ module.exports = Self => { return new UserError(`The observation type can't be repeated`); return err; }); + + Self.observe('before save', async function(ctx, options) { + const loopBackContext = LoopBackContext.getCurrentContext(); + const userId = loopBackContext.active.accessToken.userId; + const entryId = ctx.instance.entryFk; + let tx; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const entry = await Self.app.models.Entry.findOne({where: {id: entryId}}, myOptions); + await entry.updateAttribute('observationEditorFk', userId, myOptions); + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }); }; diff --git a/modules/entry/back/models/entry.json b/modules/entry/back/models/entry.json index d9b7c52df..6c5e1b7d3 100644 --- a/modules/entry/back/models/entry.json +++ b/modules/entry/back/models/entry.json @@ -77,6 +77,9 @@ "companyFk": { "type": "number", "required": true + }, + "observationEditorFk": { + "type": "number" } }, "relations": { @@ -99,6 +102,11 @@ "type": "belongsTo", "model": "Currency", "foreignKey": "currencyFk" + }, + "observationEditor": { + "type": "belongsTo", + "model": "Account", + "foreignKey": "observationEditorFk" } } } From c3977577620cebb0f511cdf6e2181e40141130eb Mon Sep 17 00:00:00 2001 From: alexandre Date: Mon, 6 Feb 2023 12:26:54 +0100 Subject: [PATCH 2/7] sql version updated --- db/changes/230601/.gitkeep | 0 db/changes/{225201 => 230601}/00-lastUser.sql | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 db/changes/230601/.gitkeep rename db/changes/{225201 => 230601}/00-lastUser.sql (100%) diff --git a/db/changes/230601/.gitkeep b/db/changes/230601/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/db/changes/225201/00-lastUser.sql b/db/changes/230601/00-lastUser.sql similarity index 100% rename from db/changes/225201/00-lastUser.sql rename to db/changes/230601/00-lastUser.sql From d3564209ba9f26d88dcbfbdcf17f499e435916fb Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 7 Feb 2023 08:05:48 +0100 Subject: [PATCH 3/7] findById --- modules/entry/back/models/entry-observation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/entry/back/models/entry-observation.js b/modules/entry/back/models/entry-observation.js index 5a89a9e4b..f4782a733 100644 --- a/modules/entry/back/models/entry-observation.js +++ b/modules/entry/back/models/entry-observation.js @@ -24,7 +24,7 @@ module.exports = Self => { } try { - const entry = await Self.app.models.Entry.findOne({where: {id: entryId}}, myOptions); + const entry = await Self.app.models.Entry.findById(entryId, null, myOptions); await entry.updateAttribute('observationEditorFk', userId, myOptions); if (tx) await tx.commit(); } catch (e) { From 549d530690a4db07ca91122fff695615f6f81c2b Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 8 Feb 2023 07:35:52 +0100 Subject: [PATCH 4/7] . --- db/changes/230601/00-lastUser.sql | 54 ------------------------------- 1 file changed, 54 deletions(-) delete mode 100644 db/changes/230601/00-lastUser.sql diff --git a/db/changes/230601/00-lastUser.sql b/db/changes/230601/00-lastUser.sql deleted file mode 100644 index 151038064..000000000 --- a/db/changes/230601/00-lastUser.sql +++ /dev/null @@ -1,54 +0,0 @@ -ALTER TABLE `vn`.`entry` ADD observationEditorFk INT(10) unsigned NULL COMMENT 'Último usuario que ha modificado el campo evaNotes'; -ALTER TABLE `vn`.`entry` ADD CONSTRAINT entry_observationEditorFk FOREIGN KEY (observationEditorFk) REFERENCES account.user(id) ON UPDATE CASCADE; - -CREATE OR REPLACE DEFINER=`root`@`localhost` -VIEW `vn2008`.`entrySource` AS -select - `e`.`gestDocFk` AS `gestdoc_id`, - `e`.`id` AS `Id_Entrada`, - `e`.`invoiceNumber` AS `invoiceNumber`, - `e`.`reference` AS `reference`, - `e`.`isExcludedFromAvailable` AS `Inventario`, - `e`.`notes` AS `observaciones`, - `e`.`isConfirmed` AS `Confirmada`, - `e`.`isOrdered` AS `Pedida`, - `e`.`isRaid` AS `Redada`, - `e`.`evaNotes` AS `notas`, - `e`.`supplierFk` AS `Id_Proveedor`, - `tr`.`shipped` AS `shipment`, - `tr`.`landed` AS `landing`, - `w2`.`name` AS `wh_in`, - `w1`.`name` AS `wh_out`, - `am`.`name` AS `Agencia`, - `e`.`commission` AS `comision`, - `tr`.`warehouseInFk` AS `warehouse_id`, - `w1`.`id` AS `warehouse_id_out`, - `e`.`isBooked` AS `anotadoencaja`, - `e`.`invoiceInFk` AS `invoiceInFk`, - `e`.`companyFk` AS `empresa_id`, - `e`.`currencyFk` AS `Id_Moneda`, - `tr`.`id` AS `TravelFk`, - `e`.`sub` AS `sub`, - `e`.`kop` AS `kop`, - `e`.`pro` AS `pro`, - `e`.`invoiceAmount` AS `invoiceAmount`, - `w`.`code` AS `buyerCode`, - `e`.`typeFk` AS `typeFk`, - `w3`.`code` AS `observationWorkerCode` -from - (((((((`vn`.`entry` `e` -left join `vn`.`travel` `tr` on - (`e`.`travelFk` = `tr`.`id`)) -left join `vn`.`agencyMode` `am` on - (`am`.`id` = `tr`.`agencyModeFk`)) -left join `vn`.`warehouse` `w1` on - (`tr`.`warehouseOutFk` = `w1`.`id`)) -left join `vn`.`warehouse` `w2` on - (`tr`.`warehouseInFk` = `w2`.`id`)) -left join `vn`.`supplier` `s` on - (`e`.`supplierFk` = `s`.`id`)) -left join `vn`.`worker` `w` on - (`w`.`id` = `e`.`buyerFk`)) -left join `vn`.`worker` `w3` on - (`w3`.`id` = `e`.`observationEditorFk`)); - From 139dac0bc1df5d3bf6325262e4b528caf3422c45 Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 14 Feb 2023 08:08:48 +0100 Subject: [PATCH 5/7] refs #4588 moved hook from entryObservation to entry --- .../entry/back/models/entry-observation.js | 26 ------------- modules/entry/back/models/entry.js | 38 +++++++++++++++++++ 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/modules/entry/back/models/entry-observation.js b/modules/entry/back/models/entry-observation.js index f4782a733..77d15d85c 100644 --- a/modules/entry/back/models/entry-observation.js +++ b/modules/entry/back/models/entry-observation.js @@ -1,5 +1,4 @@ const UserError = require('vn-loopback/util/user-error'); -const LoopBackContext = require('loopback-context'); module.exports = Self => { Self.rewriteDbError(function(err) { @@ -7,29 +6,4 @@ module.exports = Self => { return new UserError(`The observation type can't be repeated`); return err; }); - - Self.observe('before save', async function(ctx, options) { - const loopBackContext = LoopBackContext.getCurrentContext(); - const userId = loopBackContext.active.accessToken.userId; - const entryId = ctx.instance.entryFk; - let tx; - const myOptions = {}; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; - } - - try { - const entry = await Self.app.models.Entry.findById(entryId, null, myOptions); - await entry.updateAttribute('observationEditorFk', userId, myOptions); - if (tx) await tx.commit(); - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }); }; diff --git a/modules/entry/back/models/entry.js b/modules/entry/back/models/entry.js index 4854bc3d3..1980f964c 100644 --- a/modules/entry/back/models/entry.js +++ b/modules/entry/back/models/entry.js @@ -1,3 +1,4 @@ +const LoopBackContext = require('loopback-context'); module.exports = Self => { require('../methods/entry/filter')(Self); require('../methods/entry/getEntry')(Self); @@ -7,4 +8,41 @@ module.exports = Self => { require('../methods/entry/importBuysPreview')(Self); require('../methods/entry/lastItemBuys')(Self); require('../methods/entry/entryOrderPdf')(Self); + + Self.observe('before save', async function(ctx, options) { + if (ctx.isNewInstance) return; + + const changes = ctx.data || ctx.instance; + const orgData = ctx.currentInstance; + + const observation = changes.observation || orgData.observation; + const hasChanges = orgData && changes; + const observationChanged = hasChanges + && orgData.observation != observation; + + if (observationChanged) { + let tx; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const loopbackContext = LoopBackContext.getCurrentContext(); + const userId = loopbackContext.active.accessToken.userId; + const id = changes.id || orgData.id; + const entry = await Self.app.models.Entry.findById(id, null, myOptions); + await entry.updateAttribute('observationEditorFk', userId, myOptions); + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + } + }); }; From 7d5591462dbccaf2feecc1d1f558d91364191daa Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 14 Feb 2023 09:08:49 +0100 Subject: [PATCH 6/7] refs #4588 entry sql --- db/changes/230601/00-entry.sql | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 db/changes/230601/00-entry.sql diff --git a/db/changes/230601/00-entry.sql b/db/changes/230601/00-entry.sql new file mode 100644 index 000000000..f62433aaf --- /dev/null +++ b/db/changes/230601/00-entry.sql @@ -0,0 +1,53 @@ +ALTER TABLE `vn`.`entry` ADD observationEditorFk INT(10) unsigned NULL COMMENT 'Último usuario que ha modificado el campo evaNotes'; +ALTER TABLE `vn`.`entry` ADD CONSTRAINT entry_observationEditorFk FOREIGN KEY (observationEditorFk) REFERENCES account.user(id) ON UPDATE CASCADE; + +CREATE OR REPLACE DEFINER=`root`@`localhost` +VIEW `vn2008`.`entrySource` AS +select + `e`.`gestDocFk` AS `gestdoc_id`, + `e`.`id` AS `Id_Entrada`, + `e`.`invoiceNumber` AS `invoiceNumber`, + `e`.`reference` AS `reference`, + `e`.`isExcludedFromAvailable` AS `Inventario`, + `e`.`notes` AS `observaciones`, + `e`.`isConfirmed` AS `Confirmada`, + `e`.`isOrdered` AS `Pedida`, + `e`.`isRaid` AS `Redada`, + `e`.`evaNotes` AS `notas`, + `e`.`supplierFk` AS `Id_Proveedor`, + `tr`.`shipped` AS `shipment`, + `tr`.`landed` AS `landing`, + `w2`.`name` AS `wh_in`, + `w1`.`name` AS `wh_out`, + `am`.`name` AS `Agencia`, + `e`.`commission` AS `comision`, + `tr`.`warehouseInFk` AS `warehouse_id`, + `w1`.`id` AS `warehouse_id_out`, + `e`.`isBooked` AS `anotadoencaja`, + `e`.`invoiceInFk` AS `invoiceInFk`, + `e`.`companyFk` AS `empresa_id`, + `e`.`currencyFk` AS `Id_Moneda`, + `tr`.`id` AS `TravelFk`, + `e`.`sub` AS `sub`, + `e`.`kop` AS `kop`, + `e`.`pro` AS `pro`, + `e`.`invoiceAmount` AS `invoiceAmount`, + `w`.`code` AS `buyerCode`, + `e`.`typeFk` AS `typeFk`, + `w3`.`code` AS `observationWorkerCode` +from + (((((((`vn`.`entry` `e` +left join `vn`.`travel` `tr` on + (`e`.`travelFk` = `tr`.`id`)) +left join `vn`.`agencyMode` `am` on + (`am`.`id` = `tr`.`agencyModeFk`)) +left join `vn`.`warehouse` `w1` on + (`tr`.`warehouseOutFk` = `w1`.`id`)) +left join `vn`.`warehouse` `w2` on + (`tr`.`warehouseInFk` = `w2`.`id`)) +left join `vn`.`supplier` `s` on + (`e`.`supplierFk` = `s`.`id`)) +left join `vn`.`worker` `w` on + (`w`.`id` = `e`.`buyerFk`)) +left join `vn`.`worker` `w3` on + (`w3`.`id` = `e`.`observationEditorFk`)); From 7194e62cae4717b00804e5187d9b07386dde38cd Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 14 Feb 2023 13:28:21 +0100 Subject: [PATCH 7/7] refs #4588 faked observationEditorFk in structure --- db/changes/230601/00-entry.sql | 53 ---------------------------------- db/dump/structure.sql | 4 ++- 2 files changed, 3 insertions(+), 54 deletions(-) delete mode 100644 db/changes/230601/00-entry.sql diff --git a/db/changes/230601/00-entry.sql b/db/changes/230601/00-entry.sql deleted file mode 100644 index f62433aaf..000000000 --- a/db/changes/230601/00-entry.sql +++ /dev/null @@ -1,53 +0,0 @@ -ALTER TABLE `vn`.`entry` ADD observationEditorFk INT(10) unsigned NULL COMMENT 'Último usuario que ha modificado el campo evaNotes'; -ALTER TABLE `vn`.`entry` ADD CONSTRAINT entry_observationEditorFk FOREIGN KEY (observationEditorFk) REFERENCES account.user(id) ON UPDATE CASCADE; - -CREATE OR REPLACE DEFINER=`root`@`localhost` -VIEW `vn2008`.`entrySource` AS -select - `e`.`gestDocFk` AS `gestdoc_id`, - `e`.`id` AS `Id_Entrada`, - `e`.`invoiceNumber` AS `invoiceNumber`, - `e`.`reference` AS `reference`, - `e`.`isExcludedFromAvailable` AS `Inventario`, - `e`.`notes` AS `observaciones`, - `e`.`isConfirmed` AS `Confirmada`, - `e`.`isOrdered` AS `Pedida`, - `e`.`isRaid` AS `Redada`, - `e`.`evaNotes` AS `notas`, - `e`.`supplierFk` AS `Id_Proveedor`, - `tr`.`shipped` AS `shipment`, - `tr`.`landed` AS `landing`, - `w2`.`name` AS `wh_in`, - `w1`.`name` AS `wh_out`, - `am`.`name` AS `Agencia`, - `e`.`commission` AS `comision`, - `tr`.`warehouseInFk` AS `warehouse_id`, - `w1`.`id` AS `warehouse_id_out`, - `e`.`isBooked` AS `anotadoencaja`, - `e`.`invoiceInFk` AS `invoiceInFk`, - `e`.`companyFk` AS `empresa_id`, - `e`.`currencyFk` AS `Id_Moneda`, - `tr`.`id` AS `TravelFk`, - `e`.`sub` AS `sub`, - `e`.`kop` AS `kop`, - `e`.`pro` AS `pro`, - `e`.`invoiceAmount` AS `invoiceAmount`, - `w`.`code` AS `buyerCode`, - `e`.`typeFk` AS `typeFk`, - `w3`.`code` AS `observationWorkerCode` -from - (((((((`vn`.`entry` `e` -left join `vn`.`travel` `tr` on - (`e`.`travelFk` = `tr`.`id`)) -left join `vn`.`agencyMode` `am` on - (`am`.`id` = `tr`.`agencyModeFk`)) -left join `vn`.`warehouse` `w1` on - (`tr`.`warehouseOutFk` = `w1`.`id`)) -left join `vn`.`warehouse` `w2` on - (`tr`.`warehouseInFk` = `w2`.`id`)) -left join `vn`.`supplier` `s` on - (`e`.`supplierFk` = `s`.`id`)) -left join `vn`.`worker` `w` on - (`w`.`id` = `e`.`buyerFk`)) -left join `vn`.`worker` `w3` on - (`w3`.`id` = `e`.`observationEditorFk`)); diff --git a/db/dump/structure.sql b/db/dump/structure.sql index 4626279e4..3186fa01f 100644 --- a/db/dump/structure.sql +++ b/db/dump/structure.sql @@ -26286,6 +26286,7 @@ CREATE TABLE `entry` ( `typeFk` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT 'Tipo de entrada', `reference` varchar(50) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT 'Referencia para eti', `ref` varchar(50) GENERATED ALWAYS AS (`invoiceNumber`) VIRTUAL COMMENT 'Columna virtual provisional para Salix', + `observationEditorFk` INT(10) unsigned NULL COMMENT 'Último usuario que ha modificado el campo evaNotes', PRIMARY KEY (`id`), KEY `Id_Proveedor` (`supplierFk`), KEY `Fecha` (`dated`), @@ -26300,7 +26301,8 @@ CREATE TABLE `entry` ( CONSTRAINT `entry_FK_1` FOREIGN KEY (`typeFk`) REFERENCES `entryType` (`code`) ON UPDATE CASCADE, CONSTRAINT `entry_ibfk_1` FOREIGN KEY (`supplierFk`) REFERENCES `supplier` (`id`) ON UPDATE CASCADE, CONSTRAINT `entry_ibfk_6` FOREIGN KEY (`travelFk`) REFERENCES `travel` (`id`) ON UPDATE CASCADE, - CONSTRAINT `entry_ibfk_7` FOREIGN KEY (`companyFk`) REFERENCES `company` (`id`) ON UPDATE CASCADE + CONSTRAINT `entry_ibfk_7` FOREIGN KEY (`companyFk`) REFERENCES `company` (`id`) ON UPDATE CASCADE, + CONSTRAINT `entry_observationEditorFk` FOREIGN KEY (`observationEditorFk`) REFERENCES `account`.`user`(`id`) ON UPDATE CASCADE ) ENGINE=InnoDBDEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci COMMENT='InnoDB free: 88064 kB; (`Id_Proveedor`) REFER `vn2008/Provee'; /*!40101 SET character_set_client = @saved_cs_client */; /*!50003 SET @saved_cs_client = @@character_set_client */ ;