Merge pull request '2493-route_ticket_drag_and_drop' (#462) from 2493-route_ticket_drag_and_drop into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #462
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Bernat Exposito 2020-11-24 07:57:36 +00:00
commit f2e8f7655b
7 changed files with 118 additions and 8 deletions

View File

@ -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 to 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');
});
});

View File

@ -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);
};
};

View File

@ -5,6 +5,7 @@ module.exports = Self => {
require('../methods/route/guessPriority')(Self);
require('../methods/route/updateVolume')(Self);
require('../methods/route/getDeliveryPoint')(Self);
require('../methods/route/ticketToRoute')(Self);
Self.validate('kmStart', validateDistance, {
message: 'Distance must be lesser than 1000'
@ -17,9 +18,7 @@ module.exports = Self => {
function validateDistance(err) {
const routeTotalKm = this.kmEnd - this.kmStart;
const routeMaxKm = 1000;
if ( routeTotalKm > routeMaxKm || this.kmStart > this.kmEnd)
if (routeTotalKm > routeMaxKm || this.kmStart > this.kmEnd)
err();
}
};

View File

@ -162,8 +162,10 @@ class Controller extends Section {
}
insert(id) {
const params = {routeFk: this.route.id};
this.$http.patch(`Tickets/${id}`, params).then(() => {
const params = {routeId: this.route.id};
const query = `Routes/${id}/ticketToRoute`;
return this.$http.patch(query, params).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.$.model.refresh();
this.card.reload();

View File

@ -309,8 +309,8 @@ describe('Route', () => {
jest.spyOn(controller.vnApp, 'showSuccess');
const ticketId = 11;
$httpBackend.expect('PATCH', `Tickets/11`).respond({id: 11});
const data = {routeId: 1};
$httpBackend.expect('PATCH', `Routes/11/ticketToRoute`, data).respond();
controller.insert(ticketId);
$httpBackend.flush();

View File

@ -6,4 +6,5 @@ Delete ticket from route?: ¿Quitar el ticket de la ruta?
Sort routes: Ordenar rutas
Add ticket: Añadir ticket
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

View File

@ -14,6 +14,7 @@ describe('zone deletezone()', () => {
let ticketIDs;
let originalZoneIncluded;
let originalTicketStates;
let originalRoutes;
beforeAll(async done => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
@ -27,6 +28,7 @@ describe('zone deletezone()', () => {
zoneFk: zoneId
}
});
originalRoutes = await app.models.Route.find({where: {zoneFk: zoneId}});
ticketIDs = originalTickets.map(ticket => ticket.id);
originalZoneIncluded = await app.models.ZoneIncluded.find({where: {zoneFk: zoneId}});
originalTicketStates = await app.models.TicketState.find({where: {
@ -44,6 +46,9 @@ describe('zone deletezone()', () => {
await originalZone.save();
await app.models.ZoneWarehouse.create(originalZoneWarehouses);
for (route of originalRoutes)
await route.updateAttributes({zoneFk: zoneId});
for (ticket of originalTickets)
await ticket.updateAttributes({zoneFk: zoneId});