44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('deleteSales', {
|
|
description: 'Deletes the selected sales',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'sales',
|
|
type: ['Object'],
|
|
required: true,
|
|
description: 'The sales to remove'
|
|
},
|
|
{
|
|
arg: 'ticketId',
|
|
type: 'Number',
|
|
required: true,
|
|
description: 'The ticket id'
|
|
}],
|
|
returns: {
|
|
type: ['Object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/deleteSales`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.deleteSales = async(ctx, sales, ticketId) => {
|
|
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 deletedSale = models.Sale.destroyById(sale.id);
|
|
promises.push(deletedSale);
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
};
|
|
};
|