2019-04-23 09:25:49 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2019-07-22 11:14:00 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2019-04-23 09:25:49 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.observe('before save', async ctx => {
|
2019-07-22 11:14:00 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
2019-04-24 05:43:11 +00:00
|
|
|
let changes = ctx.currentInstance || ctx.instance;
|
|
|
|
if (changes) {
|
|
|
|
let ticketId = changes.ticketFk;
|
2019-07-22 11:14:00 +00:00
|
|
|
let isEditable = await Self.app.models.Ticket.isEditable(loopBackContext, ticketId);
|
2019-05-29 11:06:42 +00:00
|
|
|
if (!isEditable)
|
|
|
|
throw new UserError(`The current ticket can't be modified`);
|
2019-04-23 09:25:49 +00:00
|
|
|
}
|
|
|
|
});
|
2019-05-29 11:06:42 +00:00
|
|
|
|
|
|
|
Self.observe('before delete', async ctx => {
|
2019-07-22 11:14:00 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
2019-05-29 11:06:42 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const service = await models.TicketService.findById(ctx.where.id);
|
2019-07-22 11:14:00 +00:00
|
|
|
const isEditable = await Self.app.models.Ticket.isEditable(loopBackContext, service.ticketFk);
|
2019-05-29 11:06:42 +00:00
|
|
|
|
|
|
|
if (!isEditable)
|
|
|
|
throw new UserError(`The current ticket can't be modified`);
|
|
|
|
});
|
2019-04-23 09:25:49 +00:00
|
|
|
};
|