salix/modules/ticket/back/models/stowaway.js

25 lines
903 B
JavaScript

const LoopBackContext = require('loopback-context');
const UserError = require('vn-loopback/util/user-error');
module.exports = function(Self) {
Self.observe('before save', async function(ctx) {
const models = Self.app.models;
const canHaveStowaway = await models.Ticket.canHaveStowaway(ctx.instance.shipFk);
if (!canHaveStowaway)
throw new UserError(`Can't create stowaway for this ticket`);
if (ctx.isNewInstance) {
let where = {
code: 'BOARDING'
};
let state = await models.State.findOne({where});
let params = {ticketFk: ctx.instance.shipFk, stateFk: state.id};
const loopBackContext = LoopBackContext.getCurrentContext();
let httpCtx = {req: loopBackContext.active};
await models.TicketTracking.changeState(httpCtx, params);
}
});
};