salix/modules/ticket/back/methods/ticket/deleteStowaway.js

85 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-12-17 07:38:36 +00:00
module.exports = Self => {
2020-03-26 13:45:38 +00:00
Self.remoteMethodCtx('deleteStowaway', {
2019-12-17 07:38:36 +00:00
description: 'Deletes an stowaway',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The ticket id',
http: {source: 'path'}
}],
returns: {
root: true
},
http: {
path: `/:id/deleteStowaway`,
verb: 'POST'
}
});
2020-03-26 13:45:38 +00:00
Self.deleteStowaway = async(ctx, id) => {
const models = Self.app.models;
const $t = ctx.req.__; // $translate
2019-12-17 07:38:36 +00:00
const ticket = await Self.findById(id, {
include: [{
relation: 'ship'
}, {
relation: 'stowaway'
2020-03-26 13:45:38 +00:00
}, {
relation: 'client',
scope: {
include: {
relation: 'salesPerson'
}
}
2019-12-17 07:38:36 +00:00
}]
});
2020-03-26 13:45:38 +00:00
let stowawayFk;
let shipFk;
if (ticket.stowaway()) {
shipFk = ticket.stowaway().shipFk;
stowawayFk = ticket.stowaway().id;
} else if (ticket.ship()) {
shipFk = ticket.ship().shipFk;
stowawayFk = ticket.ship().id;
}
const stowaway = await models.Stowaway.findOne({
where: {
id: stowawayFk,
shipFk: shipFk
}
});
const result = await stowaway.destroy();
const state = await models.State.findOne({
where: {
code: 'BOARDING'
}
});
const ticketTracking = await models.TicketTracking.findOne({
where: {
ticketFk: shipFk,
stateFk: state.id
}
});
await ticketTracking.destroy();
const salesPerson = ticket.client().salesPerson();
if (salesPerson) {
const origin = ctx.req.headers.origin;
const message = $t('This ticket is not an stowaway anymore', {
ticketId: stowawayFk,
ticketUrl: `${origin}/#!/ticket/${stowawayFk}/summary`
});
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
}
2019-12-17 07:38:36 +00:00
2020-03-26 13:45:38 +00:00
return result;
2019-12-17 07:38:36 +00:00
};
};