2019-04-23 09:25:49 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.observe('before save', async ctx => {
|
2019-07-26 09:48:01 +00:00
|
|
|
const models = Self.app.models;
|
2022-07-14 11:40:50 +00:00
|
|
|
const changes = ctx.currentInstance || ctx.instance;
|
|
|
|
|
|
|
|
if (changes && !ctx.isNewInstance) {
|
|
|
|
const ticketId = changes.ticketFk;
|
|
|
|
const isLocked = await models.Ticket.isLocked(ticketId);
|
2021-04-22 07:06:08 +00:00
|
|
|
if (isLocked)
|
2019-05-29 11:06:42 +00:00
|
|
|
throw new UserError(`The current ticket can't be modified`);
|
2022-07-14 11:40:50 +00:00
|
|
|
}
|
2019-12-13 10:59:25 +00:00
|
|
|
|
2022-07-14 11:40:50 +00:00
|
|
|
if (changes && changes.ticketServiceTypeFk) {
|
|
|
|
const ticketServiceType = await models.TicketServiceType.findById(changes.ticketServiceTypeFk);
|
|
|
|
changes.description = ticketServiceType.name;
|
2019-04-23 09:25:49 +00:00
|
|
|
}
|
|
|
|
});
|
2019-05-29 11:06:42 +00:00
|
|
|
|
|
|
|
Self.observe('before delete', async ctx => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const service = await models.TicketService.findById(ctx.where.id);
|
2021-04-22 07:06:08 +00:00
|
|
|
const isLocked = await models.Ticket.isLocked(service.ticketFk);
|
2019-05-29 11:06:42 +00:00
|
|
|
|
2021-04-22 07:06:08 +00:00
|
|
|
if (isLocked)
|
2019-05-29 11:06:42 +00:00
|
|
|
throw new UserError(`The current ticket can't be modified`);
|
|
|
|
});
|
2019-04-23 09:25:49 +00:00
|
|
|
};
|