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

25 lines
903 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});
2019-10-10 07:59:28 +00:00
let params = {ticketFk: ctx.instance.shipFk, 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);
}
});
};