From 3c41c4656b47eab40559674d5c66e45a324c49bf Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 6 Mar 2020 13:06:01 +0100 Subject: [PATCH] 2111 - Drop tickets to route ticket list --- loopback/locale/es.json | 3 +- modules/client/front/fiscal-data/index.js | 3 +- .../client/front/fiscal-data/index.spec.js | 3 +- modules/route/front/tickets/index.html | 6 +- modules/route/front/tickets/index.js | 57 ++++++++---- modules/route/front/tickets/index.spec.js | 86 ++++++++++++++++--- modules/route/front/tickets/locale/es.yml | 3 +- modules/travel/front/summary/index.spec.js | 1 - 8 files changed, 127 insertions(+), 35 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 7577c5349..06c708981 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -127,5 +127,6 @@ "Sent units from ticket": "Envio *{{quantity}}* unidades de [{{concept}} (#{{itemId}})]({{{itemUrl}}}) a *\"{{nickname}}\"* provenientes del ticket id [#{{ticketId}}]({{{ticketUrl}}})", "Client checked as validated despite of duplication": "Cliente comprobado a pesar de que existe el cliente id {{clientId}}", "ORDER_ROW_UNAVAILABLE": "No hay disponibilidad de este producto", - "Distance must be lesser than 1000": "La distancia debe ser inferior a 1000" + "Distance must be lesser than 1000": "La distancia debe ser inferior a 1000", + "This ticket is deleted": "This ticket is deleted" } \ No newline at end of file diff --git a/modules/client/front/fiscal-data/index.js b/modules/client/front/fiscal-data/index.js index 7fdb03dda..f302606dd 100644 --- a/modules/client/front/fiscal-data/index.js +++ b/modules/client/front/fiscal-data/index.js @@ -45,7 +45,8 @@ export default class Controller extends Component { this.$.confirmDuplicatedClient.show(); }).catch(error => { if (error.status == 404) - this.save(); + return this.save(); + throw error; }); } diff --git a/modules/client/front/fiscal-data/index.spec.js b/modules/client/front/fiscal-data/index.spec.js index 30ff80d04..a884f3e97 100644 --- a/modules/client/front/fiscal-data/index.spec.js +++ b/modules/client/front/fiscal-data/index.spec.js @@ -67,9 +67,8 @@ describe('Client', () => { ] } }; - const expectedClient = {id: 102}; const filter = encodeURIComponent(JSON.stringify(filterObj)); - $httpBackend.expect('GET', `Clients/findOne?filter=${filter}`).respond(expectedClient); + $httpBackend.expect('GET', `Clients/findOne?filter=${filter}`).respond(404); controller.checkExistingClient(); $httpBackend.flush(); }); diff --git a/modules/route/front/tickets/index.html b/modules/route/front/tickets/index.html index 4a0e58112..c4e4f8d7e 100644 --- a/modules/route/front/tickets/index.html +++ b/modules/route/front/tickets/index.html @@ -1,6 +1,6 @@ @@ -23,7 +23,7 @@ - + @@ -86,7 +86,7 @@ diff --git a/modules/route/front/tickets/index.js b/modules/route/front/tickets/index.js index e516a5fa6..f36330235 100644 --- a/modules/route/front/tickets/index.js +++ b/modules/route/front/tickets/index.js @@ -1,26 +1,24 @@ import ngModule from '../module'; +import Section from 'salix/components/section'; import './style.scss'; -class Controller { - constructor($stateParams, $scope, $translate, $http, vnApp, $filter) { - this.$translate = $translate; - this.$stateParams = $stateParams; - this.$ = $scope; - this.$http = $http; - this.vnApp = vnApp; +class Controller extends Section { + constructor($element, $scope, $filter) { + super($element, $scope); + this.$filter = $filter; } + get route() { + return this._route; + } + set route(value) { this._route = value; if (value) this.buildPossibleTicketsFilter(); } - get route() { - return this._route; - } - get isChecked() { if (this.tickets) { for (let instance of this.tickets) @@ -104,7 +102,6 @@ class Controller { }); } - showDeleteConfirm(id) { this.selectedTicket = id; this.$.confirm.show(); @@ -122,7 +119,7 @@ class Controller { } updateVolume() { - let url = `Routes/${this.$stateParams.id}/updateVolume`; + let url = `Routes/${this.$params.id}/updateVolume`; this.$http.post(url).then(() => { this.card.reload(); this.$.model.refresh(); @@ -130,7 +127,7 @@ class Controller { } guessPriority() { - let query = `Routes/${this.$stateParams.id}/guessPriority/`; + let query = `Routes/${this.$params.id}/guessPriority/`; this.$http.get(query).then(() => { this.vnApp.showSuccess(this.$translate.instant('Order changed')); this.$.model.refresh(); @@ -171,9 +168,39 @@ class Controller { } return Promise.resolve(); } + + onDrop($event) { + const ticketId = $event.dataTransfer.getData('Text'); + + if (isNaN(ticketId)) { + const regexp = new RegExp(/\/ticket\/([0-9]+)\//i); + const matches = ticketId.match(regexp); + + if (matches && matches.length) + this.insert(matches[1]); + else + this.vnApp.showError(this.$translate.instant('Ticket not found')); + } + + if (!isNaN(ticketId)) + this.insert(ticketId); + } + + insert(id) { + const params = {routeFk: this.route.id}; + this.$http.patch(`Tickets/${id}`, params).then(() => { + this.vnApp.showSuccess(this.$translate.instant('Data saved!')); + this.$.model.refresh(); + this.card.reload(); + }).catch(error => { + if (error.status == 404) + return this.vnApp.showError(this.$translate.instant('Ticket not found')); + throw error; + }); + } } -Controller.$inject = ['$stateParams', '$scope', '$translate', '$http', 'vnApp', '$filter']; +Controller.$inject = ['$element', '$scope', '$filter']; ngModule.component('vnRouteTickets', { template: require('./index.html'), diff --git a/modules/route/front/tickets/index.spec.js b/modules/route/front/tickets/index.spec.js index d1313dd65..f3c02cf51 100644 --- a/modules/route/front/tickets/index.spec.js +++ b/modules/route/front/tickets/index.spec.js @@ -1,14 +1,20 @@ -import './index.js'; +import './index'; describe('Route', () => { let controller; let $httpBackend; + let $scope; beforeEach(ngModule('route')); - beforeEach(angular.mock.inject(($componentController, _$httpBackend_) => { + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { $httpBackend = _$httpBackend_; - controller = $componentController('vnRouteTickets'); + $scope = $rootScope.$new(); + const $element = angular.element(''); + controller = $componentController('vnRouteTickets', {$element, $scope}); + controller.route = {id: 1}; + controller.$.model = {refresh: () => {}}; + controller.card = {reload: () => {}}; })); describe('route setter/getter', () => { @@ -86,7 +92,6 @@ describe('Route', () => { describe('setPriority()', () => { it('should set a ticket priority', () => { - controller.$.model = {refresh: () => {}}; jest.spyOn(controller.$.model, 'refresh'); jest.spyOn(controller.vnApp, 'showSuccess'); const ticketId = 1; @@ -175,16 +180,14 @@ describe('Route', () => { describe('updateVolume()', () => { it('should perform a POST query then call both reload and refresh methods', () => { - controller.$.model = {refresh: () => {}}; - controller.card = {reload: () => {}}; - controller.$stateParamds = {id: 999}; + controller.$params = {id: 999}; jest.spyOn(controller.$.model, 'refresh'); jest.spyOn(controller.card, 'reload'); let ticketId = 1; controller.selectedTicket = ticketId; - const url = `Routes/${controller.$stateParams.id}/updateVolume`; + const url = `Routes/${controller.$params.id}/updateVolume`; $httpBackend.expectPOST(url).respond('ok'); controller.updateVolume(); $httpBackend.flush(); @@ -196,12 +199,11 @@ describe('Route', () => { describe('guessPriority()', () => { it('should perform a GET query then call both refresh and showSuccess methods', () => { - controller.$.model = {refresh: () => {}}; jest.spyOn(controller.$.model, 'refresh'); jest.spyOn(controller.vnApp, 'showSuccess'); - controller.$stateParams = {id: 99}; + controller.$params = {id: 99}; - const url = `Routes/${controller.$stateParams.id}/guessPriority/`; + const url = `Routes/${controller.$params.id}/guessPriority/`; $httpBackend.expectGET(url).respond('ok'); controller.guessPriority(); $httpBackend.flush(); @@ -288,4 +290,66 @@ describe('Route', () => { expect(controller.setTicketsRoute('cancel')).toEqual(jasmine.any(Promise)); }); }); + + describe('onDrop()', () => { + it('should call the insert method when dragging a ticket number', () => { + jest.spyOn(controller, 'insert'); + + const expectedTicketId = '11'; + const draggedElement = '11'; + const $event = { + dataTransfer: { + getData: () => draggedElement + } + }; + controller.onDrop($event); + + expect(controller.insert).toHaveBeenCalledWith(expectedTicketId); + }); + + it('should call the insert method when dragging a ticket link', () => { + jest.spyOn(controller, 'insert'); + + const expectedTicketId = '11'; + const draggedElement = 'http://arkamcity.com/#!/ticket/11/summary'; + const $event = { + dataTransfer: { + getData: () => draggedElement + } + }; + controller.onDrop($event); + + expect(controller.insert).toHaveBeenCalledWith(expectedTicketId); + }); + + it('should throw an error when dragging an invalid ticket link', () => { + jest.spyOn(controller.vnApp, 'showError'); + + const draggedElement = 'http://arkamcity.com/#!/item/11/summary'; + const $event = { + dataTransfer: { + getData: () => draggedElement + } + }; + controller.onDrop($event); + + expect(controller.vnApp.showError).toHaveBeenCalledWith('Ticket not found'); + }); + }); + + describe('insert()', () => { + it('should make a HTTP patch query and then call both refresh and showSuccess methods', () => { + jest.spyOn(controller.$.model, 'refresh').mockReturnThis(); + jest.spyOn(controller.vnApp, 'showSuccess'); + + const ticketId = 11; + + $httpBackend.expect('PATCH', `Tickets/11`).respond({id: 11}); + controller.insert(ticketId); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); + expect(controller.$.model.refresh).toHaveBeenCalledWith(); + }); + }); }); diff --git a/modules/route/front/tickets/locale/es.yml b/modules/route/front/tickets/locale/es.yml index 02f9eed08..b9892a299 100644 --- a/modules/route/front/tickets/locale/es.yml +++ b/modules/route/front/tickets/locale/es.yml @@ -5,4 +5,5 @@ Order changed: Orden cambiado 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 \ No newline at end of file +Tickets to add: Tickets a añadir +Ticket not found: No se ha encontrado el ticket \ No newline at end of file diff --git a/modules/travel/front/summary/index.spec.js b/modules/travel/front/summary/index.spec.js index 202c66637..9b041f22b 100644 --- a/modules/travel/front/summary/index.spec.js +++ b/modules/travel/front/summary/index.spec.js @@ -26,7 +26,6 @@ describe('component vnTravelSummary', () => { jest.spyOn(controller, 'getThermographs'); controller.travel = {id: 99}; - expect(controller._travel.id).toEqual(99); expect(controller.getTravel).toHaveBeenCalledWith(); expect(controller.getEntries).toHaveBeenCalledWith();