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

54 lines
1.5 KiB
JavaScript

let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('reserve', {
description: 'Change the state of a ticket',
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 canEditSale = await models.Sale.canEdit(ctx, sales);
if (!canEditSale)
throw new UserError(`Sale(s) blocked, please contact production`);
const promises = [];
for (let sale of sales) {
const reservedSale = models.Sale.update({id: sale.id}, {reserved: reserved});
promises.push(reservedSale);
}
return Promise.all(promises);
};
};