77 lines
2.4 KiB
JavaScript
77 lines
2.4 KiB
JavaScript
let UserError = require('../../helpers').UserError;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('moveToNewTicket', {
|
|
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'
|
|
}
|
|
});
|
|
|
|
Self.moveToNewTicket = async(ctx, params) => {
|
|
let userId = ctx.req.accessToken.userId;
|
|
let model = Self.app.models;
|
|
let thisTicketIsEditable = await model.Ticket.isEditable(params.ticket.oldTicketFk);
|
|
if (!thisTicketIsEditable)
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
|
|
|
let travelDates = await model.Agency.getFirstShipped(params.ticket);
|
|
let shipped = new Date(travelDates.vShipped);
|
|
shipped.setMinutes(shipped.getMinutes() + shipped.getTimezoneOffset());
|
|
|
|
let landed = new Date(travelDates.vLanded);
|
|
landed.setMinutes(landed.getMinutes() + landed.getTimezoneOffset());
|
|
|
|
let newTicketParams = {
|
|
clientFk: params.ticket.clientFk,
|
|
addressFk: params.ticket.addressFk,
|
|
agencyModeFk: params.ticket.agencyModeFk,
|
|
warehouseFk: params.ticket.warehouseFk,
|
|
shipped: shipped,
|
|
landed: landed,
|
|
userId: userId
|
|
};
|
|
|
|
let transaction = await Self.beginTransaction({});
|
|
try {
|
|
let newTicket = await model.Ticket.new(newTicketParams, {transaction: transaction});
|
|
|
|
let selectedSalesId = [];
|
|
params.sales.forEach(sale => {
|
|
selectedSalesId.push(sale.id);
|
|
});
|
|
|
|
await model.Sale.updateAll(
|
|
{id: {inq: selectedSalesId}},
|
|
{ticketFk: newTicket.id},
|
|
{transaction: transaction});
|
|
|
|
await transaction.commit();
|
|
|
|
return newTicket;
|
|
} catch (e) {
|
|
await transaction.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|