salix/modules/ticket/back/methods/sale/reserve.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

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