diff --git a/modules/zone/back/methods/zone/editExclusionGeo.js b/modules/zone/back/methods/zone/editExclusionGeo.js index c9d98b5f6..f3d58c9ed 100644 --- a/modules/zone/back/methods/zone/editExclusionGeo.js +++ b/modules/zone/back/methods/zone/editExclusionGeo.js @@ -5,9 +5,9 @@ module.exports = Self => { description: 'Edit the geos excluded from a zone', accepts: [ { - arg: 'exclusionsGeo', - type: 'any', - description: 'The exclusions by geo' + arg: 'zoneExclusionFk', + type: 'number', + description: 'The zoneExclusion id' }, { arg: 'geoIds', @@ -26,7 +26,7 @@ module.exports = Self => { } }); - Self.editExclusionGeo = async(exclusionsGeo, geoIds, options) => { + Self.editExclusionGeo = async(zoneExclusionFk, geoIds, options) => { const models = Self.app.models; const myOptions = {}; @@ -35,31 +35,21 @@ module.exports = Self => { if (!geoIds[0]) throw new UserError(`You must select a location`); - let actualPosition = 0; - let geoId; - for (let exclusionGeo of exclusionsGeo) { - if (geoIds[actualPosition]) geoId = geoIds[actualPosition].id; - else geoId = null; - if (geoId && geoId == exclusionGeo.geoFk) { - const params = { - id: exclusionGeo.id, - zoneExclusionFk: exclusionGeo.zoneExclusionFk, - geoFk: geoId - }; - await models.ZoneExclusionGeo.upsert(params, myOptions); - actualPosition++; - } else - await models.ZoneExclusionGeo.destroyById(exclusionGeo.id, myOptions); - } - while (actualPosition <= geoIds.length - 1) { + await models.ZoneExclusionGeo.destroyAll({ + zoneExclusionFk: zoneExclusionFk + }, myOptions); + + const promises = []; + + for (const geoId of geoIds) { const params = { - zoneExclusionFk: exclusionsGeo[0].zoneExclusionFk, - geoFk: geoIds[actualPosition].id + zoneExclusionFk: zoneExclusionFk, + geoFk: geoId.id }; - await models.ZoneExclusionGeo.create(params, myOptions); - actualPosition++; + const deletedZoneExclusionGeos = models.ZoneExclusionGeo.create(params, myOptions); + promises.push(deletedZoneExclusionGeos); } - return true; + return Promise.all(promises); }; }; diff --git a/modules/zone/front/events/index.html b/modules/zone/front/events/index.html index c42e05e8d..46ba87dea 100644 --- a/modules/zone/front/events/index.html +++ b/modules/zone/front/events/index.html @@ -247,6 +247,7 @@ diff --git a/modules/zone/front/events/index.js b/modules/zone/front/events/index.js index 3ab035fe8..b22568e50 100644 --- a/modules/zone/front/events/index.js +++ b/modules/zone/front/events/index.js @@ -7,8 +7,6 @@ class Controller extends Section { super($element, $); this.vnWeekDays = vnWeekDays; this.editMode = 'exclude'; - this.exclusions = []; - this.exclusionsGeo = []; } $onInit() { @@ -61,36 +59,28 @@ class Controller extends Section { : this.$t('Everyday'); } - onSelection(days, type, weekday, events, exclusions, exclusionsGeo) { + onSelection(days, type, weekday, events, exclusions, exclusionGeos) { if (this.editMode == 'include') { if (events.length) return this.editInclusion(events[0]); return this.createInclusion(type, days, weekday); - } - - if (this.editMode == 'exclude') { - if (exclusionsGeo.length) { - this.exclusionsGeo = exclusionsGeo; - return this.editExclusion(); - } - if (exclusions.length) { - this.exclusions = exclusions; - return this.editExclusion(); - } + } else if (this.editMode == 'exclude') { + if (exclusions.length || exclusionGeos.length) + return this.editExclusion(exclusions[0] || {}, exclusionGeos); return this.createExclusion(days); } } - editExclusion() { + editExclusion(exclusion, exclusionGeos) { this.isNew = false; + this.excludeSelected = angular.copy(exclusion); + this.excludeSelected.type = exclusionGeos.length ? + 'specificLocations' : 'all'; - if (this.exclusionsGeo.length) { - this.excludeSelected = angular.copy(this.exclusionsGeo[0]); - this.excludeSelected.type = 'specificLocations'; - } - if (this.exclusions.length) { - this.excludeSelected = angular.copy(this.exclusions[0]); - this.excludeSelected.type = 'all'; + this.exclusionGeos = new Set(); + if (exclusionGeos.length) { + this.excludeSelected.id = exclusionGeos[0].zoneExclusionFk; + exclusionGeos.forEach(x => this.exclusionGeos.add(x.geoFk)); } this.$.excludeDialog.show(); @@ -102,7 +92,7 @@ class Controller extends Section { type: 'all', dated: days[0] }; - this.exclusionsGeo = []; + this.exclusionGeos = new Set(); this.$.excludeDialog.show(); } @@ -187,7 +177,7 @@ class Controller extends Section { case 'delete': if (type == 'all') return this.exclusionDelete(this.exclusions); - return this.exclusionGeoDelete(this.exclusionsGeo); + return this.exclusionGeoDelete(this.exclusionGeos); } } @@ -227,30 +217,23 @@ class Controller extends Section { const excludeSelected = this.excludeSelected; let req; const geoIds = []; - - for (let zoneGeo of this.checked) { - geoIds.push({ - id: zoneGeo.id - }); - } + this.exclusionGeos.forEach(id => geoIds.push({id})); if (this.isNew) { const params = { zoneFk: parseInt(this.$params.id), date: excludeSelected.dated, - geoIds: geoIds + geoIds }; req = this.$http.post(`Zones/exclusionGeo`, params); } else { const params = { - exclusionsGeo: this.exclusionsGeo, - geoIds: geoIds + zoneExclusionFk: this.excludeSelected.id, + geoIds }; req = this.$http.post(`Zones/editExclusionGeo`, params); } - return req.then(() => { - this.refresh(); - }); + return req.then(() => this.refresh()); } exclusionDelete(exclusions) { @@ -287,7 +270,7 @@ class Controller extends Section { this.excludeSelected = null; this.isNew = null; this.exclusions = []; - this.exclusionsGeo = []; + this.exclusionGeos = []; } set excludeSearch(value) { @@ -326,15 +309,6 @@ class Controller extends Section { }); } - getChecked(data) { - for (let geo of data) { - for (let exclusionGeo of this.exclusionsGeo) { - if (geo.id == exclusionGeo.geoFk) - geo.checked = true; - } - } - } - onSort(a, b) { if (b.selected !== a.selected) { if (a.selected == null) @@ -347,6 +321,18 @@ class Controller extends Section { return a.name.localeCompare(b.name); } + getChecked(data) { + for (let geo of data) + geo.checked = this.exclusionGeos.has(geo.id); + } + + onItemCheck(geoId, checked) { + if (checked) + this.exclusionGeos.add(geoId); + else + this.exclusionGeos.delete(geoId); + } + exprBuilder(param, value) { switch (param) { case 'search':