salix/loopback/common/methods/log/pitInstance.js

57 lines
1.5 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('pitInstance', {
description: 'Gets the status of instance at specific point in time',
accepts: [
{
arg: 'id',
type: 'integer',
description: 'The log id',
required: true
}
],
returns: {
type: [Self],
root: true
},
http: {
path: `/:id/pitInstance`,
verb: 'GET'
}
});
Self.pitInstance = async function(id) {
const log = await Self.findById(id, {
fields: ['changedModel', 'changedModelId']
});
const where = {
changedModel: log.changedModel,
changedModelId: log.changedModelId
};
const createdWhere = {action: 'insert'};
const createdLog = await Self.findOne({
fields: ['id'],
where: Object.assign(createdWhere, where)
});
const logsWhere = {
id: {between: [
Math.min(id, createdLog.id),
Math.max(id, createdLog.id)
]}
};
const logs = await Self.find({
fields: ['newInstance'],
where: Object.assign(logsWhere, where),
order: 'creationDate'
});
const instance = {};
for (const log of logs)
Object.assign(instance, log.newInstance);
return instance;
};
};