2018-08-02 07:49:00 +00:00
|
|
|
let UserError = require('../../helpers').UserError;
|
|
|
|
|
2018-06-28 13:24:27 +00:00
|
|
|
module.exports = Self => {
|
2018-07-24 09:27:04 +00:00
|
|
|
Self.remoteMethodCtx('moveToNewTicket', {
|
2018-06-28 13:24:27 +00:00
|
|
|
description: 'Change the state of a ticket',
|
|
|
|
accessType: '',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'ticketParams',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: '[sales IDs], newTicketFk, actualTicketFk',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}, {
|
|
|
|
arg: 'sales',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: '[sales IDs]',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/moveToNewTicket`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-24 09:27:04 +00:00
|
|
|
Self.moveToNewTicket = async(ctx, params) => {
|
|
|
|
let userId = ctx.req.accessToken.userId;
|
2018-06-28 13:24:27 +00:00
|
|
|
let model = Self.app.models;
|
|
|
|
let thisTicketIsEditable = await model.Ticket.isEditable(params.ticket.oldTicketFk);
|
|
|
|
if (!thisTicketIsEditable)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2018-06-28 13:24:27 +00:00
|
|
|
|
|
|
|
let travelDates = await model.Agency.getFirstShipped(params.ticket);
|
2018-07-10 12:55:13 +00:00
|
|
|
let shipped = new Date(travelDates.vShipped);
|
|
|
|
shipped.setMinutes(shipped.getMinutes() + shipped.getTimezoneOffset());
|
|
|
|
|
|
|
|
let landed = new Date(travelDates.vLanded);
|
|
|
|
landed.setMinutes(landed.getMinutes() + landed.getTimezoneOffset());
|
2018-06-28 13:56:45 +00:00
|
|
|
|
2018-06-28 13:24:27 +00:00
|
|
|
let newTicketParams = {
|
|
|
|
clientFk: params.ticket.clientFk,
|
|
|
|
addressFk: params.ticket.addressFk,
|
|
|
|
agencyModeFk: params.ticket.agencyModeFk,
|
|
|
|
warehouseFk: params.ticket.warehouseFk,
|
2018-07-10 12:55:13 +00:00
|
|
|
shipped: shipped,
|
2018-07-24 09:27:04 +00:00
|
|
|
landed: landed,
|
|
|
|
userId: userId
|
2018-06-28 13:24:27 +00:00
|
|
|
};
|
|
|
|
|
2018-07-11 11:52:04 +00:00
|
|
|
let newTicket = await model.Ticket.new(newTicketParams);
|
2018-06-28 13:24:27 +00:00
|
|
|
|
2018-07-24 09:27:04 +00:00
|
|
|
let promises = [];
|
2018-06-28 13:24:27 +00:00
|
|
|
for (let i = 0; i < params.sales.length; i++) {
|
2018-07-24 09:27:04 +00:00
|
|
|
promises.push(model.Sale.update({id: params.sales[i].id}, {ticketFk: newTicket}));
|
2018-06-28 13:24:27 +00:00
|
|
|
}
|
2018-07-24 09:27:04 +00:00
|
|
|
await Promise.all(promises);
|
|
|
|
|
2018-06-28 13:24:27 +00:00
|
|
|
return newTicket;
|
|
|
|
};
|
|
|
|
};
|