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

25 lines
899 B
JavaScript
Raw Normal View History

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