From b5fa360960b9a1f0508b96456de2b6d4fc3aa579 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 2 Jun 2022 10:40:02 +0200 Subject: [PATCH] feat: edit excluded selecetion with specificLocations --- .../zone/back/methods/zone/exclusionGeo.js | 2 +- .../back/methods/zone/getEventsFiltered.js | 4 +- .../zone/back/models/zone-exclusion-geo.json | 11 +-- modules/zone/front/events/index.js | 73 ++++++++++++++----- 4 files changed, 64 insertions(+), 26 deletions(-) diff --git a/modules/zone/back/methods/zone/exclusionGeo.js b/modules/zone/back/methods/zone/exclusionGeo.js index 0bd6075b9..aa1e4ad9e 100644 --- a/modules/zone/back/methods/zone/exclusionGeo.js +++ b/modules/zone/back/methods/zone/exclusionGeo.js @@ -30,7 +30,7 @@ module.exports = Self => { } }); - Self.exclusionGeo = async(zoneFk, date, geoIds, options) => { + Self.exclusionGeo = async(zoneFk, date, geoIds) => { const models = Self.app.models; const newZoneExclusion = await models.ZoneExclusion.create({ diff --git a/modules/zone/back/methods/zone/getEventsFiltered.js b/modules/zone/back/methods/zone/getEventsFiltered.js index b97e49b1c..0d0cf9376 100644 --- a/modules/zone/back/methods/zone/getEventsFiltered.js +++ b/modules/zone/back/methods/zone/getEventsFiltered.js @@ -52,7 +52,7 @@ module.exports = Self => { const events = await Self.rawSql(query, [zoneFk, started, ended, started, ended, started, ended], myOptions); query = ` - SELECT * + SELECT e.* FROM vn.zoneExclusion e LEFT JOIN vn.zoneExclusionGeo eg ON eg.zoneExclusionFk = e.id WHERE zoneFk = ? @@ -61,7 +61,7 @@ module.exports = Self => { const exclusions = await Self.rawSql(query, [zoneFk, started, ended], myOptions); query = ` - SELECT * + SELECT eg.*, e.zoneFk, e.dated, e.created, e.userFk FROM vn.zoneExclusion e LEFT JOIN vn.zoneExclusionGeo eg ON eg.zoneExclusionFk = e.id WHERE zoneFk = ? diff --git a/modules/zone/back/models/zone-exclusion-geo.json b/modules/zone/back/models/zone-exclusion-geo.json index fba2378db..816e4b650 100644 --- a/modules/zone/back/models/zone-exclusion-geo.json +++ b/modules/zone/back/models/zone-exclusion-geo.json @@ -7,14 +7,15 @@ } }, "properties": { - "zoneExclusionFk": { + "id": { "id": true, - "type": "number", - "required": true + "type": "number" + }, + "zoneExclusionFk": { + "type": "number" }, "geoFk": { - "type": "number", - "required": true + "type": "number" } } } \ No newline at end of file diff --git a/modules/zone/front/events/index.js b/modules/zone/front/events/index.js index 56b6a82e0..115130f73 100644 --- a/modules/zone/front/events/index.js +++ b/modules/zone/front/events/index.js @@ -8,7 +8,7 @@ class Controller extends Section { this.vnWeekDays = vnWeekDays; this.editMode = 'exclude'; this.exclusions; - this.geoExclusions; + this.exclusionsGeo; } $onInit() { @@ -23,7 +23,7 @@ class Controller extends Section { return `Zones/${this.$params.id}/exclusions`; } - get geoExclusionsPath() { + get exclusionsGeoPath() { return `Zones/exclusionGeo`; } @@ -65,15 +65,15 @@ class Controller extends Section { : this.$t('Everyday'); } - onSelection(days, type, weekday, events, exclusions, geoExclusions) { + onSelection(days, type, weekday, events, exclusions, exclusionsGeo) { if (this.editMode == 'include') { if (events.length) this.editInclusion(events[0]); else this.createInclusion(type, days, weekday); } else { - if (geoExclusions.length) { - this.geoExclusions = geoExclusions; + if (exclusionsGeo.length) { + this.exclusionsGeo = exclusionsGeo; this.editExclusion(); } else if (exclusions.length) { this.exclusions = exclusions; @@ -85,8 +85,8 @@ class Controller extends Section { editExclusion() { this.isNew = false; - if (this.geoExclusions && this.geoExclusions.length) { - this.excludeSelected = angular.copy(this.geoExclusions[0]); + if (this.exclusionsGeo && this.exclusionsGeo.length) { + this.excludeSelected = angular.copy(this.exclusionsGeo[0]); this.excludeSelected.type = 'specificLocations'; } if (this.exclusions && this.exclusions.length) { @@ -103,7 +103,7 @@ class Controller extends Section { type: 'all', dated: days[0] }; - this.geoExclusions = []; + this.exclusionsGeo = []; this.$.excludeDialog.show(); } @@ -190,7 +190,7 @@ class Controller extends Section { if (type == 'all') return this.exclusionDelete(this.exclusions); if (type == 'specificLocations') - return this.exclusionDelete(this.geoExclusions); + return this.exclusionGeoDelete(this.exclusionsGeo); } } @@ -240,21 +240,42 @@ class Controller extends Section { const params = { zoneFk: parseInt(this.$params.id), date: excludeSelected.dated, - geoIds: geoIds + geoFk: geoIds }; if (this.isNew) - req = this.$http.post(this.geoExclusionsPath, params); - else - req = this.$http.put(`${this.geoExclusionsPath}/${excludeSelected.zoneExclusionFk}`, params); + req = this.$http.post(this.exclusionsGeoPath, params); + else { + let actualPosition = 0; + let geoId; + for (let exclusionGeo of this.exclusionsGeo) { + if (geoIds[actualPosition]) geoId = geoIds[actualPosition].id; + else geoId = null; + if (geoId) { + const params = { + zoneExclusionFk: excludeSelected.zoneExclusionFk, + geoFk: geoId + }; + req = this.$http.put(`ZoneExclusionGeos/${exclusionGeo.id}`, params); + actualPosition++; + } else + req = this.$http.delete(`ZoneExclusionGeos/${exclusionGeo.id}`); + } + while (actualPosition <= geoIds.length - 1) { + const params = { + zoneExclusionFk: excludeSelected.zoneExclusionFk, + geoFk: geoIds[actualPosition].id + }; + req = this.$http.post(`ZoneExclusionGeos`, params); + actualPosition++; + } + } return req.then(() => { this.excludeSelected = null; this.isNew = null; this.refresh(); }); - // return this.$http.post(`Zones/exclusionGeo`, params) - // .then(() => this.refresh()); } exclusionDelete(exclusions) { @@ -269,12 +290,28 @@ class Controller extends Section { this.$q.all(reqs) .then(() => { this.excludeSelected = null; - this.geoExclusions = null; this.exclusions = null; this.refresh(); }); } + exclusionGeoDelete(exclusionsGeo) { + let reqs = []; + + for (let exclusionGeo of exclusionsGeo) { + if (!exclusionGeo.id) continue; + let path = `${this.exclusionsPath}/${exclusionGeo.zoneExclusionFk}`; + reqs.push(this.$http.delete(path)); + } + + this.$q.all(reqs) + .then(() => { + this.excludeSelected = null; + this.exclusionsGeo = null; + this.refresh(); + }); + } + set excludeSearch(value) { this._excludeSearch = value; if (!value) this.onSearch(); @@ -298,8 +335,8 @@ class Controller extends Section { const data = this.$.model.data; for (let geo of data) { - for (let geoExclusion of this.geoExclusions) { - if (geo.id == geoExclusion.geoFk) + for (let exclusionGeo of this.exclusionsGeo) { + if (geo.id == exclusionGeo.geoFk) geo.checked = true; } }