From 139dac0bc1df5d3bf6325262e4b528caf3422c45 Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 14 Feb 2023 08:08:48 +0100 Subject: [PATCH] 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; + } + } + }); };