72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethod('insertTicket', {
|
|
description: 'Check if the ticket can be insert into the route and insert it',
|
|
accessType: 'READ',
|
|
accepts: [{
|
|
arg: 'routeId',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The route id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'ticketId',
|
|
type: 'number',
|
|
required: true
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:routeId/insertTicket`,
|
|
verb: 'PATCH'
|
|
}
|
|
});
|
|
|
|
Self.insertTicket = async(routeId, ticketId, options) => {
|
|
const models = Self.app.models;
|
|
|
|
let tx;
|
|
let myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const route = await models.Route.findById(routeId, null, myOptions);
|
|
const minDate = new Date(route.created);
|
|
minDate.setHours(0, 0, 0, 0);
|
|
|
|
const maxDate = new Date(route.created);
|
|
maxDate.setHours(23, 59, 59, 59);
|
|
const ticket = await models.Ticket.findOne({
|
|
where: {
|
|
id: ticketId,
|
|
routeFk: null,
|
|
landed: {between: [minDate, maxDate]},
|
|
}
|
|
}, myOptions);
|
|
|
|
if (!ticket)
|
|
throw new UserError('The selected ticket is not suitable for this route');
|
|
|
|
const result = await ticket.updateAttribute('routeFk', route.id, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return result;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|