refactor: move front to back funcionality
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2022-10-18 13:45:46 +02:00
parent ae3e3c3c49
commit 9ea427dbfe
3 changed files with 55 additions and 25 deletions

View File

@ -235,5 +235,6 @@
"Dirección incorrecta": "Dirección incorrecta",
"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",
"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"
}

View File

@ -1,19 +1,47 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('moveExpeditions', {
Self.remoteMethodCtx('moveExpeditions', {
description: 'Move the selected expeditions to another ticket',
accessType: 'WRITE',
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',
type: ['number'],
required: true,
description: 'The expeditions ids to nove'
},
{
arg: 'ticketId',
type: 'number',
required: true,
description: 'the ticket id to which the expeditions are added'
description: 'The expeditions ids to move'
}],
returns: {
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 args = ctx.args;
const myOptions = {};
let tx;
@ -39,18 +68,23 @@ module.exports = Self => {
}
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 = [];
for (let expeditionsId of expeditionIds) {
for (let expeditionsId of args.expeditionIds) {
const expeditionToUpdate = await models.Expedition.findById(expeditionsId);
const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticketId, myOptions);
const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticket.id, myOptions);
promises.push(expeditionUpdated);
}
const updated = await Promise.all(promises);
await Promise.all(promises);
if (tx) await tx.commit();
return updated;
return ticket;
} catch (e) {
if (tx) await tx.rollback();
throw e;

View File

@ -44,22 +44,17 @@ class Controller extends Section {
}
createTicket(landed, routeFk) {
const ticketParams = {
const params = {
clientId: this.ticket.clientFk,
landed: landed,
warehouseId: this.ticket.warehouseFk,
addressId: this.ticket.addressFk,
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`;
this.$http.post(query, params);
const query = `Expeditions/moveExpeditions`;
this.$http.post(query, params).then(res => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.$state.go('ticket.card.summary', {id: res.data.id});
});