3888-ticket.expedition_moveExpedition #1097

Merged
vicent merged 26 commits from 3888-ticket.expedition_moveExpedition into dev 2022-11-02 13:54:11 +00:00
3 changed files with 55 additions and 25 deletions
Showing only changes of commit 9ea427dbfe - Show all commits

View File

@ -235,5 +235,6 @@
"Dirección incorrecta": "Dirección incorrecta", "Dirección incorrecta": "Dirección incorrecta",
"Modifiable user details only by an administrator": "Detalles de usuario modificables solo por un administrador", "Modifiable user details only by an administrator": "Detalles de usuario modificables solo por un administrador",
"Modifiable password only via recovery or by an administrator": "Contraseña modificable solo a través de la recuperación o por un administrador", "Modifiable password only via recovery or by an administrator": "Contraseña modificable solo a través de la recuperación o por un administrador",
"Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente" "Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente",
"This route not exists": "Esta ruta no existe"
vicent marked this conversation as resolved Outdated
Outdated
Review

This route does not exists

This route does not exists
} }

View File

@ -1,19 +1,47 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('moveExpeditions', { Self.remoteMethodCtx('moveExpeditions', {
description: 'Move the selected expeditions to another ticket', description: 'Move the selected expeditions to another ticket',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [{ accepts: [{
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
},
{
arg: 'addressId',
type: 'number',
description: `The address id`,
required: true
},
{
arg: 'agencyModeId',
type: 'any',
description: `The agencyMode id`
},
{
arg: 'routeId',
type: 'any',
description: `The route id`
},
{
arg: 'expeditionIds', arg: 'expeditionIds',
type: ['number'], type: ['number'],
required: true, required: true,
description: 'The expeditions ids to nove' description: 'The expeditions ids to move'
},
{
arg: 'ticketId',
type: 'number',
required: true,
description: 'the ticket id to which the expeditions are added'
}], }],
returns: { returns: {
type: 'object', type: 'object',
@ -25,8 +53,9 @@ module.exports = Self => {
} }
}); });
Self.moveExpeditions = async(expeditionIds, ticketId, options) => { Self.moveExpeditions = async(ctx, options) => {
const models = Self.app.models; const models = Self.app.models;
const args = ctx.args;
const myOptions = {}; const myOptions = {};
let tx; let tx;
@ -39,18 +68,23 @@ module.exports = Self => {
} }
try { try {
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);
const promises = []; const promises = [];
for (let expeditionsId of expeditionIds) { for (let expeditionsId of args.expeditionIds) {
const expeditionToUpdate = await models.Expedition.findById(expeditionsId); const expeditionToUpdate = await models.Expedition.findById(expeditionsId);
vicent marked this conversation as resolved Outdated
Outdated
Review

pasar transacción null, myOptions

pasar transacción null, myOptions
const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticketId, myOptions); const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticket.id, myOptions);
promises.push(expeditionUpdated); promises.push(expeditionUpdated);
} }
const updated = await Promise.all(promises); await Promise.all(promises);
if (tx) await tx.commit(); if (tx) await tx.commit();
return updated; return ticket;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;

View File

@ -44,22 +44,17 @@ class Controller extends Section {
} }
createTicket(landed, routeFk) { createTicket(landed, routeFk) {
const ticketParams = { const params = {
clientId: this.ticket.clientFk, clientId: this.ticket.clientFk,
landed: landed, landed: landed,
warehouseId: this.ticket.warehouseFk,
addressId: this.ticket.addressFk, addressId: this.ticket.addressFk,
agencyModeId: this.ticket.agencyModeFk, agencyModeId: this.ticket.agencyModeFk,
warehouseId: this.ticket.warehouseFk routeId: routeFk,
}; expeditionIds: this.checked
const query = `Tickets/new`;
this.$http.post(query, ticketParams).then(res => {
if (routeFk) this.$http.patch(`Tickets/${res.data.id}`, {routeFk: routeFk});
const params = {
expeditionIds: this.checked,
ticketId: res.data.id
}; };
const query = `Expeditions/moveExpeditions`; const query = `Expeditions/moveExpeditions`;
this.$http.post(query, params); this.$http.post(query, params).then(res => {
this.vnApp.showSuccess(this.$t('Data saved!')); this.vnApp.showSuccess(this.$t('Data saved!'));
this.$state.go('ticket.card.summary', {id: res.data.id}); this.$state.go('ticket.card.summary', {id: res.data.id});
}); });