#707 logs activated in all client module
This commit is contained in:
parent
fa8825d4d0
commit
c512192052
|
@ -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",
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
{
|
||||
"name": "ClientSample",
|
||||
"base": "VnModel",
|
||||
"base": "Loggable",
|
||||
"log": {
|
||||
"model": "ClientLog",
|
||||
"relation": "client",
|
||||
"changedModelValue": "type"
|
||||
},
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "clientSample"
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
{
|
||||
"name": "Greuge",
|
||||
"base": "VnModel",
|
||||
"base": "Loggable",
|
||||
"log": {
|
||||
"model": "ClientLog",
|
||||
"relation": "client",
|
||||
"changedModelValue": "description"
|
||||
},
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "greuge"
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"name": "Recovery",
|
||||
"base": "VnModel",
|
||||
"base": "Loggable",
|
||||
"log": {
|
||||
"model": "ClientLog",
|
||||
"relation": "client"
|
||||
},
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "recovery"
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
{
|
||||
"name": "Address",
|
||||
"description": "Client addresses",
|
||||
"base": "VnModel",
|
||||
"base": "Loggable",
|
||||
"log": {
|
||||
"model": "ClientLog",
|
||||
"relation": "client",
|
||||
"changedModelValue": "nickname"
|
||||
},
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "address"
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue