2022-10-18 11:45:46 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2022-10-07 08:47:54 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
2022-10-18 11:45:46 +00:00
|
|
|
Self.remoteMethodCtx('moveExpeditions', {
|
2022-10-07 08:47:54 +00:00
|
|
|
description: 'Move the selected expeditions to another ticket',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2022-10-18 11:45:46 +00:00
|
|
|
arg: 'clientId',
|
|
|
|
type: 'number',
|
|
|
|
description: `The client id`,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'landed',
|
|
|
|
type: 'date',
|
|
|
|
description: `The landing date`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'warehouseId',
|
|
|
|
type: 'number',
|
|
|
|
description: `The warehouse id`,
|
|
|
|
required: true
|
2022-10-07 08:47:54 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-18 11:45:46 +00:00
|
|
|
arg: 'addressId',
|
2022-10-07 08:47:54 +00:00
|
|
|
type: 'number',
|
2022-10-18 11:45:46 +00:00
|
|
|
description: `The address id`,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'agencyModeId',
|
|
|
|
type: 'any',
|
|
|
|
description: `The agencyMode id`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'routeId',
|
|
|
|
type: 'any',
|
|
|
|
description: `The route id`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'expeditionIds',
|
|
|
|
type: ['number'],
|
2022-10-07 08:47:54 +00:00
|
|
|
required: true,
|
2022-10-18 11:45:46 +00:00
|
|
|
description: 'The expeditions ids to move'
|
2022-10-07 08:47:54 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/moveExpeditions`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-18 11:45:46 +00:00
|
|
|
Self.moveExpeditions = async(ctx, options) => {
|
2022-10-07 08:47:54 +00:00
|
|
|
const models = Self.app.models;
|
2022-10-18 11:45:46 +00:00
|
|
|
const args = ctx.args;
|
2022-10-07 08:47:54 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-10-18 11:45:46 +00:00
|
|
|
if (args.routeId) {
|
|
|
|
const route = await models.Route.findById(args.routeId, null, myOptions);
|
|
|
|
if (!route) throw new UserError('This route not exists');
|
|
|
|
}
|
|
|
|
const ticket = await models.Ticket.new(ctx, myOptions);
|
2022-10-07 08:47:54 +00:00
|
|
|
const promises = [];
|
2022-10-18 11:45:46 +00:00
|
|
|
for (let expeditionsId of args.expeditionIds) {
|
2022-10-07 08:47:54 +00:00
|
|
|
const expeditionToUpdate = await models.Expedition.findById(expeditionsId);
|
2022-10-18 11:45:46 +00:00
|
|
|
const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticket.id, myOptions);
|
2022-10-07 08:47:54 +00:00
|
|
|
promises.push(expeditionUpdated);
|
|
|
|
}
|
|
|
|
|
2022-10-18 11:45:46 +00:00
|
|
|
await Promise.all(promises);
|
2022-10-07 08:47:54 +00:00
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
2022-10-18 11:45:46 +00:00
|
|
|
return ticket;
|
2022-10-07 08:47:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|