diff --git a/avAdmin/admin-controller/admin-controller-spec.js b/avAdmin/admin-controller/admin-controller-spec.js index 1b96eba9..4117d22a 100644 --- a/avAdmin/admin-controller/admin-controller-spec.js +++ b/avAdmin/admin-controller/admin-controller-spec.js @@ -39,6 +39,7 @@ describe("Admin Controler tests", function () { $i18next: undefined, $cookies: undefined, ConfigService: { helpUrl: 'http://sequentech.io', showSuccessAction: true }, + AutomaticIds: undefined, ElectionsApi: { autoreloadStats: function() {}, getElection: function() { diff --git a/avAdmin/admin-controller/admin-controller.js b/avAdmin/admin-controller/admin-controller.js index 9544ea8b..80fa0f26 100644 --- a/avAdmin/admin-controller/admin-controller.js +++ b/avAdmin/admin-controller/admin-controller.js @@ -32,7 +32,8 @@ angular $timeout, $q, $window, - $modal + $modal, + AutomaticIds ) { var id = $stateParams.id; $scope.electionId = id; @@ -136,8 +137,15 @@ angular .then( function (fileText) { - ElectionsApi.currentElections = JSON.parse(fileText); - $state.go("admin.create"); + var elections = JSON.parse(fileText); + AutomaticIds.fillInElectionIds(elections) + .then( + function (fixedElections) + { + ElectionsApi.currentElections = fixedElections; + $state.go("admin.create"); + } + ); } ); } diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index 1042388d..ac2c3660 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -35,7 +35,8 @@ angular.module('avAdmin') CheckerService, ElectionCreation, CsvLoad, - MustExtraFieldsService) + MustExtraFieldsService, + AutomaticIds) { /** * @returns true if the url with the specific title and url appears in the @@ -1227,94 +1228,6 @@ angular.module('avAdmin') deferred.resolve(scope.elections[electionIndex]); } - /** - * 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; - }); - - /* jshint ignore:start */ - - if (elections.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 idx = 0; idx < elections.length; idx++) { - var electionId = elections[idx].id; - if (_.isNumber(electionId) && electionId > 0) { - newIdsMap[electionId] = electionId; - } else { - newIdsMap[electionId] = highestId + idx + 1; - } - } - - // replace election ids with new ids - for (var index = 0; index < elections.length; index++) { - var election = elections[index]; - // 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) { - 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 event; - }); - } - return category; - }); - } - } - deferred.resolve(elections); - }, - deferred.reject - ); - } else { - deferred.resolve(elections); - } - /* jshint ignore:end */ - return promise; - } - function addElection(electionIndex) { var deferred = $q.defer(); @@ -1378,7 +1291,7 @@ angular.module('avAdmin') function (data) { var elections = angular.fromJson(data.electionJson); - return fillInElectionIds(elections); + return AutomaticIds.fillInElectionIds(elections); } ).then( function (data) diff --git a/avAdmin/automatic-ids-service.js b/avAdmin/automatic-ids-service.js new file mode 100644 index 00000000..1deeb70a --- /dev/null +++ b/avAdmin/automatic-ids-service.js @@ -0,0 +1,120 @@ +/** + * This file is part of admin-console. + * Copyright (C) 2022 Sequent Tech Inc + + * admin-console is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License. + + * admin-console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with admin-console. If not, see . +**/ + +/** + * Service to manage the keys ceremony modal steps. + */ +angular +.module('avAdmin') +.factory('AutomaticIds', function( + $q, + Authmethod +) +{ + var service = { + }; + + /** + * 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. + */ + service.fillInElectionIds = function (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; + }); + + /* jshint ignore:start */ + + if (elections.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 idx = 0; idx < elections.length; idx++) { + var electionId = elections[idx].id; + if (_.isNumber(electionId) && electionId > 0) { + newIdsMap[electionId] = electionId; + } else { + newIdsMap[electionId] = highestId + idx + 1; + } + } + + // replace election ids with new ids + for (var index = 0; index < elections.length; index++) { + var election = elections[index]; + // 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) { + 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 event; + }); + } + return category; + }); + } + } + deferred.resolve(elections); + }, + deferred.reject + ); + } else { + deferred.resolve(elections); + } + /* jshint ignore:end */ + return promise; + }; + + return service; +}); \ No newline at end of file diff --git a/index.html b/index.html index b87dc700..bcd96d6a 100755 --- a/index.html +++ b/index.html @@ -135,6 +135,7 @@ +