Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions avAdmin/admin-controller/admin-controller-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 11 additions & 3 deletions avAdmin/admin-controller/admin-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ angular
$timeout,
$q,
$window,
$modal
$modal,
AutomaticIds
) {
var id = $stateParams.id;
$scope.electionId = id;
Expand Down Expand Up @@ -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");
}
);
}
);
}
Expand Down
93 changes: 3 additions & 90 deletions avAdmin/admin-directives/create/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
120 changes: 120 additions & 0 deletions avAdmin/automatic-ids-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* This file is part of admin-console.
* Copyright (C) 2022 Sequent Tech Inc <legal@sequentech.io>

* 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 <http://www.gnu.org/licenses/>.
**/

/**
* 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;
});
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<script src="avAdmin/onboarding-tour-service.js" class="app"></script>
<script src="avAdmin/keys-ceremony-service.js" class="app"></script>
<script src="avAdmin/base64-codec-service.js" class="app"></script>
<script src="avAdmin/automatic-ids-service.js" class="app"></script>
<!-- endof admin directives -->

<script src="avAdmin/next-button-service.js" class="app"></script>
Expand Down