2019-06-21 05:41:03 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2019-09-06 07:47:58 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
2019-06-21 05:41:03 +00:00
|
|
|
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-06 07:47:58 +00:00
|
|
|
|
2019-09-25 09:26:12 +00:00
|
|
|
if (!canHaveStowaway)
|
2019-09-06 07:47:58 +00:00
|
|
|
throw new UserError(`Can't create stowaway for this ticket`);
|
|
|
|
|
2019-06-21 05:41:03 +00:00
|
|
|
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};
|
2019-06-21 05:41:03 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
|
|
|
|
let httpCtx = {req: loopBackContext.active};
|
2019-09-25 09:26:12 +00:00
|
|
|
await models.TicketTracking.changeState(httpCtx, params);
|
2019-06-21 05:41:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|