2021-03-22 08:08:27 +00:00
|
|
|
const pick = require('object.pick');
|
2018-10-03 07:37:34 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
|
|
|
|
module.exports = function(Self) {
|
|
|
|
Self.setup = function() {
|
|
|
|
Self.super_.setup.call(this);
|
|
|
|
};
|
|
|
|
|
2018-10-22 12:35:21 +00:00
|
|
|
Self.observe('after save', async function(ctx) {
|
2018-10-03 07:37:34 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
await logInModel(ctx, loopBackContext);
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.observe('before save', async function(ctx) {
|
2023-01-09 12:49:35 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
const models = ctx.Model.app.models;
|
2020-05-26 13:49:20 +00:00
|
|
|
const definition = ctx.Model.definition;
|
2023-01-09 12:49:35 +00:00
|
|
|
const Model = models[definition.name];
|
|
|
|
const settings = definition.settings;
|
|
|
|
let opts = ctx.options;
|
|
|
|
if (!opts) opts = ctx.options = {};
|
2020-05-26 12:37:34 +00:00
|
|
|
|
|
|
|
// Check for transactions
|
2023-01-09 12:49:35 +00:00
|
|
|
let tx;
|
|
|
|
if (!opts.transaction) {
|
|
|
|
opts.transaction = tx = await Model.beginTransaction({});
|
|
|
|
opts.tx = true;
|
|
|
|
}
|
2019-02-27 09:56:31 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
try {
|
|
|
|
// Check if grabUser is active
|
|
|
|
if (loopBackContext && settings.log && settings.log.grabUser) {
|
|
|
|
const userId = loopBackContext.active.accessToken.userId;
|
|
|
|
const user = await models.Account.findById(userId, {fields: ['name']}, opts);
|
|
|
|
await Model.rawSql(`CALL account.myUser_loginWithName(?)`, [user.name], opts);
|
|
|
|
}
|
2018-10-22 12:35:21 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
let oldInstance;
|
|
|
|
let newInstance;
|
|
|
|
if (ctx.data) {
|
|
|
|
const changes = pick(ctx.currentInstance, Object.keys(ctx.data));
|
|
|
|
newInstance = ctx.data;
|
|
|
|
oldInstance = changes;
|
2020-05-26 12:37:34 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
if (ctx.where && !ctx.currentInstance) {
|
|
|
|
const fields = Object.keys(ctx.data);
|
2020-05-26 12:37:34 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
ctx.oldInstances = await Model.find({
|
|
|
|
where: ctx.where,
|
|
|
|
fields: fields
|
|
|
|
}, opts);
|
|
|
|
}
|
2018-10-22 12:35:21 +00:00
|
|
|
}
|
2020-05-26 12:37:34 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
// Get changes from created instance
|
|
|
|
if (ctx.isNewInstance)
|
|
|
|
newInstance = ctx.instance.__data;
|
2019-02-20 15:30:46 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
ctx.hookState.oldInstance = oldInstance;
|
|
|
|
ctx.hookState.newInstance = newInstance;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-10-03 07:37:34 +00:00
|
|
|
});
|
|
|
|
|
2018-10-18 09:11:17 +00:00
|
|
|
Self.observe('before delete', async function(ctx) {
|
2023-01-09 12:49:35 +00:00
|
|
|
const models = ctx.Model.app.models;
|
2020-05-26 12:37:34 +00:00
|
|
|
const definition = ctx.Model.definition;
|
|
|
|
const relations = ctx.Model.relations;
|
2023-01-09 12:49:35 +00:00
|
|
|
let opts = ctx.options;
|
|
|
|
if (!opts) opts = ctx.options = {};
|
2019-02-27 09:56:31 +00:00
|
|
|
|
2018-10-18 09:11:17 +00:00
|
|
|
if (ctx.where) {
|
2023-01-09 12:49:35 +00:00
|
|
|
const affectedModel = definition.name;
|
|
|
|
let deletedInstances = await models[affectedModel].find({
|
2020-05-26 12:37:34 +00:00
|
|
|
where: ctx.where
|
2023-01-09 12:49:35 +00:00
|
|
|
}, opts);
|
2020-05-26 12:37:34 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
const relation = definition.settings.log.relation;
|
2018-10-18 09:11:17 +00:00
|
|
|
|
2018-10-22 12:35:21 +00:00
|
|
|
if (relation) {
|
2023-01-09 12:49:35 +00:00
|
|
|
const primaryKey = relations[relation].keyFrom;
|
2018-10-18 09:11:17 +00:00
|
|
|
|
2021-03-22 08:08:27 +00:00
|
|
|
let arrangedDeletedInstances = [];
|
|
|
|
for (let i = 0; i < deletedInstances.length; i++) {
|
2018-10-22 12:35:21 +00:00
|
|
|
if (primaryKey)
|
2021-03-22 08:08:27 +00:00
|
|
|
deletedInstances[i].originFk = deletedInstances[i][primaryKey];
|
|
|
|
let arrangedInstance = await fkToValue(deletedInstances[i], ctx);
|
|
|
|
arrangedDeletedInstances[i] = arrangedInstance;
|
2018-10-22 12:35:21 +00:00
|
|
|
}
|
2021-03-22 08:08:27 +00:00
|
|
|
ctx.hookState.oldInstance = arrangedDeletedInstances;
|
2018-10-22 12:35:21 +00:00
|
|
|
}
|
2018-10-03 07:37:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-18 09:11:17 +00:00
|
|
|
Self.observe('after delete', async function(ctx) {
|
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
2023-01-09 12:49:35 +00:00
|
|
|
if (ctx.hookState.oldInstance) logDeletedInstances(ctx, loopBackContext);
|
2018-10-22 12:35:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
async function logDeletedInstances(ctx, loopBackContext) {
|
2023-01-09 12:49:35 +00:00
|
|
|
const models = ctx.Model.app.models;
|
2020-05-26 13:49:20 +00:00
|
|
|
const definition = ctx.Model.definition;
|
2023-01-09 12:49:35 +00:00
|
|
|
const settings = definition.settings;
|
|
|
|
let opts = ctx.options;
|
|
|
|
if (!opts) opts = ctx.options = {};
|
2019-02-27 09:56:31 +00:00
|
|
|
|
2018-10-22 12:35:21 +00:00
|
|
|
ctx.hookState.oldInstance.forEach(async instance => {
|
|
|
|
let userFk;
|
|
|
|
if (loopBackContext)
|
|
|
|
userFk = loopBackContext.active.accessToken.userId;
|
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
const changedModelValue = settings.log.changedModelValue;
|
|
|
|
const logRecord = {
|
2018-10-22 12:35:21 +00:00
|
|
|
originFk: instance.originFk,
|
|
|
|
userFk: userFk,
|
|
|
|
action: 'delete',
|
2020-05-26 13:49:20 +00:00
|
|
|
changedModel: definition.name,
|
2018-10-22 12:35:21 +00:00
|
|
|
changedModelId: instance.id,
|
|
|
|
changedModelValue: instance[changedModelValue],
|
|
|
|
oldInstance: instance,
|
|
|
|
newInstance: {}
|
|
|
|
};
|
|
|
|
|
2019-04-04 10:38:54 +00:00
|
|
|
delete instance.originFk;
|
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
const logModel = settings.log.model;
|
|
|
|
await models[logModel].create(logRecord, opts);
|
2018-10-22 12:35:21 +00:00
|
|
|
});
|
|
|
|
}
|
2018-10-03 07:37:34 +00:00
|
|
|
|
2021-03-22 08:08:27 +00:00
|
|
|
// Get log values from a foreign key
|
|
|
|
async function fkToValue(instance, ctx) {
|
2023-01-09 12:49:35 +00:00
|
|
|
const models = ctx.Model.app.models;
|
2021-03-22 08:08:27 +00:00
|
|
|
const relations = ctx.Model.relations;
|
2023-01-09 12:49:35 +00:00
|
|
|
let opts = ctx.options;
|
|
|
|
if (!opts) opts = ctx.options = {};
|
2021-03-22 08:08:27 +00:00
|
|
|
|
|
|
|
const instanceCopy = JSON.parse(JSON.stringify(instance));
|
|
|
|
const result = {};
|
|
|
|
for (const key in instanceCopy) {
|
|
|
|
let value = instanceCopy[key];
|
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
if (value instanceof Object || value === undefined)
|
2021-03-22 08:08:27 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (value) {
|
2023-01-09 12:49:35 +00:00
|
|
|
for (const relationName in relations) {
|
2021-03-22 08:08:27 +00:00
|
|
|
const relation = relations[relationName];
|
|
|
|
if (relation.keyFrom == key && key != 'id') {
|
|
|
|
const model = relation.modelTo;
|
|
|
|
const modelName = relation.modelTo.modelName;
|
|
|
|
const properties = model && model.definition.properties;
|
|
|
|
const settings = model && model.definition.settings;
|
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
const recordSet = await models[modelName].findById(value, null, opts);
|
2021-03-22 08:08:27 +00:00
|
|
|
|
|
|
|
const hasShowField = settings.log && settings.log.showField;
|
|
|
|
let showField = hasShowField && recordSet
|
|
|
|
&& recordSet[settings.log.showField];
|
|
|
|
|
|
|
|
if (!showField) {
|
|
|
|
const showFieldNames = [
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'code',
|
|
|
|
'nickname'
|
|
|
|
];
|
|
|
|
for (field of showFieldNames) {
|
|
|
|
const propField = properties && properties[field];
|
|
|
|
const recordField = recordSet && recordSet[field];
|
|
|
|
|
|
|
|
if (propField && recordField) {
|
|
|
|
showField = field;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showField && recordSet && recordSet[showField]) {
|
|
|
|
value = recordSet[showField];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = recordSet && recordSet.id || value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result[key] = value;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-03 07:37:34 +00:00
|
|
|
async function logInModel(ctx, loopBackContext) {
|
2023-01-09 12:49:35 +00:00
|
|
|
const models = ctx.Model.app.models;
|
2020-05-26 12:37:34 +00:00
|
|
|
const definition = ctx.Model.definition;
|
2023-01-09 12:49:35 +00:00
|
|
|
const Model = models[definition.name];
|
|
|
|
const settings = ctx.Model.definition.settings;
|
2020-05-26 12:37:34 +00:00
|
|
|
const relations = ctx.Model.relations;
|
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
let opts = ctx.options;
|
|
|
|
if (!opts) opts = ctx.options = {};
|
2019-02-27 09:56:31 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
// Check for transactions
|
|
|
|
let tx;
|
|
|
|
if (opts.tx === true) tx = opts.transaction;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let primaryKey;
|
|
|
|
for (let property in definition.properties) {
|
|
|
|
if (definition.properties[property].id) {
|
|
|
|
primaryKey = property;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-03 07:37:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
if (!primaryKey) throw new Error('Primary key not found');
|
|
|
|
let originId;
|
2018-10-18 09:11:17 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
// RELATIONS LOG
|
|
|
|
let changedModelId;
|
2018-10-03 07:37:34 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
if (ctx.instance && !settings.log.relation) {
|
|
|
|
originId = ctx.instance.id;
|
2018-10-18 09:11:17 +00:00
|
|
|
changedModelId = ctx.instance.id;
|
2023-01-09 12:49:35 +00:00
|
|
|
} else if (settings.log.relation) {
|
|
|
|
primaryKey = relations[settings.log.relation].keyFrom;
|
|
|
|
|
|
|
|
if (ctx.where && ctx.where[primaryKey])
|
|
|
|
originId = ctx.where[primaryKey];
|
|
|
|
else if (ctx.instance) {
|
|
|
|
originId = ctx.instance[primaryKey];
|
|
|
|
changedModelId = ctx.instance.id;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
originId = ctx.currentInstance.id;
|
|
|
|
changedModelId = ctx.currentInstance.id;
|
2018-10-03 07:37:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
// Sets the changedModelValue to save and the instances changed in case its an updateAll
|
|
|
|
let showField = settings.log.showField;
|
|
|
|
let where;
|
|
|
|
if (showField && (!ctx.instance || !ctx.instance[showField]) && ctx.where) {
|
|
|
|
changedModelId = [];
|
|
|
|
where = [];
|
|
|
|
let changedInstances = await models[definition.name].find({
|
|
|
|
where: ctx.where,
|
|
|
|
fields: ['id', showField, primaryKey]
|
|
|
|
}, opts);
|
|
|
|
|
|
|
|
changedInstances.forEach(element => {
|
|
|
|
where.push(element[showField]);
|
|
|
|
changedModelId.push(element.id);
|
|
|
|
originId = element[primaryKey];
|
|
|
|
});
|
|
|
|
} else if (ctx.hookState.oldInstance)
|
|
|
|
where = ctx.instance[showField];
|
|
|
|
|
|
|
|
// Set oldInstance, newInstance, userFk and action
|
|
|
|
let oldInstance = {};
|
|
|
|
if (ctx.hookState.oldInstance)
|
|
|
|
Object.assign(oldInstance, ctx.hookState.oldInstance);
|
|
|
|
|
|
|
|
let newInstance = {};
|
|
|
|
if (ctx.hookState.newInstance)
|
|
|
|
Object.assign(newInstance, ctx.hookState.newInstance);
|
|
|
|
|
|
|
|
let userFk;
|
|
|
|
if (loopBackContext)
|
|
|
|
userFk = loopBackContext.active.accessToken.userId;
|
|
|
|
|
|
|
|
const action = setActionType(ctx);
|
|
|
|
|
|
|
|
removeUnloggable(definition, oldInstance);
|
|
|
|
removeUnloggable(definition, newInstance);
|
|
|
|
|
|
|
|
oldInstance = await fkToValue(oldInstance, ctx);
|
|
|
|
newInstance = await fkToValue(newInstance, ctx);
|
|
|
|
|
|
|
|
// Prevent log with no new changes
|
|
|
|
const hasNewChanges = Object.keys(newInstance).length;
|
|
|
|
if (!hasNewChanges) return;
|
|
|
|
|
|
|
|
let logRecord = {
|
|
|
|
originFk: originId,
|
|
|
|
userFk: userFk,
|
|
|
|
action: action,
|
|
|
|
changedModel: definition.name,
|
|
|
|
changedModelId: changedModelId, // Model property with an different data type will throw a NaN error
|
|
|
|
changedModelValue: where,
|
|
|
|
oldInstance: oldInstance,
|
|
|
|
newInstance: newInstance
|
|
|
|
};
|
|
|
|
|
|
|
|
const logsToSave = setLogsToSave(where, changedModelId, logRecord, ctx);
|
|
|
|
const logModel = settings.log.model;
|
|
|
|
await models[logModel].create(logsToSave, opts);
|
|
|
|
|
|
|
|
// Check if grabUser is active
|
|
|
|
if (settings.log && settings.log.grabUser) await Model.rawSql(`CALL account.myUser_logout()`, null, opts);
|
|
|
|
if (tx) {
|
|
|
|
await tx.commit();
|
|
|
|
delete opts.transaction;
|
|
|
|
delete opts.tx;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-10-18 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 07:49:02 +00:00
|
|
|
/**
|
|
|
|
* Removes unwanted properties
|
|
|
|
* @param {*} definition Model definition
|
|
|
|
* @param {*} properties Modified object properties
|
|
|
|
*/
|
2020-05-26 12:37:34 +00:00
|
|
|
function removeUnloggable(definition, properties) {
|
2022-02-01 10:24:04 +00:00
|
|
|
const objectCopy = Object.assign({}, properties);
|
|
|
|
const propList = Object.keys(objectCopy);
|
2019-09-12 07:49:02 +00:00
|
|
|
const propDefs = new Map();
|
|
|
|
|
|
|
|
for (let property in definition.properties) {
|
|
|
|
const propertyDef = definition.properties[property];
|
|
|
|
propDefs.set(property, propertyDef);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let property of propList) {
|
|
|
|
const propertyDef = propDefs.get(property);
|
2022-02-01 10:24:04 +00:00
|
|
|
const firstChar = property.substring(0, 1);
|
|
|
|
const isPrivate = firstChar == '$';
|
2019-09-12 07:49:02 +00:00
|
|
|
|
2022-02-01 10:24:04 +00:00
|
|
|
if (isPrivate || !propertyDef)
|
|
|
|
delete properties[property];
|
|
|
|
|
|
|
|
if (!propertyDef) continue;
|
2019-09-12 07:49:02 +00:00
|
|
|
|
2023-01-09 12:49:35 +00:00
|
|
|
if (propertyDef.log === false)
|
2019-09-12 07:49:02 +00:00
|
|
|
delete properties[property];
|
|
|
|
else if (propertyDef.logValue === false)
|
|
|
|
properties[property] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 09:11:17 +00:00
|
|
|
// this function retuns all the instances changed in case this is an updateAll
|
2018-10-22 12:35:21 +00:00
|
|
|
function setLogsToSave(changedInstances, changedInstancesIds, logRecord, ctx) {
|
2018-10-18 09:11:17 +00:00
|
|
|
let promises = [];
|
2019-02-20 15:30:46 +00:00
|
|
|
if (changedInstances && typeof changedInstances == 'object') {
|
2018-10-18 09:11:17 +00:00
|
|
|
for (let i = 0; i < changedInstances.length; i++) {
|
|
|
|
logRecord.changedModelId = changedInstancesIds[i];
|
|
|
|
logRecord.changedModelValue = changedInstances[i];
|
2018-10-22 12:35:21 +00:00
|
|
|
if (ctx.oldInstances)
|
|
|
|
logRecord.oldInstance = ctx.oldInstances[i];
|
2018-10-18 09:11:17 +00:00
|
|
|
promises.push(JSON.parse(JSON.stringify(logRecord)));
|
|
|
|
}
|
2019-02-20 15:30:46 +00:00
|
|
|
} else
|
2018-10-18 09:11:17 +00:00
|
|
|
return logRecord;
|
2019-02-20 15:30:46 +00:00
|
|
|
|
2018-10-18 09:11:17 +00:00
|
|
|
return promises;
|
2018-10-03 07:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setActionType(ctx) {
|
2021-03-22 08:08:27 +00:00
|
|
|
let oldInstance = ctx.hookState.oldInstance;
|
|
|
|
let newInstance = ctx.hookState.newInstance;
|
2018-10-03 07:37:34 +00:00
|
|
|
|
2019-02-20 15:30:46 +00:00
|
|
|
if (oldInstance && newInstance)
|
2018-10-03 07:37:34 +00:00
|
|
|
return 'update';
|
2019-02-20 15:30:46 +00:00
|
|
|
else if (!oldInstance && newInstance)
|
2018-10-03 07:37:34 +00:00
|
|
|
return 'insert';
|
2019-02-20 15:30:46 +00:00
|
|
|
|
2018-10-18 09:11:17 +00:00
|
|
|
return 'delete';
|
2018-10-03 07:37:34 +00:00
|
|
|
}
|
|
|
|
};
|