2018-08-02 07:49:00 +00:00
|
|
|
let UserError = require('../../helpers').UserError;
|
|
|
|
|
2018-06-06 14:54:40 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('moveToTicket', {
|
2018-09-13 10:39:56 +00:00
|
|
|
description: 'Moves a line to a given ticket',
|
2018-06-06 14:54:40 +00:00
|
|
|
accessType: '',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'params',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: '[sales IDs], newTicketFk, actualTicketFk',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/moveToTicket`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.moveToTicket = 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
|
|
|
|
|
|
|
let newTicketIsEditable = await Self.app.models.Ticket.isEditable(params.newTicketFk);
|
|
|
|
if (!newTicketIsEditable)
|
2018-09-21 13:01:51 +00:00
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2018-06-06 14:54:40 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < params.sales.length; i++) {
|
|
|
|
await Self.app.models.Sale.update({id: params.sales[i].id}, {ticketFk: params.newTicketFk});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|