From da1e1889831642ea6cb88abec8b638c673bb8013 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 27 May 2022 13:54:35 +0200 Subject: [PATCH] fix: put requests --- .../zone/back/models/zone-exclusion-geo.json | 1 + modules/zone/front/calendar/index.spec.js | 4 +- modules/zone/front/events/index.js | 120 +++++++++++------- modules/zone/front/events/index.spec.js | 100 +++++++++++---- 4 files changed, 155 insertions(+), 70 deletions(-) diff --git a/modules/zone/back/models/zone-exclusion-geo.json b/modules/zone/back/models/zone-exclusion-geo.json index 7c77c5458..fba2378db 100644 --- a/modules/zone/back/models/zone-exclusion-geo.json +++ b/modules/zone/back/models/zone-exclusion-geo.json @@ -8,6 +8,7 @@ }, "properties": { "zoneExclusionFk": { + "id": true, "type": "number", "required": true }, diff --git a/modules/zone/front/calendar/index.spec.js b/modules/zone/front/calendar/index.spec.js index be002925e..990ecd32e 100644 --- a/modules/zone/front/calendar/index.spec.js +++ b/modules/zone/front/calendar/index.spec.js @@ -15,6 +15,7 @@ describe('component vnZoneCalendar', () => { controller.zone = {id: 1}; controller.days = []; controller.exclusions = []; + controller.geoExclusions = []; })); describe('date() setter', () => { @@ -122,7 +123,8 @@ describe('component vnZoneCalendar', () => { $events: [], $exclusions: [], $type: 'day', - $weekday: 1 + $weekday: 1, + $geoExclusions: [], } ); }); diff --git a/modules/zone/front/events/index.js b/modules/zone/front/events/index.js index 3007ea510..447aa1da8 100644 --- a/modules/zone/front/events/index.js +++ b/modules/zone/front/events/index.js @@ -6,9 +6,9 @@ class Controller extends Section { constructor($element, $, vnWeekDays) { super($element, $); this.vnWeekDays = vnWeekDays; - this.editMode = 'include'; + this.editMode = 'exclude'; this.exclusions; - this.days; + this.geoExclusions; } $onInit() { @@ -23,6 +23,10 @@ class Controller extends Section { return `Zones/${this.$params.id}/exclusions`; } + get geoExclusionsPath() { + return `Zones/exclusionGeo`; + } + get checked() { const geos = this.$.model.data || []; const checkedLines = []; @@ -64,57 +68,57 @@ class Controller extends Section { onSelection(days, type, weekday, events, exclusions, geoExclusions) { if (this.editMode == 'include') { if (events.length) - this.edit(events[0]); + this.editInclusion(events[0]); else - this.create(type, days, weekday); + this.createInclusion(type, days, weekday); } else { if (geoExclusions.length) this.editExclusion(geoExclusions); else if (exclusions.length) this.editExclusion(exclusions); else - this.createExclusion(exclusions, days); + this.createExclusion(days); } } - editExclusion(exclusions) { + editExclusion(allTypeExclusions) { this.isNew = false; - this.excludeSelected = angular.copy(exclusions[0]); - this.exclusions = exclusions; + this.excludeSelected = angular.copy(allTypeExclusions[0]); - if (this.excludeSelected.geoFk) + if (this.excludeSelected.geoFk) { this.excludeSelected.type = 'specificLocations'; - else + this.geoExclusions = allTypeExclusions; + } else { this.excludeSelected.type = 'all'; + this.exclusions = allTypeExclusions; + } this.$.excludeDialog.show(); } - createExclusion(exclusions, days) { + createExclusion(days) { this.isNew = true; this.excludeSelected = { type: 'all', dated: days[0] }; - this.exclusions = exclusions; - this.days = days; - + this.geoExclusions = []; this.$.excludeDialog.show(); } onEditClick(row, event) { if (event.defaultPrevented) return; - this.edit(row); + this.editInclusion(row); } - edit(row) { + editInclusion(row) { this.isNew = false; this.selected = angular.copy(row); this.selected.wdays = this.vnWeekDays.fromSet(row.weekDays); this.$.includeDialog.show(); } - create(type, days, weekday) { + createInclusion(type, days, weekday) { this.isNew = true; if (type == 'weekday') { @@ -173,31 +177,19 @@ class Controller extends Section { } onExcludeResponse(response) { + const type = this.excludeSelected.type; switch (response) { case 'accept': { - let excludeSelected = this.excludeSelected; - let type = excludeSelected.type; if (type == 'all') - return this.exclusionCreate(this.days); - else { - const geoIds = []; - for (let zoneGeo of this.checked) { - geoIds.push({ - id: zoneGeo.id - }); - } - - const params = { - zoneFk: parseInt(this.$params.id), - date: this.days[0], - geoIds: geoIds - }; - return this.$http.post(`Zones/exclusionGeo`, params) - .then(() => this.refresh()); - } + return this.exclusionCreate(); + else + return this.exclusionGeoCreate(); } case 'delete': - return this.exclusionDelete(this.exclusions); + if (type == 'all') + return this.exclusionDelete(this.exclusions); + if (type == 'specificLocations') + return this.exclusionDelete(this.geoExclusions); } } @@ -218,13 +210,51 @@ class Controller extends Section { .then(() => this.refresh()); } - exclusionCreate(days) { - let exclusions = days.map(dated => { - return {dated}; - }); + exclusionCreate() { + const excludeSelected = this.excludeSelected; + const dated = excludeSelected.dated; + let req; - this.$http.post(this.exclusionsPath, exclusions) - .then(() => this.refresh()); + if (this.isNew) + req = this.$http.post(this.exclusionsPath, [{dated}]); + else + req = this.$http.put(`${this.exclusionsPath}/${excludeSelected.id}`, {dated}); + + return req.then(() => { + this.excludeSelected = null; + this.isNew = null; + this.refresh(); + }); + } + + exclusionGeoCreate() { + const excludeSelected = this.excludeSelected; + let req; + const geoIds = []; + for (let zoneGeo of this.checked) { + geoIds.push({ + id: zoneGeo.id + }); + } + const params = { + zoneFk: parseInt(this.$params.id), + date: excludeSelected.dated, + geoIds: geoIds + }; + + console.log(excludeSelected); + if (this.isNew) + req = this.$http.post(this.geoExclusionsPath, params); + else + req = this.$http.put(`${this.geoExclusionsPath}/${excludeSelected.zoneExclusionFk}`, params); + + return req.then(() => { + this.excludeSelected = null; + this.isNew = null; + this.refresh(); + }); + // return this.$http.post(`Zones/exclusionGeo`, params) + // .then(() => this.refresh()); } exclusionDelete(exclusions) { @@ -263,8 +293,8 @@ class Controller extends Section { const data = this.$.model.data; for (let geo of data) { - for (let exclusion of this.exclusions) { - if (geo.id == exclusion.geoFk) + for (let geoExclusion of this.geoExclusions) { + if (geo.id == geoExclusion.geoFk) geo.checked = true; } } diff --git a/modules/zone/front/events/index.spec.js b/modules/zone/front/events/index.spec.js index ed2c91c31..2a830fa63 100644 --- a/modules/zone/front/events/index.spec.js +++ b/modules/zone/front/events/index.spec.js @@ -34,7 +34,8 @@ describe('component vnZoneEvents', () => { const query = `Zones/getEventsFiltered?ended=${date}&started=${date}&zoneFk=${params.zoneFk}`; const response = { events: 'myEvents', - exclusions: 'myExclusions' + exclusions: 'myExclusions', + geoExclusions: 'myGeoExclusions', }; $httpBackend.whenGET(query).respond(response); controller.refresh(); @@ -76,38 +77,50 @@ describe('component vnZoneEvents', () => { expect(controller.create).toHaveBeenCalledWith(type, days, weekday); }); - it('should call the exclusionDelete() method', () => { - jest.spyOn(controller, 'exclusionDelete').mockReturnThis(); + // it('should call the exclusionDelete() method', () => { + // jest.spyOn(controller, 'exclusionDelete').mockReturnThis(); - const weekday = {}; - const days = []; - const type = 'EventType'; - const events = []; - const exclusions = [{id: 1}]; - controller.editMode = 'delete'; - controller.onSelection(days, type, weekday, events, exclusions); + // const weekday = {}; + // const days = []; + // const type = 'EventType'; + // const events = []; + // const exclusions = [{id: 1}]; + // const geoExclusions = []; + // controller.editMode = 'delete'; + // controller.onSelection(days, type, weekday, events, exclusions, geoExclusions); - expect(controller.exclusionDelete).toHaveBeenCalledWith(exclusions); - }); + // expect(controller.exclusionDelete).toHaveBeenCalledWith(exclusions); + // }); it('should call the exclusionCreate() method', () => { jest.spyOn(controller, 'exclusionCreate').mockReturnThis(); - const weekday = {}; const days = [{dated: new Date()}]; - const type = 'EventType'; - const events = []; - const exclusions = []; - controller.editMode = 'delete'; - controller.onSelection(days, type, weekday, events, exclusions); + controller.days = days; + controller.excludeSelected = {type: 'all'}; + controller.onExcludeResponse('accept'); expect(controller.exclusionCreate).toHaveBeenCalledWith(days); }); + + // it('should call the exclusionCreate() method', () => { + // jest.spyOn(controller, 'exclusionCreate').mockReturnThis(); + + // const weekday = {}; + // const days = [{dated: new Date()}]; + // const type = 'EventType'; + // const events = []; + // const exclusions = []; + // controller.editMode = 'delete'; + // controller.onSelection(days, type, weekday, events, exclusions); + + // expect(controller.exclusionCreate).toHaveBeenCalledWith(days); + // }); }); describe('create()', () => { - it('shoud set the selected property and then call the dialog show() method', () => { - controller.$.dialog = {show: jest.fn()}; + it('shoud set the selected property and then call the includeDialog show() method', () => { + controller.$.includeDialog = {show: jest.fn()}; const type = 'weekday'; const days = [new Date()]; @@ -120,11 +133,11 @@ describe('component vnZoneEvents', () => { expect(selection.type).toEqual('indefinitely'); expect(firstWeekday).toBeTruthy(); expect(controller.isNew).toBeTruthy(); - expect(controller.$.dialog.show).toHaveBeenCalledWith(); + expect(controller.$.includeDialog.show).toHaveBeenCalledWith(); }); - it('shoud set the selected property with the first day and then call the dialog show() method', () => { - controller.$.dialog = {show: jest.fn()}; + it('shoud set the selected property with the first day and then call the includeDialog show() method', () => { + controller.$.includeDialog = {show: jest.fn()}; const type = 'nonListedType'; const days = [new Date()]; @@ -136,7 +149,7 @@ describe('component vnZoneEvents', () => { expect(selection.type).toEqual('day'); expect(selection.dated).toEqual(days[0]); expect(controller.isNew).toBeTruthy(); - expect(controller.$.dialog.show).toHaveBeenCalledWith(); + expect(controller.$.includeDialog.show).toHaveBeenCalledWith(); }); }); @@ -180,6 +193,45 @@ describe('component vnZoneEvents', () => { }); }); + describe('onExcludeResponse()', () => { + it('should call the exclusionDelete() method', () => { + jest.spyOn(controller, 'exclusionDelete').mockReturnThis(); + + const exclusions = [{id: 1}]; + controller.exclusions = exclusions; + controller.onExcludeResponse('delete'); + + expect(controller.exclusionDelete).toHaveBeenCalledWith(exclusions); + }); + + it('shoud make an HTTP POST query to create a new one and then call the refresh() method', () => { + jest.spyOn(controller, 'refresh').mockReturnThis(); + + controller.selected = {id: 1}; + controller.isNew = true; + + $httpBackend.when('POST', `Zones/1/events`).respond(200); + controller.onExcludeResponse('accept'); + $httpBackend.flush(); + + expect(controller.refresh).toHaveBeenCalledWith(); + }); + + it('shoud make an HTTP PUT query and then call the refresh() method', () => { + jest.spyOn(controller, 'refresh').mockReturnThis(); + + controller.selected = {id: 1}; + controller.isNew = false; + + const eventId = 1; + $httpBackend.when('PUT', `Zones/1/events/${eventId}`).respond(200); + controller.onExcludeResponse('accept'); + $httpBackend.flush(); + + expect(controller.refresh).toHaveBeenCalledWith(); + }); + }); + describe('onDeleteResponse()', () => { it('shoud make an HTTP DELETE query and then call the refresh() method', () => { jest.spyOn(controller, 'refresh').mockReturnThis();