From 76cdd6e479a0b167db5ce00e1f4ae64fcf35246c Mon Sep 17 00:00:00 2001 From: Findeton Date: Fri, 30 Dec 2022 15:42:07 +0100 Subject: [PATCH 1/8] wip --- avAdmin/admin-directives/create/create.js | 87 ++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index 57fc0c3b..2dd99a2d 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -1310,7 +1310,13 @@ angular.module('avAdmin') .result.then( function (data) { - scope.elections = angular.fromJson(data.electionJson); + var elections = angular.fromJson(data.electionJson); + return fillInElectionIds(elections); + } + ).then( + function (data) + { + scope.elections = data; scope.errors = []; CheckerService({ @@ -1327,6 +1333,85 @@ angular.module('avAdmin') ); }; + /** + * If the elections are using negative numbers, find the existing election + * with the highest election id and replace the negative numbers with + * higher election ids. + */ + function fillInElectionIds(elections) { + var deferred = $q.defer(); + var promise = deferred.promise; + + // check if there are negative election ids + var hasNegativeIds = undefined !== elections.find(function (el) { + return _.isNumber(el.id) && el.id <= 0; + }); + + if (data.length > 1 || hasNegativeIds) { + + // Find highest election id + Authmethod.highestEvent() + .then( + function onSuccess(response) { + var highestId = response.data.highest_id; + var newIdsMap = {}; + + // map negative ids to new election ids + for (var eid = 0; eid < elections.length; eid++) { + var election = elections[eid]; + if (_.isNumber(election.id) && election.id > 0) { + newIdsMap[election.id] = election.id; + } else { + newIdsMap[election.id] = highestId + eid + 1; + } + } + + // replace election ids with new ids + for (var eid = 0; eid < elections.length; eid++) { + var election = elections[eid]; + // replace the election id + election.id = newIdsMap[election.id]; + + // replace ids for virtualSubelections + if (election.virtualSubelections) { + election.virtualSubelections = election.virtualSubelections.map(function (e) { + return newIdsMap[e] || e; + }); + } + + // replace ids in the children elections structure + if (election.children_election_info && + election.children_election_info.natural_order) { + election.children_election_info.natural_order = election.children_election_info.natural_order.map(function (e) { + return newIdsMap[e] || e; + }); + } + if (election.children_election_info && + election.children_election_info.presentation && + election.children_election_info.presentation.categories) { + election.children_election_info.presentation.categories = + election.children_election_info.presentation.categories.map(function (category) { + if (category.events) { + category.events = category.events.map(function (event) { + if (event.event_id) { + event.event_id = newIdsMap[event.event_id] || event.event_id; + } + }); + } + return category; + }); + } + } + deferred.resolve(elections); + }, + deferred.reject + ); + } else { + deferred.resolve(elections); + } + return promise; + } + function createElections() { var deferred = $q.defer(); addElection(0); From 272268ae71fc3e59681a888d593c4d2d1ef69a8b Mon Sep 17 00:00:00 2001 From: Findeton Date: Fri, 30 Dec 2022 15:53:58 +0100 Subject: [PATCH 2/8] wip --- avAdmin/admin-directives/create/create.js | 140 +++++++++++----------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index 2dd99a2d..07482517 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -1263,76 +1263,6 @@ angular.module('avAdmin') deferred.resolve(scope.elections[electionIndex]); } - function addElection(electionIndex) - { - var deferred = $q.defer(); - - // After creating the auth events and registering all the elections - // in ballot_box, we proceed to: - // - set children election info - // - add census - // - create election public keys - if (electionIndex === scope.elections.length) { - secondElectionsStage(0); - return; - } - - var promise = deferred.promise; - promise = promise - .then(createAuthEvent) - .then(registerElection) - .then(function(election) { - addElection(electionIndex + 1); - }) - .catch(function(error) { - scope.creating = false; - scope.creating_text = ''; - logError(angular.toJson(error)); - }); - deferred.resolve(scope.elections[electionIndex]); - } - - scope.editJson = function() - { - if(!ConfigService.allowEditElectionJson) { - return; - } - // show the initial edit dialog - $modal - .open({ - templateUrl: "avAdmin/admin-directives/create/edit-election-json-modal.html", - controller: "EditElectionJsonModal", - size: 'lg', - resolve: { - electionJson: function () { return angular.toJson(scope.elections, true); } - } - }) - .result.then( - function (data) - { - var elections = angular.fromJson(data.electionJson); - return fillInElectionIds(elections); - } - ).then( - function (data) - { - scope.elections = data; - - scope.errors = []; - CheckerService({ - checks: checks, - data: scope.elections, - onError: function (errorKey, errorData) { - scope.errors.push({ - data: $sanitize(errorData), - key: errorKey - }); - } - }); - } - ); - }; - /** * If the elections are using negative numbers, find the existing election * with the highest election id and replace the negative numbers with @@ -1412,6 +1342,76 @@ angular.module('avAdmin') return promise; } + function addElection(electionIndex) + { + var deferred = $q.defer(); + + // After creating the auth events and registering all the elections + // in ballot_box, we proceed to: + // - set children election info + // - add census + // - create election public keys + if (electionIndex === scope.elections.length) { + secondElectionsStage(0); + return; + } + + var promise = deferred.promise; + promise = promise + .then(createAuthEvent) + .then(registerElection) + .then(function(election) { + addElection(electionIndex + 1); + }) + .catch(function(error) { + scope.creating = false; + scope.creating_text = ''; + logError(angular.toJson(error)); + }); + deferred.resolve(scope.elections[electionIndex]); + } + + scope.editJson = function() + { + if(!ConfigService.allowEditElectionJson) { + return; + } + // show the initial edit dialog + $modal + .open({ + templateUrl: "avAdmin/admin-directives/create/edit-election-json-modal.html", + controller: "EditElectionJsonModal", + size: 'lg', + resolve: { + electionJson: function () { return angular.toJson(scope.elections, true); } + } + }) + .result.then( + function (data) + { + var elections = angular.fromJson(data.electionJson); + return fillInElectionIds(elections); + } + ).then( + function (data) + { + scope.elections = data; + + scope.errors = []; + CheckerService({ + checks: checks, + data: scope.elections, + onError: function (errorKey, errorData) { + scope.errors.push({ + data: $sanitize(errorData), + key: errorKey + }); + } + }); + } + ); + }; + function createElections() { var deferred = $q.defer(); addElection(0); From f300ff5d9c6045d0430cba22ec087aa5bb794ffa Mon Sep 17 00:00:00 2001 From: Findeton Date: Fri, 30 Dec 2022 16:01:51 +0100 Subject: [PATCH 3/8] wip --- avAdmin/admin-directives/create/create.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index 07482517..09a4bc89 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -1277,7 +1277,7 @@ angular.module('avAdmin') return _.isNumber(el.id) && el.id <= 0; }); - if (data.length > 1 || hasNegativeIds) { + if (elections.length > 1 || hasNegativeIds) { // Find highest election id Authmethod.highestEvent() @@ -1287,18 +1287,18 @@ angular.module('avAdmin') var newIdsMap = {}; // map negative ids to new election ids - for (var eid = 0; eid < elections.length; eid++) { - var election = elections[eid]; - if (_.isNumber(election.id) && election.id > 0) { - newIdsMap[election.id] = election.id; + for (var idx = 0; idx < elections.length; idx++) { + var electionId = elections[idx].id; + if (_.isNumber(electionId) && electionId > 0) { + newIdsMap[electionId] = electionId; } else { - newIdsMap[election.id] = highestId + eid + 1; + newIdsMap[electionId] = highestId + idx + 1; } } // replace election ids with new ids - for (var eid = 0; eid < elections.length; eid++) { - var election = elections[eid]; + for (var index = 0; index < elections.length; index++) { + var election = elections[index]; // replace the election id election.id = newIdsMap[election.id]; From b4cf6d274c5d5fa9129faf82ea544dcfbc71ec11 Mon Sep 17 00:00:00 2001 From: Findeton Date: Fri, 30 Dec 2022 16:13:21 +0100 Subject: [PATCH 4/8] wip --- avAdmin/admin-directives/create/create.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index 09a4bc89..6b1a93e9 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -1296,6 +1296,10 @@ angular.module('avAdmin') } } + function mapElectionId(e) { + return newIdsMap[e] || e; + } + // replace election ids with new ids for (var index = 0; index < elections.length; index++) { var election = elections[index]; @@ -1312,9 +1316,7 @@ angular.module('avAdmin') // replace ids in the children elections structure if (election.children_election_info && election.children_election_info.natural_order) { - election.children_election_info.natural_order = election.children_election_info.natural_order.map(function (e) { - return newIdsMap[e] || e; - }); + election.children_election_info.natural_order = election.children_election_info.natural_order.map(mapElectionId); } if (election.children_election_info && election.children_election_info.presentation && @@ -1324,7 +1326,7 @@ angular.module('avAdmin') if (category.events) { category.events = category.events.map(function (event) { if (event.event_id) { - event.event_id = newIdsMap[event.event_id] || event.event_id; + event.event_id = mapElectionId(event.event_id); } }); } From 185bbc2648e5daba6322146ce225a5830268d04b Mon Sep 17 00:00:00 2001 From: Findeton Date: Fri, 30 Dec 2022 16:29:22 +0100 Subject: [PATCH 5/8] wip --- avAdmin/admin-directives/create/create.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index 6b1a93e9..c99111c4 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -1296,10 +1296,6 @@ angular.module('avAdmin') } } - function mapElectionId(e) { - return newIdsMap[e] || e; - } - // replace election ids with new ids for (var index = 0; index < elections.length; index++) { var election = elections[index]; @@ -1316,7 +1312,9 @@ angular.module('avAdmin') // replace ids in the children elections structure if (election.children_election_info && election.children_election_info.natural_order) { - election.children_election_info.natural_order = election.children_election_info.natural_order.map(mapElectionId); + election.children_election_info.natural_order = election.children_election_info.natural_order.map(function (myMap, e) { + return myMap[e] || e; + }).bind(null, newIdsMap); } if (election.children_election_info && election.children_election_info.presentation && @@ -1324,11 +1322,11 @@ angular.module('avAdmin') election.children_election_info.presentation.categories = election.children_election_info.presentation.categories.map(function (category) { if (category.events) { - category.events = category.events.map(function (event) { + category.events = category.events.map(function (myMap, event) { if (event.event_id) { - event.event_id = mapElectionId(event.event_id); + event.event_id = myMap[event.event_id] || event.event_id; } - }); + }).bind(null, newIdsMap); } return category; }); From ad973190f81f690642c95a582b9086c9d3865098 Mon Sep 17 00:00:00 2001 From: Findeton Date: Fri, 30 Dec 2022 16:38:47 +0100 Subject: [PATCH 6/8] wip --- avAdmin/admin-directives/create/create.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index c99111c4..69b9619b 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -1277,6 +1277,8 @@ angular.module('avAdmin') return _.isNumber(el.id) && el.id <= 0; }); + /* jshint ignore:start */ + if (elections.length > 1 || hasNegativeIds) { // Find highest election id @@ -1312,9 +1314,9 @@ angular.module('avAdmin') // replace ids in the children elections structure if (election.children_election_info && election.children_election_info.natural_order) { - election.children_election_info.natural_order = election.children_election_info.natural_order.map(function (myMap, e) { - return myMap[e] || e; - }).bind(null, newIdsMap); + election.children_election_info.natural_order = election.children_election_info.natural_order.map(function (e) { + return newIdsMap[e] || e; + }); } if (election.children_election_info && election.children_election_info.presentation && @@ -1322,11 +1324,11 @@ angular.module('avAdmin') election.children_election_info.presentation.categories = election.children_election_info.presentation.categories.map(function (category) { if (category.events) { - category.events = category.events.map(function (myMap, event) { + category.events = category.events.map(function (event) { if (event.event_id) { - event.event_id = myMap[event.event_id] || event.event_id; + event.event_id = newIdsMap[event.event_id] || event.event_id; } - }).bind(null, newIdsMap); + }); } return category; }); @@ -1339,6 +1341,7 @@ angular.module('avAdmin') } else { deferred.resolve(elections); } + /* jshint ignore:end */ return promise; } From 3bd98fc802688c8037b591b7dc7220045a58d8f7 Mon Sep 17 00:00:00 2001 From: Findeton Date: Fri, 30 Dec 2022 19:11:15 +0100 Subject: [PATCH 7/8] parent id --- avAdmin/admin-directives/create/create.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index 69b9619b..acbab467 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -1304,6 +1304,11 @@ angular.module('avAdmin') // replace the election id election.id = newIdsMap[election.id]; + // replace the parent id + if (election.parent_id) { + election.parent_id = newIdsMap[election.parent_id] || election.parent_id; + } + // replace ids for virtualSubelections if (election.virtualSubelections) { election.virtualSubelections = election.virtualSubelections.map(function (e) { From cd022f82bec1ef152e7c14f990c0f595145bcfba Mon Sep 17 00:00:00 2001 From: Findeton Date: Tue, 17 Jan 2023 09:50:27 +0100 Subject: [PATCH 8/8] wip --- avAdmin/admin-directives/create/create.js | 1 + 1 file changed, 1 insertion(+) diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index acbab467..9cc13616 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -1333,6 +1333,7 @@ angular.module('avAdmin') if (event.event_id) { event.event_id = newIdsMap[event.event_id] || event.event_id; } + return event; }); } return category;