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-06-06 14:54:40 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('removes', {
|
|
|
|
description: 'Change the state of a ticket',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2018-06-06 14:54:40 +00:00
|
|
|
accepts: [{
|
|
|
|
arg: 'params',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: '[sales IDs], actualTicketFk',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/removes`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.removes = async params => {
|
|
|
|
let thisTicketIsEditable = await Self.app.models.Ticket.isEditable(params.actualTicketFk);
|
|
|
|
if (!thisTicketIsEditable)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2018-06-06 14:54:40 +00:00
|
|
|
|
2018-08-21 10:56:32 +00:00
|
|
|
let promises = [];
|
2018-12-27 11:54:16 +00:00
|
|
|
for (let i = 0; i < params.sales.length; i++)
|
2018-08-21 10:56:32 +00:00
|
|
|
promises.push(Self.app.models.Sale.destroyById(params.sales[i].id));
|
2018-12-27 11:54:16 +00:00
|
|
|
|
2018-08-21 10:56:32 +00:00
|
|
|
return Promise.all(promises);
|
2018-06-06 14:54:40 +00:00
|
|
|
};
|
|
|
|
};
|