diff --git a/modules/route/back/methods/route/getSuggestedTickets.js b/modules/route/back/methods/route/getSuggestedTickets.js index c1d6b67fe..f0333e66b 100644 --- a/modules/route/back/methods/route/getSuggestedTickets.js +++ b/modules/route/back/methods/route/getSuggestedTickets.js @@ -25,6 +25,7 @@ module.exports = Self => { Object.assign(myOptions, options); const route = await Self.app.models.Route.findById(id, null, myOptions); + const zoneAgencyModes = await Self.app.models.ZoneAgencyMode.find({ where: { agencyModeFk: route.agencyModeFk @@ -52,6 +53,12 @@ module.exports = Self => { fields: ['id', 'name'] } }, + { + relation: 'zone', + scope: { + fields: ['id', 'name'] + } + }, { relation: 'address', scope: { diff --git a/modules/route/back/methods/route/specs/unlink.spec.js b/modules/route/back/methods/route/specs/unlink.spec.js new file mode 100644 index 000000000..808cedccc --- /dev/null +++ b/modules/route/back/methods/route/specs/unlink.spec.js @@ -0,0 +1,33 @@ +const models = require('vn-loopback/server/server').models; + +describe('route unlink()', () => { + it('should show no tickets since the link between zone and route for the give agencymode was removed', async() => { + const tx = await models.ZoneAgencyMode.beginTransaction({}); + const agencyModeId = 1; + const zoneId = 1; + routeId = 1; + + try { + const options = {transaction: tx}; + + let zoneAgencyModes = await models.ZoneAgencyMode.find(null, options); + let tickets = await models.Route.getSuggestedTickets(routeId, options); + + expect(zoneAgencyModes.length).toEqual(4); + expect(tickets.length).toEqual(3); + + await models.Route.unlink(agencyModeId, zoneId, options); + + zoneAgencyModes = await models.ZoneAgencyMode.find(null, options); + tickets = await models.Route.getSuggestedTickets(routeId, options); + + expect(zoneAgencyModes.length).toEqual(3); + expect(tickets.length).toEqual(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/route/back/methods/route/unlink.js b/modules/route/back/methods/route/unlink.js new file mode 100644 index 000000000..5a847e337 --- /dev/null +++ b/modules/route/back/methods/route/unlink.js @@ -0,0 +1,42 @@ +module.exports = Self => { + Self.remoteMethod('unlink', { + description: 'Removes the matching entries from zoneAgencyMode', + accessType: 'WRITE', + accepts: [ + { + arg: 'agencyModeId', + type: 'number', + required: true, + description: 'The agencyMode id', + }, + { + arg: 'zoneId', + type: 'number', + required: true, + description: 'The zone id', + }, + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/unlink`, + verb: 'POST' + } + }); + + Self.unlink = async(agencyModeId, zoneId, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const where = { + agencyModeFk: agencyModeId, + zoneFk: zoneId + }; + + await Self.app.models.ZoneAgencyMode.destroyAll(where, myOptions); + }; +}; diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index 1cfe0927e..c82d1722e 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -8,6 +8,7 @@ module.exports = Self => { require('../methods/route/insertTicket')(Self); require('../methods/route/clone')(Self); require('../methods/route/getSuggestedTickets')(Self); + require('../methods/route/unlink')(Self); Self.validate('kmStart', validateDistance, { message: 'Distance must be lesser than 1000' diff --git a/modules/route/front/tickets/index.html b/modules/route/front/tickets/index.html index 7d515b67c..970c7574b 100644 --- a/modules/route/front/tickets/index.html +++ b/modules/route/front/tickets/index.html @@ -150,7 +150,7 @@ PC Address - Warehouse + Zone @@ -174,7 +174,15 @@ {{::ticket.address.city}} {{::ticket.address.postalCode}} {{::ticket.address.street}} - {{::ticket.warehouse.name}} + + {{::ticket.zone.name}} + + + @@ -196,3 +204,11 @@ + + + + \ No newline at end of file diff --git a/modules/route/front/tickets/index.js b/modules/route/front/tickets/index.js index fd763e32f..e74cbcd40 100644 --- a/modules/route/front/tickets/index.js +++ b/modules/route/front/tickets/index.js @@ -37,6 +37,19 @@ class Controller extends Section { }); } + unlinkZone(ticket) { + const params = { + agencyModeId: this.route.agencyModeFk, + zoneId: ticket.zoneFk, + }; + + const query = `Routes/unlink`; + this.$http.post(query, params).then(() => { + this.vnApp.showSuccess(this.$t('Data saved!')); + this.$.possibleTicketsModel.refresh(); + }); + } + getSelectedItems(items) { const selectedItems = []; diff --git a/modules/route/front/tickets/index.spec.js b/modules/route/front/tickets/index.spec.js index fbbe94360..092445e6f 100644 --- a/modules/route/front/tickets/index.spec.js +++ b/modules/route/front/tickets/index.spec.js @@ -1,3 +1,4 @@ +/* eslint max-len: ["error", { "code": 150 }]*/ import './index'; describe('Route', () => { @@ -73,6 +74,32 @@ describe('Route', () => { }); }); + describe('unlink()', () => { + it('should call the route unlink endpoint with the agency and zone ids', () => { + controller.$.possibleTicketsModel = {refresh: jest.fn()}; + jest.spyOn(controller.vnApp, 'showSuccess'); + + controller.route = { + agencyModeFk: 1 + }; + + const ticket = { + zoneFk: 2, + }; + const params = { + agencyModeId: controller.route.agencyModeFk, + zoneId: ticket.zoneFk, + }; + + $httpBackend.expectPOST(`Routes/unlink`, params).respond('ok'); + controller.unlinkZone(ticket); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + expect(controller.$.possibleTicketsModel.refresh).toHaveBeenCalledWith(); + }); + }); + describe('getSelectedItems()', () => { it('should return the selected items', () => { let items = [ diff --git a/modules/route/front/tickets/locale/es.yml b/modules/route/front/tickets/locale/es.yml index c38d4115c..6d63b4b6e 100644 --- a/modules/route/front/tickets/locale/es.yml +++ b/modules/route/front/tickets/locale/es.yml @@ -11,4 +11,5 @@ The selected ticket is not suitable for this route: El ticket seleccionado no es PC: CP The route's vehicle doesn't have a delivery point: El vehículo de la ruta no tiene un punto de entrega The route doesn't have a vehicle: La ruta no tiene un vehículo -Population: Población \ No newline at end of file +Population: Población +Unlink selected zone?: Desvincular zona seleccionada? diff --git a/modules/zone/back/models/agency.json b/modules/zone/back/models/agency.json index 9269b3db6..edec36f87 100644 --- a/modules/zone/back/models/agency.json +++ b/modules/zone/back/models/agency.json @@ -9,11 +9,11 @@ "properties": { "id": { "id": true, - "type": "Number", + "type": "number", "forceId": false }, "name": { - "type": "String", + "type": "string", "required": false } } diff --git a/modules/zone/back/models/zone.json b/modules/zone/back/models/zone.json index ad43bd6f6..5d5970173 100644 --- a/modules/zone/back/models/zone.json +++ b/modules/zone/back/models/zone.json @@ -13,10 +13,10 @@ "properties": { "id": { "id": true, - "type": "Number" + "type": "number" }, "name": { - "type": "String", + "type": "string", "required": true }, "hour": { @@ -24,22 +24,22 @@ "required": true }, "travelingDays": { - "type": "Number" + "type": "number" }, "price": { - "type": "Number" + "type": "number" }, "bonus": { - "type": "Number" + "type": "number" }, "isVolumetric": { - "type": "Boolean" + "type": "boolean" }, "inflation": { - "type": "Number" + "type": "number" }, "itemMaxSize": { - "type": "Number" + "type": "number" } }, "relations": {