2018-07-03 13:00:16 +00:00
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-07-03 13:00:16 +00:00
|
|
|
module.exports = Self => {
|
2019-07-22 11:14:00 +00:00
|
|
|
Self.remoteMethodCtx('reserve', {
|
2018-07-03 13:00:16 +00:00
|
|
|
description: 'Change the state of a ticket',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2018-07-03 13:00:16 +00:00
|
|
|
accepts: [{
|
2020-07-01 12:28:05 +00:00
|
|
|
arg: 'ticketId',
|
|
|
|
type: 'Number',
|
2018-07-03 13:00:16 +00:00
|
|
|
required: true,
|
2020-07-01 12:28:05 +00:00
|
|
|
description: 'The ticket id'
|
|
|
|
}, {
|
|
|
|
arg: 'sales',
|
|
|
|
type: ['Object'],
|
|
|
|
required: true,
|
|
|
|
description: 'The sale to reserve'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'reserved',
|
|
|
|
type: 'Boolean',
|
|
|
|
required: true
|
2018-07-03 13:00:16 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
2020-07-01 12:28:05 +00:00
|
|
|
type: ['Object'],
|
2018-07-03 13:00:16 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/reserve`,
|
2020-07-01 12:28:05 +00:00
|
|
|
verb: 'POST'
|
2018-07-03 13:00:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-01 12:28:05 +00:00
|
|
|
Self.reserve = async(ctx, ticketId, sales, reserved) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const isTicketEditable = await models.Ticket.isEditable(ctx, ticketId);
|
|
|
|
if (!isTicketEditable)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2018-07-03 13:00:16 +00:00
|
|
|
|
2021-05-26 07:14:18 +00:00
|
|
|
const canEditSale = await models.Sale.canEdit(ctx, sales);
|
|
|
|
|
|
|
|
if (!canEditSale)
|
|
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
|
|
|
|
2020-07-01 12:28:05 +00:00
|
|
|
const promises = [];
|
|
|
|
for (let sale of sales) {
|
|
|
|
const reservedSale = models.Sale.update({id: sale.id}, {reserved: reserved});
|
|
|
|
promises.push(reservedSale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises);
|
2018-07-03 13:00:16 +00:00
|
|
|
};
|
|
|
|
};
|