#1116 loggable now works with transaction and log enabled on ticket module
gitea/salix/dev This commit has test failures
Details
gitea/salix/dev This commit has test failures
Details
This commit is contained in:
parent
8c401b6921
commit
79cf4afb3e
|
@ -22,12 +22,12 @@ module.exports = function(Self) {
|
|||
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});
|
||||
ctx.oldInstances = await ctx.Model.app.models[ctx.Model.definition.name].find({where: ctx.where, fields: fields});
|
||||
}
|
||||
}
|
||||
if (ctx.isNewInstance) {
|
||||
if (ctx.isNewInstance)
|
||||
newInstance = await fkToValue(ctx.instance.__data, ctx);
|
||||
}
|
||||
|
||||
ctx.hookState.oldInstance = oldInstance;
|
||||
ctx.hookState.newInstance = newInstance;
|
||||
});
|
||||
|
@ -36,7 +36,7 @@ module.exports = function(Self) {
|
|||
if (ctx.where) {
|
||||
let affectedModel = ctx.Model.definition.name;
|
||||
let definition = ctx.Model.definition;
|
||||
let deletedInstances = await Self.modelBuilder.models[affectedModel].find({where: ctx.where});
|
||||
let deletedInstances = await ctx.Model.app.models[affectedModel].find({where: ctx.where});
|
||||
let relation = definition.settings.log.relation;
|
||||
|
||||
if (relation) {
|
||||
|
@ -81,12 +81,11 @@ module.exports = function(Self) {
|
|||
};
|
||||
|
||||
let transaction = {};
|
||||
if (ctx.options && ctx.options.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);
|
||||
await ctx.Model.app.models[logModel].create(logRecord, transaction);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -147,25 +146,23 @@ module.exports = function(Self) {
|
|||
if (changedModelValue && (!ctx.instance || !ctx.instance[changedModelValue])) {
|
||||
var where = [];
|
||||
changedModelId = [];
|
||||
let changedInstances = await Self.modelBuilder.models[definition.name].find({where: ctx.where, fields: ['id', changedModelValue]});
|
||||
let changedInstances = await ctx.Model.app.models[definition.name].find({where: ctx.where, fields: ['id', changedModelValue]});
|
||||
changedInstances.forEach(element => {
|
||||
where.push(element[changedModelValue]);
|
||||
changedModelId.push(element.id);
|
||||
});
|
||||
} else if (ctx.hookState.oldInstance) {
|
||||
} else if (ctx.hookState.oldInstance)
|
||||
where = ctx.instance[changedModelValue];
|
||||
}
|
||||
|
||||
|
||||
// Set oldInstance, newInstance, userFk and action
|
||||
let oldInstance = {};
|
||||
if (ctx.hookState.oldInstance) {
|
||||
if (ctx.hookState.oldInstance)
|
||||
Object.assign(oldInstance, ctx.hookState.oldInstance);
|
||||
}
|
||||
|
||||
let newInstance = {};
|
||||
if (ctx.hookState.newInstance) {
|
||||
if (ctx.hookState.newInstance)
|
||||
Object.assign(newInstance, ctx.hookState.newInstance);
|
||||
}
|
||||
|
||||
let userFk;
|
||||
if (loopBackContext)
|
||||
|
@ -189,17 +186,16 @@ module.exports = function(Self) {
|
|||
let logModel = definition.settings.log.model;
|
||||
|
||||
let transaction = {};
|
||||
if (ctx.options && ctx.options.transaction) {
|
||||
if (ctx.options && ctx.options.transaction)
|
||||
transaction = ctx.options.transaction;
|
||||
}
|
||||
|
||||
await Self.modelBuilder.models[logModel].create(logsToSave, transaction);
|
||||
await ctx.Model.app.models[logModel].create(logsToSave, transaction);
|
||||
}
|
||||
|
||||
// this function retuns all the instances changed in case this is an updateAll
|
||||
function setLogsToSave(changedInstances, changedInstancesIds, logRecord, ctx) {
|
||||
let promises = [];
|
||||
if (changedInstances && typeof changedInstances == "object") {
|
||||
if (changedInstances && typeof changedInstances == 'object') {
|
||||
for (let i = 0; i < changedInstances.length; i++) {
|
||||
logRecord.changedModelId = changedInstancesIds[i];
|
||||
logRecord.changedModelValue = changedInstances[i];
|
||||
|
@ -207,9 +203,9 @@ module.exports = function(Self) {
|
|||
logRecord.oldInstance = ctx.oldInstances[i];
|
||||
promises.push(JSON.parse(JSON.stringify(logRecord)));
|
||||
}
|
||||
} else {
|
||||
} else
|
||||
return logRecord;
|
||||
}
|
||||
|
||||
return promises;
|
||||
}
|
||||
|
||||
|
@ -217,11 +213,11 @@ module.exports = function(Self) {
|
|||
let oldInstance = ctx.hookState.oldInstance;
|
||||
let newInstance = ctx.hookState.newInstance;
|
||||
|
||||
if (oldInstance && newInstance) {
|
||||
if (oldInstance && newInstance)
|
||||
return 'update';
|
||||
} else if (!oldInstance && newInstance) {
|
||||
else if (!oldInstance && newInstance)
|
||||
return 'insert';
|
||||
}
|
||||
|
||||
return 'delete';
|
||||
}
|
||||
};
|
||||
|
|
|
@ -3,21 +3,21 @@ const app = require('vn-loopback/server/server');
|
|||
describe('ticket deleted()', () => {
|
||||
let ticket;
|
||||
|
||||
beforeAll(async () => {
|
||||
beforeAll(async() => {
|
||||
let originalTicket = await app.models.Ticket.findOne({where: {id: 16}});
|
||||
originalTicket.id = null;
|
||||
ticket = await app.models.Ticket.create(originalTicket);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
afterAll(async() => {
|
||||
await app.models.Ticket.destroyById(ticket.id);
|
||||
});
|
||||
|
||||
it('should make sure the ticket is not deleted yet', async () => {
|
||||
it('should make sure the ticket is not deleted yet', async() => {
|
||||
expect(ticket.isDeleted).toEqual(false);
|
||||
});
|
||||
|
||||
it('should set a ticket to deleted and log the change on TicketState table', async () => {
|
||||
it('should set a ticket to deleted and log the change on TicketState table', async() => {
|
||||
let ctx = {req: {accessToken: {userId: 9}}};
|
||||
let params = {id: ticket.id};
|
||||
await app.models.Ticket.deleted(ctx, params);
|
||||
|
@ -29,7 +29,7 @@ describe('ticket deleted()', () => {
|
|||
expect(changedState.stateFk).toEqual(17);
|
||||
});
|
||||
|
||||
it('should throw an error if the given ticket has a claim', async () => {
|
||||
it('should throw an error if the given ticket has a claim', async() => {
|
||||
let ctx = {req: {accessToken: {userId: 9}}};
|
||||
let params = {id: 16};
|
||||
let error;
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
{
|
||||
"name": "Ticket",
|
||||
"base": "VnModel",
|
||||
"base": "Loggable",
|
||||
"log": {
|
||||
"model":"TicketLog"
|
||||
},
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "ticket"
|
||||
|
|
Loading…
Reference in New Issue