#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);
|
oldInstance = await fkToValue(oldInstanceFk, ctx);
|
||||||
if (ctx.where && !ctx.currentInstance) {
|
if (ctx.where && !ctx.currentInstance) {
|
||||||
let fields = Object.keys(ctx.data);
|
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);
|
newInstance = await fkToValue(ctx.instance.__data, ctx);
|
||||||
}
|
|
||||||
ctx.hookState.oldInstance = oldInstance;
|
ctx.hookState.oldInstance = oldInstance;
|
||||||
ctx.hookState.newInstance = newInstance;
|
ctx.hookState.newInstance = newInstance;
|
||||||
});
|
});
|
||||||
|
@ -36,7 +36,7 @@ module.exports = function(Self) {
|
||||||
if (ctx.where) {
|
if (ctx.where) {
|
||||||
let affectedModel = ctx.Model.definition.name;
|
let affectedModel = ctx.Model.definition.name;
|
||||||
let definition = ctx.Model.definition;
|
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;
|
let relation = definition.settings.log.relation;
|
||||||
|
|
||||||
if (relation) {
|
if (relation) {
|
||||||
|
@ -81,12 +81,11 @@ module.exports = function(Self) {
|
||||||
};
|
};
|
||||||
|
|
||||||
let transaction = {};
|
let transaction = {};
|
||||||
if (ctx.options && ctx.options.transaction) {
|
if (ctx.options && ctx.options.transaction)
|
||||||
transaction = ctx.options.transaction;
|
transaction = ctx.options.transaction;
|
||||||
}
|
|
||||||
|
|
||||||
let logModel = definition.settings.log.model;
|
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])) {
|
if (changedModelValue && (!ctx.instance || !ctx.instance[changedModelValue])) {
|
||||||
var where = [];
|
var where = [];
|
||||||
changedModelId = [];
|
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 => {
|
changedInstances.forEach(element => {
|
||||||
where.push(element[changedModelValue]);
|
where.push(element[changedModelValue]);
|
||||||
changedModelId.push(element.id);
|
changedModelId.push(element.id);
|
||||||
});
|
});
|
||||||
} else if (ctx.hookState.oldInstance) {
|
} else if (ctx.hookState.oldInstance)
|
||||||
where = ctx.instance[changedModelValue];
|
where = ctx.instance[changedModelValue];
|
||||||
}
|
|
||||||
|
|
||||||
// Set oldInstance, newInstance, userFk and action
|
// Set oldInstance, newInstance, userFk and action
|
||||||
let oldInstance = {};
|
let oldInstance = {};
|
||||||
if (ctx.hookState.oldInstance) {
|
if (ctx.hookState.oldInstance)
|
||||||
Object.assign(oldInstance, ctx.hookState.oldInstance);
|
Object.assign(oldInstance, ctx.hookState.oldInstance);
|
||||||
}
|
|
||||||
|
|
||||||
let newInstance = {};
|
let newInstance = {};
|
||||||
if (ctx.hookState.newInstance) {
|
if (ctx.hookState.newInstance)
|
||||||
Object.assign(newInstance, ctx.hookState.newInstance);
|
Object.assign(newInstance, ctx.hookState.newInstance);
|
||||||
}
|
|
||||||
|
|
||||||
let userFk;
|
let userFk;
|
||||||
if (loopBackContext)
|
if (loopBackContext)
|
||||||
|
@ -189,17 +186,16 @@ module.exports = function(Self) {
|
||||||
let logModel = definition.settings.log.model;
|
let logModel = definition.settings.log.model;
|
||||||
|
|
||||||
let transaction = {};
|
let transaction = {};
|
||||||
if (ctx.options && ctx.options.transaction) {
|
if (ctx.options && ctx.options.transaction)
|
||||||
transaction = 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
|
// this function retuns all the instances changed in case this is an updateAll
|
||||||
function setLogsToSave(changedInstances, changedInstancesIds, logRecord, ctx) {
|
function setLogsToSave(changedInstances, changedInstancesIds, logRecord, ctx) {
|
||||||
let promises = [];
|
let promises = [];
|
||||||
if (changedInstances && typeof changedInstances == "object") {
|
if (changedInstances && typeof changedInstances == 'object') {
|
||||||
for (let i = 0; i < changedInstances.length; i++) {
|
for (let i = 0; i < changedInstances.length; i++) {
|
||||||
logRecord.changedModelId = changedInstancesIds[i];
|
logRecord.changedModelId = changedInstancesIds[i];
|
||||||
logRecord.changedModelValue = changedInstances[i];
|
logRecord.changedModelValue = changedInstances[i];
|
||||||
|
@ -207,9 +203,9 @@ module.exports = function(Self) {
|
||||||
logRecord.oldInstance = ctx.oldInstances[i];
|
logRecord.oldInstance = ctx.oldInstances[i];
|
||||||
promises.push(JSON.parse(JSON.stringify(logRecord)));
|
promises.push(JSON.parse(JSON.stringify(logRecord)));
|
||||||
}
|
}
|
||||||
} else {
|
} else
|
||||||
return logRecord;
|
return logRecord;
|
||||||
}
|
|
||||||
return promises;
|
return promises;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,11 +213,11 @@ module.exports = function(Self) {
|
||||||
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';
|
return 'delete';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,21 +3,21 @@ const app = require('vn-loopback/server/server');
|
||||||
describe('ticket deleted()', () => {
|
describe('ticket deleted()', () => {
|
||||||
let ticket;
|
let ticket;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async() => {
|
||||||
let originalTicket = await app.models.Ticket.findOne({where: {id: 16}});
|
let originalTicket = await app.models.Ticket.findOne({where: {id: 16}});
|
||||||
originalTicket.id = null;
|
originalTicket.id = null;
|
||||||
ticket = await app.models.Ticket.create(originalTicket);
|
ticket = await app.models.Ticket.create(originalTicket);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async() => {
|
||||||
await app.models.Ticket.destroyById(ticket.id);
|
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);
|
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 ctx = {req: {accessToken: {userId: 9}}};
|
||||||
let params = {id: ticket.id};
|
let params = {id: ticket.id};
|
||||||
await app.models.Ticket.deleted(ctx, params);
|
await app.models.Ticket.deleted(ctx, params);
|
||||||
|
@ -29,7 +29,7 @@ describe('ticket deleted()', () => {
|
||||||
expect(changedState.stateFk).toEqual(17);
|
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 ctx = {req: {accessToken: {userId: 9}}};
|
||||||
let params = {id: 16};
|
let params = {id: 16};
|
||||||
let error;
|
let error;
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
{
|
{
|
||||||
"name": "Ticket",
|
"name": "Ticket",
|
||||||
"base": "VnModel",
|
"base": "Loggable",
|
||||||
|
"log": {
|
||||||
|
"model":"TicketLog"
|
||||||
|
},
|
||||||
"options": {
|
"options": {
|
||||||
"mysql": {
|
"mysql": {
|
||||||
"table": "ticket"
|
"table": "ticket"
|
||||||
|
|
Loading…
Reference in New Issue