salix/modules/ticket/back/models/ticket-service.js

39 lines
1.5 KiB
JavaScript
Raw Normal View History

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 => {
const models = Self.app.models;
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)
throw new UserError(`The current ticket can't be modified`);
const isRefund = await models.TicketRefund.findOne({
where: {refundTicketFk: ticketId}
}, {
transaction: ctx.options.transaction
});
if (isRefund && ctx.data && Object.keys(ctx.data).some(field => field !== 'ticketServiceTypeFk'))
throw new UserError('Only description can be modified in refund tickets');
}
if (changes?.ticketServiceTypeFk) {
const ticketServiceType = await models.TicketServiceType.findById(changes.ticketServiceTypeFk);
changes.description = ticketServiceType.name;
2019-04-23 09:25:49 +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);
2021-04-22 07:06:08 +00:00
if (isLocked)
throw new UserError(`The current ticket can't be modified`);
});
2019-04-23 09:25:49 +00:00
};