2493-route_ticket_drag_and_drop #462
|
@ -0,0 +1,52 @@
|
||||||
|
const app = require('vn-loopback/server/server');
|
||||||
|
const LoopBackContext = require('loopback-context');
|
||||||
|
|
||||||
|
describe('route ticketToRoute()', () => {
|
||||||
|
const deliveryId = 56;
|
||||||
|
let originalTicket;
|
||||||
|
const routeId = 2;
|
||||||
|
const activeCtx = {
|
||||||
|
accessToken: {userId: deliveryId},
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeAll(async done => {
|
||||||
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||||
|
active: activeCtx
|
||||||
|
});
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async done => {
|
||||||
|
try {
|
||||||
|
await originalTicket.updateAttribute('routeFk', null);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add the ticket in a route', async() => {
|
||||||
|
|||||||
|
originalTicket = await app.models.Ticket.findById(14);
|
||||||
|
|
||||||
|
const ticketId = 14;
|
||||||
|
const result = await app.models.Route.ticketToRoute(ticketId, routeId);
|
||||||
|
|
||||||
|
expect(result.routeFk).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw and error if the ticket is not suitable for the route', async() => {
|
||||||
|
const ticketId = 23;
|
||||||
|
let error;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await app.models.Route.ticketToRoute(ticketId, routeId);
|
||||||
|
} catch (e) {
|
||||||
|
error = e.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error).toBeDefined();
|
||||||
|
expect(error).toEqual('The selected ticket is not suitable for this route');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethod('ticketToRoute', {
|
||||||
|
description: 'Check if the ticket can be insert into the route and insert it',
|
||||||
|
accessType: 'READ',
|
||||||
|
accepts: [{
|
||||||
|
arg: 'ticketId',
|
||||||
|
type: 'number',
|
||||||
|
required: true,
|
||||||
|
description: 'ticketId ',
|
||||||
|
http: {source: 'path'}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'routeId',
|
||||||
|
type: 'number',
|
||||||
|
required: true
|
||||||
|
}],
|
||||||
|
returns: {
|
||||||
|
type: 'object',
|
||||||
|
root: true
|
||||||
|
},
|
||||||
|
http: {
|
||||||
|
path: `/:ticketId/ticketToRoute`,
|
||||||
|
verb: 'PATCH'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.ticketToRoute = async(ticketId, routeId) => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
|
||||||
|
const route = await models.Route.findById(routeId);
|
||||||
|
const minDate = new Date(route.finished);
|
||||||
|
minDate.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
const maxDate = new Date(route.finished);
|
||||||
|
maxDate.setHours(23, 59, 59, 59);
|
||||||
|
const ticket = await models.Ticket.findOne({
|
||||||
|
where: {
|
||||||
|
id: ticketId,
|
||||||
|
zoneFk: route.zoneFk,
|
||||||
|
routeFk: null,
|
||||||
|
landed: {between: [minDate, maxDate]},
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!ticket)
|
||||||
|
throw new UserError('The selected ticket is not suitable for this route');
|
||||||
|
|
||||||
|
return await ticket.updateAttribute('routeFk', route.id);
|
||||||
|
};
|
||||||
|
};
|
|
@ -5,6 +5,7 @@ module.exports = Self => {
|
||||||
require('../methods/route/guessPriority')(Self);
|
require('../methods/route/guessPriority')(Self);
|
||||||
require('../methods/route/updateVolume')(Self);
|
require('../methods/route/updateVolume')(Self);
|
||||||
require('../methods/route/getDeliveryPoint')(Self);
|
require('../methods/route/getDeliveryPoint')(Self);
|
||||||
|
require('../methods/route/ticketToRoute')(Self);
|
||||||
|
|
||||||
Self.validate('kmStart', validateDistance, {
|
Self.validate('kmStart', validateDistance, {
|
||||||
message: 'Distance must be lesser than 1000'
|
message: 'Distance must be lesser than 1000'
|
||||||
|
@ -17,9 +18,7 @@ module.exports = Self => {
|
||||||
function validateDistance(err) {
|
function validateDistance(err) {
|
||||||
const routeTotalKm = this.kmEnd - this.kmStart;
|
const routeTotalKm = this.kmEnd - this.kmStart;
|
||||||
const routeMaxKm = 1000;
|
const routeMaxKm = 1000;
|
||||||
if ( routeTotalKm > routeMaxKm || this.kmStart > this.kmEnd)
|
if (routeTotalKm > routeMaxKm || this.kmStart > this.kmEnd)
|
||||||
err();
|
err();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -162,8 +162,10 @@ class Controller extends Section {
|
||||||
}
|
}
|
||||||
|
|
||||||
insert(id) {
|
insert(id) {
|
||||||
const params = {routeFk: this.route.id};
|
const params = {routeId: this.route.id};
|
||||||
this.$http.patch(`Tickets/${id}`, params).then(() => {
|
const query = `Routes/${id}/ticketToRoute`;
|
||||||
|
|
||||||
|
return this.$http.patch(query, params).then(() => {
|
||||||
this.vnApp.showSuccess(this.$t('Data saved!'));
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
||||||
this.$.model.refresh();
|
this.$.model.refresh();
|
||||||
this.card.reload();
|
this.card.reload();
|
||||||
|
|
|
@ -309,8 +309,8 @@ describe('Route', () => {
|
||||||
jest.spyOn(controller.vnApp, 'showSuccess');
|
jest.spyOn(controller.vnApp, 'showSuccess');
|
||||||
|
|
||||||
const ticketId = 11;
|
const ticketId = 11;
|
||||||
|
const data = {routeId: 1};
|
||||||
$httpBackend.expect('PATCH', `Tickets/11`).respond({id: 11});
|
$httpBackend.expect('PATCH', `Routes/11/ticketToRoute`, data).respond();
|
||||||
controller.insert(ticketId);
|
controller.insert(ticketId);
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,5 @@ Delete ticket from route?: ¿Quitar el ticket de la ruta?
|
||||||
Sort routes: Ordenar rutas
|
Sort routes: Ordenar rutas
|
||||||
Add ticket: Añadir ticket
|
Add ticket: Añadir ticket
|
||||||
Tickets to add: Tickets a añadir
|
Tickets to add: Tickets a añadir
|
||||||
Ticket not found: No se ha encontrado el ticket
|
Ticket not found: No se ha encontrado el ticket
|
||||||
|
The selected ticket is not suitable for this route: El ticket seleccionado no es apto para esta ruta
|
|
@ -14,6 +14,7 @@ describe('zone deletezone()', () => {
|
||||||
let ticketIDs;
|
let ticketIDs;
|
||||||
let originalZoneIncluded;
|
let originalZoneIncluded;
|
||||||
let originalTicketStates;
|
let originalTicketStates;
|
||||||
|
let originalRoutes;
|
||||||
|
|
||||||
beforeAll(async done => {
|
beforeAll(async done => {
|
||||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||||
|
@ -27,6 +28,7 @@ describe('zone deletezone()', () => {
|
||||||
zoneFk: zoneId
|
zoneFk: zoneId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
originalRoutes = await app.models.Route.find({where: {zoneFk: zoneId}});
|
||||||
ticketIDs = originalTickets.map(ticket => ticket.id);
|
ticketIDs = originalTickets.map(ticket => ticket.id);
|
||||||
originalZoneIncluded = await app.models.ZoneIncluded.find({where: {zoneFk: zoneId}});
|
originalZoneIncluded = await app.models.ZoneIncluded.find({where: {zoneFk: zoneId}});
|
||||||
originalTicketStates = await app.models.TicketState.find({where: {
|
originalTicketStates = await app.models.TicketState.find({where: {
|
||||||
|
@ -44,6 +46,9 @@ describe('zone deletezone()', () => {
|
||||||
await originalZone.save();
|
await originalZone.save();
|
||||||
await app.models.ZoneWarehouse.create(originalZoneWarehouses);
|
await app.models.ZoneWarehouse.create(originalZoneWarehouses);
|
||||||
|
|
||||||
|
for (route of originalRoutes)
|
||||||
|
await route.updateAttributes({zoneFk: zoneId});
|
||||||
|
|
||||||
for (ticket of originalTickets)
|
for (ticket of originalTickets)
|
||||||
await ticket.updateAttributes({zoneFk: zoneId});
|
await ticket.updateAttributes({zoneFk: zoneId});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Creo que lo correcto sería poner "to a route". Da a entender que es un ticket que ya esta en una ruta