From e16d43b2fc7e9b787bc9cd2b8b44719c999b69ef Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Thu, 12 Sep 2019 09:49:02 +0200 Subject: [PATCH] Allow disable log on model properties --- loopback/common/models/loggable.js | 31 +++++++++++++++++++++++++- modules/order/front/catalog/index.html | 5 +++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/loopback/common/models/loggable.js b/loopback/common/models/loggable.js index 781509e92..e62015440 100644 --- a/loopback/common/models/loggable.js +++ b/loopback/common/models/loggable.js @@ -211,6 +211,9 @@ module.exports = function(Self) { let action = setActionType(ctx); + removeUnloggableProperties(definition, oldInstance); + removeUnloggableProperties(definition, newInstance); + let logRecord = { originFk: originId, userFk: userFk, @@ -223,12 +226,38 @@ module.exports = function(Self) { }; let logsToSave = setLogsToSave(where, changedModelId, logRecord, ctx); - let logModel = definition.settings.log.model; await ctx.Model.app.models[logModel].create(logsToSave, options); } + /** + * Removes unwanted properties + * @param {*} definition Model definition + * @param {*} properties Modified object properties + */ + function removeUnloggableProperties(definition, properties) { + const propList = Object.keys(properties); + const propDefs = new Map(); + + for (let property in definition.properties) { + const propertyDef = definition.properties[property]; + + propDefs.set(property, propertyDef); + } + + for (let property of propList) { + const propertyDef = propDefs.get(property); + + if (!propertyDef) return; + + if (propertyDef.log === false) + delete properties[property]; + else if (propertyDef.logValue === false) + properties[property] = null; + } + } + // this function retuns all the instances changed in case this is an updateAll function setLogsToSave(changedInstances, changedInstancesIds, logRecord, ctx) { let promises = []; diff --git a/modules/order/front/catalog/index.html b/modules/order/front/catalog/index.html index 550ac5cd9..0ede05776 100644 --- a/modules/order/front/catalog/index.html +++ b/modules/order/front/catalog/index.html @@ -96,6 +96,11 @@ vn-tooltip="Add"> +