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-06 07:47:58 +00:00
|
|
|
let isStowaway = await Self.app.models.Ticket.canBeStowawayed(ctx.instance.id);
|
|
|
|
|
|
|
|
if (!isStowaway)
|
|
|
|
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'
|
|
|
|
};
|
|
|
|
let state = await Self.app.models.State.findOne({where});
|
|
|
|
let params = {ticketFk: ctx.instance.id, stateFk: state.id};
|
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
|
|
|
|
let httpCtx = {req: loopBackContext.active};
|
|
|
|
await Self.app.models.TicketTracking.changeState(httpCtx, params);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|