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

37 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('moveToTicket', {
2018-09-13 10:39:56 +00:00
description: 'Moves a line to a given ticket',
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)
throw new UserError(`The sales of this ticket can't be modified`);
let newTicketIsEditable = await Self.app.models.Ticket.isEditable(params.newTicketFk);
if (!newTicketIsEditable)
throw new UserError(`The sales of this ticket can't be modified`);
2018-12-27 11:54:16 +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});
};
};