refactor: code
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2022-06-17 08:18:23 +02:00
parent c26b09ead3
commit 1bd4b02a62
3 changed files with 49 additions and 72 deletions

View File

@ -5,9 +5,9 @@ module.exports = Self => {
description: 'Edit the geos excluded from a zone', description: 'Edit the geos excluded from a zone',
accepts: [ accepts: [
{ {
arg: 'exclusionsGeo', arg: 'zoneExclusionFk',
type: 'any', type: 'number',
description: 'The exclusions by geo' description: 'The zoneExclusion id'
}, },
{ {
arg: 'geoIds', 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 models = Self.app.models;
const myOptions = {}; const myOptions = {};
@ -35,31 +35,21 @@ module.exports = Self => {
if (!geoIds[0]) throw new UserError(`You must select a location`); if (!geoIds[0]) throw new UserError(`You must select a location`);
let actualPosition = 0; await models.ZoneExclusionGeo.destroyAll({
let geoId; zoneExclusionFk: zoneExclusionFk
for (let exclusionGeo of exclusionsGeo) { }, myOptions);
if (geoIds[actualPosition]) geoId = geoIds[actualPosition].id;
else geoId = null; const promises = [];
if (geoId && geoId == exclusionGeo.geoFk) {
const params = { for (const geoId of geoIds) {
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) {
const params = { const params = {
zoneExclusionFk: exclusionsGeo[0].zoneExclusionFk, zoneExclusionFk: zoneExclusionFk,
geoFk: geoIds[actualPosition].id geoFk: geoId.id
}; };
await models.ZoneExclusionGeo.create(params, myOptions); const deletedZoneExclusionGeos = models.ZoneExclusionGeo.create(params, myOptions);
actualPosition++; promises.push(deletedZoneExclusionGeos);
} }
return true; return Promise.all(promises);
}; };
}; };

View File

@ -247,6 +247,7 @@
<vn-check <vn-check
ng-model="item.checked" ng-model="item.checked"
ng-click="$event.preventDefault()" ng-click="$event.preventDefault()"
on-change="$ctrl.onItemCheck(item.id, value)"
label="{{::item.name}}"> label="{{::item.name}}">
</vn-check> </vn-check>
</vn-treeview> </vn-treeview>

View File

@ -7,8 +7,6 @@ class Controller extends Section {
super($element, $); super($element, $);
this.vnWeekDays = vnWeekDays; this.vnWeekDays = vnWeekDays;
this.editMode = 'exclude'; this.editMode = 'exclude';
this.exclusions = [];
this.exclusionsGeo = [];
} }
$onInit() { $onInit() {
@ -61,36 +59,28 @@ class Controller extends Section {
: this.$t('Everyday'); : this.$t('Everyday');
} }
onSelection(days, type, weekday, events, exclusions, exclusionsGeo) { onSelection(days, type, weekday, events, exclusions, exclusionGeos) {
if (this.editMode == 'include') { if (this.editMode == 'include') {
if (events.length) if (events.length)
return this.editInclusion(events[0]); return this.editInclusion(events[0]);
return this.createInclusion(type, days, weekday); return this.createInclusion(type, days, weekday);
} } else if (this.editMode == 'exclude') {
if (exclusions.length || exclusionGeos.length)
if (this.editMode == 'exclude') { return this.editExclusion(exclusions[0] || {}, exclusionGeos);
if (exclusionsGeo.length) {
this.exclusionsGeo = exclusionsGeo;
return this.editExclusion();
}
if (exclusions.length) {
this.exclusions = exclusions;
return this.editExclusion();
}
return this.createExclusion(days); return this.createExclusion(days);
} }
} }
editExclusion() { editExclusion(exclusion, exclusionGeos) {
this.isNew = false; this.isNew = false;
this.excludeSelected = angular.copy(exclusion);
this.excludeSelected.type = exclusionGeos.length ?
'specificLocations' : 'all';
if (this.exclusionsGeo.length) { this.exclusionGeos = new Set();
this.excludeSelected = angular.copy(this.exclusionsGeo[0]); if (exclusionGeos.length) {
this.excludeSelected.type = 'specificLocations'; this.excludeSelected.id = exclusionGeos[0].zoneExclusionFk;
} exclusionGeos.forEach(x => this.exclusionGeos.add(x.geoFk));
if (this.exclusions.length) {
this.excludeSelected = angular.copy(this.exclusions[0]);
this.excludeSelected.type = 'all';
} }
this.$.excludeDialog.show(); this.$.excludeDialog.show();
@ -102,7 +92,7 @@ class Controller extends Section {
type: 'all', type: 'all',
dated: days[0] dated: days[0]
}; };
this.exclusionsGeo = []; this.exclusionGeos = new Set();
this.$.excludeDialog.show(); this.$.excludeDialog.show();
} }
@ -187,7 +177,7 @@ class Controller extends Section {
case 'delete': case 'delete':
if (type == 'all') if (type == 'all')
return this.exclusionDelete(this.exclusions); 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; const excludeSelected = this.excludeSelected;
let req; let req;
const geoIds = []; const geoIds = [];
this.exclusionGeos.forEach(id => geoIds.push({id}));
for (let zoneGeo of this.checked) {
geoIds.push({
id: zoneGeo.id
});
}
if (this.isNew) { if (this.isNew) {
const params = { const params = {
zoneFk: parseInt(this.$params.id), zoneFk: parseInt(this.$params.id),
date: excludeSelected.dated, date: excludeSelected.dated,
geoIds: geoIds geoIds
}; };
req = this.$http.post(`Zones/exclusionGeo`, params); req = this.$http.post(`Zones/exclusionGeo`, params);
} else { } else {
const params = { const params = {
exclusionsGeo: this.exclusionsGeo, zoneExclusionFk: this.excludeSelected.id,
geoIds: geoIds geoIds
}; };
req = this.$http.post(`Zones/editExclusionGeo`, params); req = this.$http.post(`Zones/editExclusionGeo`, params);
} }
return req.then(() => { return req.then(() => this.refresh());
this.refresh();
});
} }
exclusionDelete(exclusions) { exclusionDelete(exclusions) {
@ -287,7 +270,7 @@ class Controller extends Section {
this.excludeSelected = null; this.excludeSelected = null;
this.isNew = null; this.isNew = null;
this.exclusions = []; this.exclusions = [];
this.exclusionsGeo = []; this.exclusionGeos = [];
} }
set excludeSearch(value) { 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) { onSort(a, b) {
if (b.selected !== a.selected) { if (b.selected !== a.selected) {
if (a.selected == null) if (a.selected == null)
@ -347,6 +321,18 @@ class Controller extends Section {
return a.name.localeCompare(b.name); 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) { exprBuilder(param, value) {
switch (param) { switch (param) {
case 'search': case 'search':