From c512192052c6ba2e5a2785d424a962633740c45c Mon Sep 17 00:00:00 2001 From: Gerard Date: Mon, 22 Oct 2018 14:35:21 +0200 Subject: [PATCH] #707 logs activated in all client module --- .../client/common/models/client-contact.json | 7 +- .../common/models/client-observation.json | 10 +- .../client/common/models/client-sample.json | 7 +- services/client/common/models/greuge.json | 7 +- services/client/common/models/recovery.json | 6 +- services/loopback/common/models/address.json | 7 +- .../loopback/common/models/client-log.json | 4 +- services/loopback/common/models/loggable.js | 91 ++++++++++++------- 8 files changed, 98 insertions(+), 41 deletions(-) diff --git a/services/client/common/models/client-contact.json b/services/client/common/models/client-contact.json index 681b4e023..0c06af814 100644 --- a/services/client/common/models/client-contact.json +++ b/services/client/common/models/client-contact.json @@ -1,7 +1,12 @@ { "name": "ClientContact", "description": "Client phone contacts", - "base": "VnModel", + "base": "Loggable", + "log": { + "model": "ClientLog", + "relation": "client", + "changedModelValue": "name" + }, "options": { "mysql": { "table": "clientContact", diff --git a/services/client/common/models/client-observation.json b/services/client/common/models/client-observation.json index 47654b134..ea05bed55 100644 --- a/services/client/common/models/client-observation.json +++ b/services/client/common/models/client-observation.json @@ -1,7 +1,11 @@ { "name": "ClientObservation", "description": "Client notes", - "base": "VnModel", + "base": "Loggable", + "log": { + "model": "ClientLog", + "relation": "client" + }, "options": { "mysql": { "table": "clientObservation" @@ -32,9 +36,9 @@ "foreignKey": "workerFk" }, "client": { - "type": "hasOne", + "type": "belongsTo", "model": "Client", - "foreignKey": "id" + "foreignKey": "clientFk" } }, "scope": { diff --git a/services/client/common/models/client-sample.json b/services/client/common/models/client-sample.json index e5850c6b1..e29832b4b 100644 --- a/services/client/common/models/client-sample.json +++ b/services/client/common/models/client-sample.json @@ -1,6 +1,11 @@ { "name": "ClientSample", - "base": "VnModel", + "base": "Loggable", + "log": { + "model": "ClientLog", + "relation": "client", + "changedModelValue": "type" + }, "options": { "mysql": { "table": "clientSample" diff --git a/services/client/common/models/greuge.json b/services/client/common/models/greuge.json index 36aa19ff8..67410c4a8 100644 --- a/services/client/common/models/greuge.json +++ b/services/client/common/models/greuge.json @@ -1,6 +1,11 @@ { "name": "Greuge", - "base": "VnModel", + "base": "Loggable", + "log": { + "model": "ClientLog", + "relation": "client", + "changedModelValue": "description" + }, "options": { "mysql": { "table": "greuge" diff --git a/services/client/common/models/recovery.json b/services/client/common/models/recovery.json index ef1bbb0cc..5a26cd842 100644 --- a/services/client/common/models/recovery.json +++ b/services/client/common/models/recovery.json @@ -1,6 +1,10 @@ { "name": "Recovery", - "base": "VnModel", + "base": "Loggable", + "log": { + "model": "ClientLog", + "relation": "client" + }, "options": { "mysql": { "table": "recovery" diff --git a/services/loopback/common/models/address.json b/services/loopback/common/models/address.json index cc72cb3d4..283fc140c 100644 --- a/services/loopback/common/models/address.json +++ b/services/loopback/common/models/address.json @@ -1,7 +1,12 @@ { "name": "Address", "description": "Client addresses", - "base": "VnModel", + "base": "Loggable", + "log": { + "model": "ClientLog", + "relation": "client", + "changedModelValue": "nickname" + }, "options": { "mysql": { "table": "address" diff --git a/services/loopback/common/models/client-log.json b/services/loopback/common/models/client-log.json index 054cb88e5..110ff7b73 100644 --- a/services/loopback/common/models/client-log.json +++ b/services/loopback/common/models/client-log.json @@ -24,7 +24,7 @@ "required": true }, "changedModel": { - "type": "Object" + "type": "String" }, "oldInstance": { "type": "Object" @@ -39,7 +39,7 @@ "type": "Number" }, "changedModelValue": { - "type": "Number" + "type": "String" } }, "relations": { diff --git a/services/loopback/common/models/loggable.js b/services/loopback/common/models/loggable.js index 02a7d9d1f..6cc7298c6 100644 --- a/services/loopback/common/models/loggable.js +++ b/services/loopback/common/models/loggable.js @@ -6,7 +6,7 @@ module.exports = function(Self) { Self.super_.setup.call(this); }; -/* Self.observe('after save', async function(ctx) { + Self.observe('after save', async function(ctx) { const loopBackContext = LoopBackContext.getCurrentContext(); await logInModel(ctx, loopBackContext); }); @@ -15,10 +15,15 @@ module.exports = function(Self) { let oldInstance; let oldInstanceFk; let newInstance; + if (ctx.data) { oldInstanceFk = pick(ctx.currentInstance, Object.keys(ctx.data)); newInstance = await fkToValue(ctx.data, ctx); oldInstance = await fkToValue(oldInstanceFk, ctx); + if (ctx.where && !ctx.currentInstance) { + let fields = Object.keys(ctx.data); + ctx.oldInstances = await Self.modelBuilder.models[ctx.Model.definition.name].find({where: ctx.where, fields: fields}); + } } if (ctx.isNewInstance) { newInstance = await fkToValue(ctx.instance.__data, ctx); @@ -28,53 +33,79 @@ module.exports = function(Self) { }); Self.observe('before delete', async function(ctx) { - let oldInstance; - // console.log(ctx.where); if (ctx.where) { let affectedModel = ctx.Model.definition.name; - // console.log(affectedModel); + let definition = ctx.Model.definition; let deletedInstances = await Self.modelBuilder.models[affectedModel].find({where: ctx.where}); - // console.log(deletedInstances); + let relation = definition.settings.log.relation; - let arrangedDeletedInstances = []; - deletedInstances.forEach(async element => { - console.log(element); - arrangedDeletedInstances.push(await fkToValue(element, ctx)); - }); + if (relation) { + let primaryKey = ctx.Model.relations[relation].keyFrom; - console.log(arrangedDeletedInstances); - // let deletedIntancesData = await fkToValue(deletedInstances, ctx); - // console.log(deletedIntancesData); - // oldInstanceFk = pick(ctx.currentInstance, Object.keys(ctx.data)); - // oldInstance = await fkToValue(oldInstanceFk, ctx); + let arrangedDeletedInstances = []; + for (let i = 0; i < deletedInstances.length; i++) { + if (primaryKey) + deletedInstances[i].originFk = deletedInstances[i][primaryKey]; + let arrangedInstance = await fkToValue(deletedInstances[i], ctx); + arrangedDeletedInstances[i] = arrangedInstance; + } + ctx.hookState.oldInstance = arrangedDeletedInstances; + } } - ctx.hookState.oldInstance = oldInstance; }); Self.observe('after delete', async function(ctx) { const loopBackContext = LoopBackContext.getCurrentContext(); - await logInModel(ctx, loopBackContext); - }); */ + if (ctx.hookState.oldInstance) + logDeletedInstances(ctx, loopBackContext); + }); + + async function logDeletedInstances(ctx, loopBackContext) { + ctx.hookState.oldInstance.forEach(async instance => { + let userFk; + if (loopBackContext) + userFk = loopBackContext.active.accessToken.userId; + + let definition = ctx.Model.definition; + + let changedModelValue = definition.settings.log.changedModelValue; + let logRecord = { + originFk: instance.originFk, + userFk: userFk, + action: 'delete', + changedModel: ctx.Model.definition.name, + changedModelId: instance.id, + changedModelValue: instance[changedModelValue], + oldInstance: instance, + newInstance: {} + }; + + let transaction = {}; + if (ctx.options && ctx.options.transaction) { + transaction = ctx.options.transaction; + } + + let logModel = definition.settings.log.model; + await Self.modelBuilder.models[logModel].create(logRecord, transaction); + }); + } async function fkToValue(instance, ctx) { + let cleanInstance = JSON.parse(JSON.stringify(instance)); let result = {}; - for (let key in instance) { - if (key == 'id') continue; - let val = instance[key]; - console.log(val); + for (let key in cleanInstance) { + let val = cleanInstance[key]; if (val === undefined || val === null) continue; for (let key1 in ctx.Model.relations) { let val1 = ctx.Model.relations[key1]; - if (val1.keyFrom == key) { + if (val1.keyFrom == key && key != 'id') { let recordSet = await val1.modelTo.findById(val); val = recordSet.name; // FIXME preparar todos los modelos con campo name - console.log(val); break; } } result[key] = val; } - // console.log(result); return result; } @@ -111,8 +142,6 @@ module.exports = function(Self) { changedModelId = ctx.currentInstance.id; } - // console.log(ctx.instance, ctx.where, ctx.currentInstance); - // Sets the changedModelValue to save and the instances changed in case its an updateAll let changedModelValue = definition.settings.log.changedModelValue; if (changedModelValue && (!ctx.instance || !ctx.instance[changedModelValue])) { @@ -126,7 +155,6 @@ module.exports = function(Self) { } else if (ctx.hookState.oldInstance) { where = ctx.instance[changedModelValue]; } - // console.log(where); // Set oldInstance, newInstance, userFk and action let oldInstance = {}; @@ -156,7 +184,7 @@ module.exports = function(Self) { newInstance: newInstance }; - let logsToSave = setLogsToSave(where, changedModelId, logRecord); + let logsToSave = setLogsToSave(where, changedModelId, logRecord, ctx); let logModel = definition.settings.log.model; @@ -165,17 +193,18 @@ module.exports = function(Self) { transaction = ctx.options.transaction; } - // console.log(logsToSave); await Self.modelBuilder.models[logModel].create(logsToSave, transaction); } // this function retuns all the instances changed in case this is an updateAll - function setLogsToSave(changedInstances, changedInstancesIds, logRecord) { + function setLogsToSave(changedInstances, changedInstancesIds, logRecord, ctx) { let promises = []; if (changedInstances && typeof changedInstances == "object") { for (let i = 0; i < changedInstances.length; i++) { logRecord.changedModelId = changedInstancesIds[i]; logRecord.changedModelValue = changedInstances[i]; + if (ctx.oldInstances) + logRecord.oldInstance = ctx.oldInstances[i]; promises.push(JSON.parse(JSON.stringify(logRecord))); } } else {