#707 logs activated in all client module

This commit is contained in:
Gerard 2018-10-22 14:35:21 +02:00
parent fa8825d4d0
commit c512192052
8 changed files with 98 additions and 41 deletions

View File

@ -1,7 +1,12 @@
{ {
"name": "ClientContact", "name": "ClientContact",
"description": "Client phone contacts", "description": "Client phone contacts",
"base": "VnModel", "base": "Loggable",
"log": {
"model": "ClientLog",
"relation": "client",
"changedModelValue": "name"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "clientContact", "table": "clientContact",

View File

@ -1,7 +1,11 @@
{ {
"name": "ClientObservation", "name": "ClientObservation",
"description": "Client notes", "description": "Client notes",
"base": "VnModel", "base": "Loggable",
"log": {
"model": "ClientLog",
"relation": "client"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "clientObservation" "table": "clientObservation"
@ -32,9 +36,9 @@
"foreignKey": "workerFk" "foreignKey": "workerFk"
}, },
"client": { "client": {
"type": "hasOne", "type": "belongsTo",
"model": "Client", "model": "Client",
"foreignKey": "id" "foreignKey": "clientFk"
} }
}, },
"scope": { "scope": {

View File

@ -1,6 +1,11 @@
{ {
"name": "ClientSample", "name": "ClientSample",
"base": "VnModel", "base": "Loggable",
"log": {
"model": "ClientLog",
"relation": "client",
"changedModelValue": "type"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "clientSample" "table": "clientSample"

View File

@ -1,6 +1,11 @@
{ {
"name": "Greuge", "name": "Greuge",
"base": "VnModel", "base": "Loggable",
"log": {
"model": "ClientLog",
"relation": "client",
"changedModelValue": "description"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "greuge" "table": "greuge"

View File

@ -1,6 +1,10 @@
{ {
"name": "Recovery", "name": "Recovery",
"base": "VnModel", "base": "Loggable",
"log": {
"model": "ClientLog",
"relation": "client"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "recovery" "table": "recovery"

View File

@ -1,7 +1,12 @@
{ {
"name": "Address", "name": "Address",
"description": "Client addresses", "description": "Client addresses",
"base": "VnModel", "base": "Loggable",
"log": {
"model": "ClientLog",
"relation": "client",
"changedModelValue": "nickname"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "address" "table": "address"

View File

@ -24,7 +24,7 @@
"required": true "required": true
}, },
"changedModel": { "changedModel": {
"type": "Object" "type": "String"
}, },
"oldInstance": { "oldInstance": {
"type": "Object" "type": "Object"
@ -39,7 +39,7 @@
"type": "Number" "type": "Number"
}, },
"changedModelValue": { "changedModelValue": {
"type": "Number" "type": "String"
} }
}, },
"relations": { "relations": {

View File

@ -6,7 +6,7 @@ module.exports = function(Self) {
Self.super_.setup.call(this); Self.super_.setup.call(this);
}; };
/* Self.observe('after save', async function(ctx) { Self.observe('after save', async function(ctx) {
const loopBackContext = LoopBackContext.getCurrentContext(); const loopBackContext = LoopBackContext.getCurrentContext();
await logInModel(ctx, loopBackContext); await logInModel(ctx, loopBackContext);
}); });
@ -15,10 +15,15 @@ module.exports = function(Self) {
let oldInstance; let oldInstance;
let oldInstanceFk; let oldInstanceFk;
let newInstance; let newInstance;
if (ctx.data) { if (ctx.data) {
oldInstanceFk = pick(ctx.currentInstance, Object.keys(ctx.data)); oldInstanceFk = pick(ctx.currentInstance, Object.keys(ctx.data));
newInstance = await fkToValue(ctx.data, ctx); newInstance = await fkToValue(ctx.data, ctx);
oldInstance = await fkToValue(oldInstanceFk, 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) { if (ctx.isNewInstance) {
newInstance = await fkToValue(ctx.instance.__data, ctx); newInstance = await fkToValue(ctx.instance.__data, ctx);
@ -28,53 +33,79 @@ module.exports = function(Self) {
}); });
Self.observe('before delete', async function(ctx) { Self.observe('before delete', async function(ctx) {
let oldInstance;
// console.log(ctx.where);
if (ctx.where) { if (ctx.where) {
let affectedModel = ctx.Model.definition.name; 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}); let deletedInstances = await Self.modelBuilder.models[affectedModel].find({where: ctx.where});
// console.log(deletedInstances); let relation = definition.settings.log.relation;
let arrangedDeletedInstances = []; if (relation) {
deletedInstances.forEach(async element => { let primaryKey = ctx.Model.relations[relation].keyFrom;
console.log(element);
arrangedDeletedInstances.push(await fkToValue(element, ctx));
});
console.log(arrangedDeletedInstances); let arrangedDeletedInstances = [];
// let deletedIntancesData = await fkToValue(deletedInstances, ctx); for (let i = 0; i < deletedInstances.length; i++) {
// console.log(deletedIntancesData); if (primaryKey)
// oldInstanceFk = pick(ctx.currentInstance, Object.keys(ctx.data)); deletedInstances[i].originFk = deletedInstances[i][primaryKey];
// oldInstance = await fkToValue(oldInstanceFk, ctx); 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) { Self.observe('after delete', async function(ctx) {
const loopBackContext = LoopBackContext.getCurrentContext(); 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) { async function fkToValue(instance, ctx) {
let cleanInstance = JSON.parse(JSON.stringify(instance));
let result = {}; let result = {};
for (let key in instance) { for (let key in cleanInstance) {
if (key == 'id') continue; let val = cleanInstance[key];
let val = instance[key];
console.log(val);
if (val === undefined || val === null) continue; if (val === undefined || val === null) continue;
for (let key1 in ctx.Model.relations) { for (let key1 in ctx.Model.relations) {
let val1 = ctx.Model.relations[key1]; let val1 = ctx.Model.relations[key1];
if (val1.keyFrom == key) { if (val1.keyFrom == key && key != 'id') {
let recordSet = await val1.modelTo.findById(val); let recordSet = await val1.modelTo.findById(val);
val = recordSet.name; // FIXME preparar todos los modelos con campo name val = recordSet.name; // FIXME preparar todos los modelos con campo name
console.log(val);
break; break;
} }
} }
result[key] = val; result[key] = val;
} }
// console.log(result);
return result; return result;
} }
@ -111,8 +142,6 @@ module.exports = function(Self) {
changedModelId = ctx.currentInstance.id; 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 // Sets the changedModelValue to save and the instances changed in case its an updateAll
let changedModelValue = definition.settings.log.changedModelValue; let changedModelValue = definition.settings.log.changedModelValue;
if (changedModelValue && (!ctx.instance || !ctx.instance[changedModelValue])) { if (changedModelValue && (!ctx.instance || !ctx.instance[changedModelValue])) {
@ -126,7 +155,6 @@ module.exports = function(Self) {
} else if (ctx.hookState.oldInstance) { } else if (ctx.hookState.oldInstance) {
where = ctx.instance[changedModelValue]; where = ctx.instance[changedModelValue];
} }
// console.log(where);
// Set oldInstance, newInstance, userFk and action // Set oldInstance, newInstance, userFk and action
let oldInstance = {}; let oldInstance = {};
@ -156,7 +184,7 @@ module.exports = function(Self) {
newInstance: newInstance newInstance: newInstance
}; };
let logsToSave = setLogsToSave(where, changedModelId, logRecord); let logsToSave = setLogsToSave(where, changedModelId, logRecord, ctx);
let logModel = definition.settings.log.model; let logModel = definition.settings.log.model;
@ -165,17 +193,18 @@ module.exports = function(Self) {
transaction = ctx.options.transaction; transaction = ctx.options.transaction;
} }
// console.log(logsToSave);
await Self.modelBuilder.models[logModel].create(logsToSave, transaction); await Self.modelBuilder.models[logModel].create(logsToSave, transaction);
} }
// this function retuns all the instances changed in case this is an updateAll // 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 = []; let promises = [];
if (changedInstances && typeof changedInstances == "object") { if (changedInstances && typeof changedInstances == "object") {
for (let i = 0; i < changedInstances.length; i++) { for (let i = 0; i < changedInstances.length; i++) {
logRecord.changedModelId = changedInstancesIds[i]; logRecord.changedModelId = changedInstancesIds[i];
logRecord.changedModelValue = changedInstances[i]; logRecord.changedModelValue = changedInstances[i];
if (ctx.oldInstances)
logRecord.oldInstance = ctx.oldInstances[i];
promises.push(JSON.parse(JSON.stringify(logRecord))); promises.push(JSON.parse(JSON.stringify(logRecord)));
} }
} else { } else {