From 4861b789f7182a608e495198b04453ab3040090f Mon Sep 17 00:00:00 2001 From: Gerard Date: Thu, 4 Apr 2019 12:38:54 +0200 Subject: [PATCH] Loggable now inserts the correct values for FK values --- loopback/common/models/loggable.js | 39 ++++++++++++++----- modules/client/back/models/address.json | 2 +- .../client/back/models/client-contact.json | 2 +- modules/client/back/models/client-sample.json | 2 +- modules/client/back/models/client.json | 3 +- modules/client/back/models/greuge.json | 2 +- modules/item/back/models/item-barcode.json | 2 +- modules/item/back/models/item-botanical.json | 2 +- modules/item/back/models/item-niche.json | 2 +- modules/item/back/models/item-tag.json | 2 +- modules/item/back/models/item.json | 3 +- .../back/models/ticket-observation.json | 6 ++- .../ticket/back/models/ticket-request.json | 3 +- .../ticket/back/models/ticket-service.json | 2 +- .../ticket/back/models/ticket-tracking.json | 2 +- modules/ticket/back/models/ticket-weekly.json | 4 +- 16 files changed, 52 insertions(+), 26 deletions(-) diff --git a/loopback/common/models/loggable.js b/loopback/common/models/loggable.js index c7550d10c..9ad1fcdd5 100644 --- a/loopback/common/models/loggable.js +++ b/loopback/common/models/loggable.js @@ -92,6 +92,8 @@ module.exports = function(Self) { newInstance: {} }; + delete instance.originFk; + let logModel = definition.settings.log.model; await ctx.Model.app.models[logModel].create(logRecord, options); }); @@ -111,10 +113,28 @@ module.exports = function(Self) { let val1 = ctx.Model.relations[key1]; if (val1.keyFrom == key && key != 'id') { let recordSet = await ctx.Model.app.models[val1.modelTo.modelName].findById(val, options); - let definition = val1.modelTo.definition; - let changedModelValue = definition.settings.log && definition.settings.log.changedModelValue; - val = (changedModelValue && recordSet && recordSet[changedModelValue]) || (recordSet && recordSet.id) || val; // FIXME preparar todos los modelos con campo name + let showField = val1.modelTo && val1.modelTo.definition.settings.log && val1.modelTo.definition.settings.log.showField && recordSet && recordSet[val1.modelTo.definition.settings.log.showField]; + if (!showField) { + const showFieldNames = [ + 'name', + 'description', + 'code' + ]; + for (field of showFieldNames) { + if (val1.modelTo.definition.properties && val1.modelTo.definition.properties[field] && recordSet && recordSet[field]) { + showField = field; + break; + } + } + } + + if (showField) { + val = recordSet[showField]; + break; + } + + val = recordSet && recordSet.id; // FIXME preparar todos los modelos con campo name break; } } @@ -151,7 +171,7 @@ module.exports = function(Self) { if (ctx.where && ctx.where[primaryKey]) originId = ctx.where[primaryKey]; - else { + else if (ctx.instance) { originId = ctx.instance[primaryKey]; changedModelId = ctx.instance.id; } @@ -161,18 +181,19 @@ module.exports = function(Self) { } // Sets the changedModelValue to save and the instances changed in case its an updateAll - let changedModelValue = definition.settings.log.changedModelValue; + let showField = definition.settings.log.showField; let where; - if (changedModelValue && (!ctx.instance || !ctx.instance[changedModelValue]) && ctx.where) { + if (showField && (!ctx.instance || !ctx.instance[showField]) && ctx.where) { changedModelId = []; where = []; - let changedInstances = await ctx.Model.app.models[definition.name].find({where: ctx.where, fields: ['id', changedModelValue]}, options); + let changedInstances = await ctx.Model.app.models[definition.name].find({where: ctx.where, fields: ['id', showField, primaryKey]}, options); changedInstances.forEach(element => { - where.push(element[changedModelValue]); + where.push(element[showField]); changedModelId.push(element.id); + originId = element[primaryKey]; }); } else if (ctx.hookState.oldInstance) - where = ctx.instance[changedModelValue]; + where = ctx.instance[showField]; // Set oldInstance, newInstance, userFk and action diff --git a/modules/client/back/models/address.json b/modules/client/back/models/address.json index 26a8a0487..ffc80c49b 100644 --- a/modules/client/back/models/address.json +++ b/modules/client/back/models/address.json @@ -5,7 +5,7 @@ "log": { "model": "ClientLog", "relation": "client", - "changedModelValue": "nickname" + "showField": "nickname" }, "options": { "mysql": { diff --git a/modules/client/back/models/client-contact.json b/modules/client/back/models/client-contact.json index 467dce757..ea916c072 100644 --- a/modules/client/back/models/client-contact.json +++ b/modules/client/back/models/client-contact.json @@ -5,7 +5,7 @@ "log": { "model": "ClientLog", "relation": "client", - "changedModelValue": "name" + "showField": "name" }, "options": { "mysql": { diff --git a/modules/client/back/models/client-sample.json b/modules/client/back/models/client-sample.json index e29832b4b..812da8be8 100644 --- a/modules/client/back/models/client-sample.json +++ b/modules/client/back/models/client-sample.json @@ -4,7 +4,7 @@ "log": { "model": "ClientLog", "relation": "client", - "changedModelValue": "type" + "showField": "type" }, "options": { "mysql": { diff --git a/modules/client/back/models/client.json b/modules/client/back/models/client.json index fbc41ee2d..fc542ce14 100644 --- a/modules/client/back/models/client.json +++ b/modules/client/back/models/client.json @@ -2,7 +2,8 @@ "name": "Client", "base": "Loggable", "log": { - "model":"ClientLog" + "model":"ClientLog", + "showField": "id" }, "options": { "mysql": { diff --git a/modules/client/back/models/greuge.json b/modules/client/back/models/greuge.json index 67410c4a8..50f3a321b 100644 --- a/modules/client/back/models/greuge.json +++ b/modules/client/back/models/greuge.json @@ -4,7 +4,7 @@ "log": { "model": "ClientLog", "relation": "client", - "changedModelValue": "description" + "showField": "description" }, "options": { "mysql": { diff --git a/modules/item/back/models/item-barcode.json b/modules/item/back/models/item-barcode.json index c2fa166e1..b9de53dd1 100644 --- a/modules/item/back/models/item-barcode.json +++ b/modules/item/back/models/item-barcode.json @@ -4,7 +4,7 @@ "log": { "model": "ItemLog", "relation": "item", - "changedModelValue": "code" + "showField": "code" }, "options": { "mysql": { diff --git a/modules/item/back/models/item-botanical.json b/modules/item/back/models/item-botanical.json index 2a1234e36..12248602d 100644 --- a/modules/item/back/models/item-botanical.json +++ b/modules/item/back/models/item-botanical.json @@ -4,7 +4,7 @@ "log": { "model": "ItemLog", "relation": "item", - "changedModelValue": "botanical" + "showField": "botanical" }, "options": { "mysql": { diff --git a/modules/item/back/models/item-niche.json b/modules/item/back/models/item-niche.json index aadcab28e..260eb07b9 100644 --- a/modules/item/back/models/item-niche.json +++ b/modules/item/back/models/item-niche.json @@ -4,7 +4,7 @@ "log": { "model": "ItemLog", "relation": "item", - "changedModelValue": "code" + "showField": "code" }, "options": { "mysql": { diff --git a/modules/item/back/models/item-tag.json b/modules/item/back/models/item-tag.json index d68e1a299..e276703a6 100644 --- a/modules/item/back/models/item-tag.json +++ b/modules/item/back/models/item-tag.json @@ -4,7 +4,7 @@ "log": { "model": "ItemLog", "relation": "item", - "changedModelValue": "value" + "showField": "value" }, "options": { "mysql": { diff --git a/modules/item/back/models/item.json b/modules/item/back/models/item.json index 4496633e8..7639f1a45 100644 --- a/modules/item/back/models/item.json +++ b/modules/item/back/models/item.json @@ -2,7 +2,8 @@ "name": "Item", "base": "Loggable", "log": { - "model":"ItemLog" + "model":"ItemLog", + "showField": "id" }, "options": { "mysql": { diff --git a/modules/ticket/back/models/ticket-observation.json b/modules/ticket/back/models/ticket-observation.json index e5b0169c1..97284377a 100644 --- a/modules/ticket/back/models/ticket-observation.json +++ b/modules/ticket/back/models/ticket-observation.json @@ -1,6 +1,10 @@ { "name": "TicketObservation", - "base": "VnModel", + "base": "Loggable", + "log": { + "model": "TicketLog", + "relation": "ticket" + }, "options": { "mysql": { "table": "ticketObservation" diff --git a/modules/ticket/back/models/ticket-request.json b/modules/ticket/back/models/ticket-request.json index 32d3163e2..dfb609e3c 100644 --- a/modules/ticket/back/models/ticket-request.json +++ b/modules/ticket/back/models/ticket-request.json @@ -3,8 +3,7 @@ "base": "Loggable", "log": { "model": "TicketLog", - "relation": "ticket", - "changedModelValue": "description" + "relation": "ticket" }, "options": { "mysql": { diff --git a/modules/ticket/back/models/ticket-service.json b/modules/ticket/back/models/ticket-service.json index e14ff68ec..b3878e89e 100644 --- a/modules/ticket/back/models/ticket-service.json +++ b/modules/ticket/back/models/ticket-service.json @@ -4,7 +4,7 @@ "log": { "model": "TicketLog", "relation": "ticket", - "changedModelValue": "description" + "showField": "description" }, "options": { "mysql": { diff --git a/modules/ticket/back/models/ticket-tracking.json b/modules/ticket/back/models/ticket-tracking.json index 3512643d1..550df7d9c 100644 --- a/modules/ticket/back/models/ticket-tracking.json +++ b/modules/ticket/back/models/ticket-tracking.json @@ -4,7 +4,7 @@ "log": { "model": "TicketLog", "relation": "ticket", - "changedModelValue": "stateFk" + "showField": "stateFk" }, "options": { "mysql": { diff --git a/modules/ticket/back/models/ticket-weekly.json b/modules/ticket/back/models/ticket-weekly.json index 7c107659a..c19e79bc1 100644 --- a/modules/ticket/back/models/ticket-weekly.json +++ b/modules/ticket/back/models/ticket-weekly.json @@ -4,7 +4,7 @@ "log": { "model": "TicketLog", "relation": "ticket", - "changedModelValue": "ticketFk" + "showField": "ticketFk" }, "options": { "mysql": { @@ -27,4 +27,4 @@ "foreignKey": "ticketFk" } } -} +} \ No newline at end of file