2020-07-01 12:28:05 +00:00
|
|
|
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;
|
2021-05-26 07:14:18 +00:00
|
|
|
|
|
|
|
const canEditSales = await models.Sale.canEdit(ctx, sales);
|
|
|
|
|
2020-07-01 12:28:05 +00:00
|
|
|
const isTicketEditable = await models.Ticket.isEditable(ctx, ticketId);
|
|
|
|
if (!isTicketEditable)
|
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
|
|
|
|
2021-05-26 07:14:18 +00:00
|
|
|
if (!canEditSales)
|
|
|
|
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 deletedSale = models.Sale.destroyById(sale.id);
|
|
|
|
promises.push(deletedSale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises);
|
|
|
|
};
|
|
|
|
};
|