Tarea #707 Loggable disabled due broken tests
This commit is contained in:
parent
239e695ed5
commit
76a2eda6e0
|
@ -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);
|
||||||
});
|
});
|
||||||
|
@ -27,49 +27,58 @@ module.exports = function(Self) {
|
||||||
ctx.hookState.newInstance = newInstance;
|
ctx.hookState.newInstance = newInstance;
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Self.observe('before delete', async function(ctx, next) {
|
Self.observe('before delete', async function(ctx) {
|
||||||
let oldInstance;
|
let oldInstance;
|
||||||
if (ctx.instance) {
|
// console.log(ctx.where);
|
||||||
oldInstance = await fkToValue(ctx.data, ctx);
|
if (ctx.where) {
|
||||||
}
|
let affectedModel = ctx.Model.definition.name;
|
||||||
await logInModel(ctx);
|
// console.log(affectedModel);
|
||||||
|
let deletedInstances = await Self.modelBuilder.models[affectedModel].find({where: ctx.where});
|
||||||
|
// console.log(deletedInstances);
|
||||||
|
|
||||||
next();
|
let arrangedDeletedInstances = [];
|
||||||
|
deletedInstances.forEach(async element => {
|
||||||
|
console.log(element);
|
||||||
|
arrangedDeletedInstances.push(await fkToValue(element, ctx));
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
ctx.hookState.oldInstance = oldInstance;
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.observe('after delete', async function(ctx, next) {
|
Self.observe('after delete', async function(ctx) {
|
||||||
let oldInstance;
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
||||||
if (ctx.instance) {
|
await logInModel(ctx, loopBackContext);
|
||||||
oldInstance = await fkToValue(ctx.data, ctx);
|
}); */
|
||||||
}
|
|
||||||
await logInModel(ctx);
|
|
||||||
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
async function fkToValue(instance, ctx) {
|
async function fkToValue(instance, ctx) {
|
||||||
let result = {};
|
let result = {};
|
||||||
for (let key in instance) {
|
for (let key in instance) {
|
||||||
if (key == 'id') continue;
|
if (key == 'id') continue;
|
||||||
let val = instance[key];
|
let val = instance[key];
|
||||||
if (val === undefined) continue;
|
console.log(val);
|
||||||
|
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) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function logInModel(ctx, loopBackContext) {
|
async function logInModel(ctx, loopBackContext) {
|
||||||
|
|
||||||
let definition = ctx.Model.definition;
|
let definition = ctx.Model.definition;
|
||||||
let primaryKey;
|
let primaryKey;
|
||||||
for (let property in definition.properties) {
|
for (let property in definition.properties) {
|
||||||
|
@ -78,83 +87,112 @@ module.exports = function(Self) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!primaryKey) throw new Error('Primary key not found');
|
if (!primaryKey) throw new Error('Primary key not found');
|
||||||
let originId;
|
let originId;
|
||||||
|
|
||||||
if (definition.settings.log.relation) {
|
// RELATIONS LOG
|
||||||
// RELATIONS LOG
|
let changedModelId;
|
||||||
|
|
||||||
|
if (ctx.instance && !definition.settings.log.relation) {
|
||||||
|
originId = ctx.instance.id;
|
||||||
|
changedModelId = ctx.instance.id;
|
||||||
|
} else if (definition.settings.log.relation) {
|
||||||
primaryKey = ctx.Model.relations[definition.settings.log.relation].keyFrom;
|
primaryKey = ctx.Model.relations[definition.settings.log.relation].keyFrom;
|
||||||
|
|
||||||
if(ctx.where && ctx.where[primaryKey])
|
if (ctx.where && ctx.where[primaryKey])
|
||||||
originId = ctx.where[primaryKey]
|
originId = ctx.where[primaryKey];
|
||||||
else
|
else {
|
||||||
originId = ctx.instance[primaryKey];
|
originId = ctx.instance[primaryKey];
|
||||||
} else {
|
changedModelId = ctx.instance.id;
|
||||||
if (ctx.instance) {
|
|
||||||
originId = ctx.instance.id;
|
|
||||||
} else {
|
|
||||||
originId = ctx.currentInstance.id;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
originId = ctx.currentInstance.id;
|
||||||
|
changedModelId = ctx.currentInstance.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This adds the originDescription field if it doesnt exists in the instances
|
// console.log(ctx.instance, ctx.where, ctx.currentInstance);
|
||||||
|
|
||||||
let originDescription = definition.settings.log.originDescription;
|
// Sets the changedModelValue to save and the instances changed in case its an updateAll
|
||||||
|
let changedModelValue = definition.settings.log.changedModelValue;
|
||||||
if (originDescription && (!ctx.instance || !ctx.instance[originDescription]))
|
if (changedModelValue && (!ctx.instance || !ctx.instance[changedModelValue])) {
|
||||||
await Self.modelBuilder.models[definition.name].findById()
|
var where = [];
|
||||||
|
changedModelId = [];
|
||||||
if (ctx.hookState.oldInstance && !ctx.hookState.oldInstance[originDescription]){
|
let changedInstances = await Self.modelBuilder.models[definition.name].find({where: ctx.where, fields: ['id', changedModelValue]});
|
||||||
ctx.hookState.oldInstance[originDescription] = ctx.instance[originDescription];
|
changedInstances.forEach(element => {
|
||||||
ctx.hookState.newInstance[originDescription] = ctx.instance[originDescription];
|
where.push(element[changedModelValue]);
|
||||||
|
changedModelId.push(element.id);
|
||||||
|
});
|
||||||
|
} else if (ctx.hookState.oldInstance) {
|
||||||
|
where = ctx.instance[changedModelValue];
|
||||||
}
|
}
|
||||||
|
// console.log(where);
|
||||||
|
|
||||||
// This put some order in the intances putting the originDescription in first place
|
// Set oldInstance, newInstance, userFk and action
|
||||||
|
|
||||||
let oldInstance = {};
|
let oldInstance = {};
|
||||||
if (ctx.hookState.oldInstance) {
|
if (ctx.hookState.oldInstance) {
|
||||||
oldInstance[originDescription] = ctx.hookState.oldInstance[originDescription];
|
|
||||||
delete ctx.hookState.oldInstance[originDescription];
|
|
||||||
Object.assign(oldInstance, ctx.hookState.oldInstance);
|
Object.assign(oldInstance, ctx.hookState.oldInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
let newInstance = {};
|
let newInstance = {};
|
||||||
if (ctx.hookState.newInstance) {
|
if (ctx.hookState.newInstance) {
|
||||||
newInstance[originDescription] = ctx.hookState.newInstance[originDescription];
|
Object.assign(newInstance, ctx.hookState.newInstance);
|
||||||
delete ctx.hookState.newInstance[originDescription];
|
|
||||||
Object.assign(newInstance, ctx.hookState.newInstance)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let userFk;
|
let userFk;
|
||||||
if (!loopBackContext) userFk = null;
|
if (loopBackContext)
|
||||||
else userFk = loopBackContext.active.accessToken.userId;
|
userFk = loopBackContext.active.accessToken.userId;
|
||||||
|
|
||||||
let action = setActionType(ctx);
|
let action = setActionType(ctx);
|
||||||
|
|
||||||
|
|
||||||
let logRecord = {
|
let logRecord = {
|
||||||
originFk: originId,
|
originFk: originId,
|
||||||
userFk: userFk,
|
userFk: userFk,
|
||||||
model: ctx.Model.definition.name,
|
|
||||||
action: action,
|
action: action,
|
||||||
|
changedModel: ctx.Model.definition.name,
|
||||||
|
changedModelId: changedModelId,
|
||||||
|
changedModelValue: where,
|
||||||
oldInstance: oldInstance,
|
oldInstance: oldInstance,
|
||||||
newInstance: newInstance
|
newInstance: newInstance
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let logsToSave = setLogsToSave(where, changedModelId, logRecord);
|
||||||
|
|
||||||
let logModel = definition.settings.log.model
|
let logModel = definition.settings.log.model;
|
||||||
await Self.modelBuilder.models[logModel].create(logRecord);
|
|
||||||
|
let transaction = {};
|
||||||
|
if (ctx.options && ctx.options.transaction) {
|
||||||
|
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) {
|
||||||
|
let promises = [];
|
||||||
|
if (changedInstances && typeof changedInstances == "object") {
|
||||||
|
for (let i = 0; i < changedInstances.length; i++) {
|
||||||
|
logRecord.changedModelId = changedInstancesIds[i];
|
||||||
|
logRecord.changedModelValue = changedInstances[i];
|
||||||
|
promises.push(JSON.parse(JSON.stringify(logRecord)));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return logRecord;
|
||||||
|
}
|
||||||
|
return promises;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setActionType(ctx) {
|
function setActionType(ctx) {
|
||||||
let oldInstance = ctx.hookState.oldInstance;
|
let oldInstance = ctx.hookState.oldInstance;
|
||||||
let newInstance = ctx.hookState.newInstance;
|
let newInstance = ctx.hookState.newInstance;
|
||||||
|
|
||||||
if (oldInstance && newInstance) {
|
if (oldInstance && newInstance) {
|
||||||
return 'update';
|
return 'update';
|
||||||
} else if (!oldInstance && newInstance) {
|
} else if (!oldInstance && newInstance) {
|
||||||
return 'insert';
|
return 'insert';
|
||||||
}
|
}
|
||||||
|
return 'delete';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue