34 lines
1.4 KiB
JavaScript
34 lines
1.4 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
module.exports = Self => {
|
|
Self.observe('before save', async ctx => {
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
const httpCtx = {req: loopBackContext.active};
|
|
const models = Self.app.models;
|
|
let changes = ctx.currentInstance || ctx.instance;
|
|
if (changes) {
|
|
let ticketId = changes.ticketFk;
|
|
let isEditable = await models.Ticket.isEditable(httpCtx, ticketId);
|
|
if (!isEditable)
|
|
throw new UserError(`The current ticket can't be modified`);
|
|
|
|
if (changes.ticketServiceTypeFk) {
|
|
const ticketServiceType = await models.TicketServiceType.findById(changes.ticketServiceTypeFk);
|
|
changes.description = ticketServiceType.name;
|
|
}
|
|
}
|
|
});
|
|
|
|
Self.observe('before delete', async ctx => {
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
const httpCtx = {req: loopBackContext.active};
|
|
const models = Self.app.models;
|
|
const service = await models.TicketService.findById(ctx.where.id);
|
|
const isEditable = await models.Ticket.isEditable(httpCtx, service.ticketFk);
|
|
|
|
if (!isEditable)
|
|
throw new UserError(`The current ticket can't be modified`);
|
|
});
|
|
};
|