diff --git a/src/admin-geographic-zone-population/admin-geographic-zone-population.service.js b/src/admin-geographic-zone-population/admin-geographic-zone-population.service.js index 89da5bab..c563dffc 100644 --- a/src/admin-geographic-zone-population/admin-geographic-zone-population.service.js +++ b/src/admin-geographic-zone-population/admin-geographic-zone-population.service.js @@ -23,17 +23,22 @@ geoZoneCatchmentPopulationService.$inject = [ 'featureFlagService', - 'GeographicLevelService', + 'GeoLevelResource', 'CATCHMENT_POPULATION_CALC_AUTO_FEATURE_FLAG' ]; function geoZoneCatchmentPopulationService( featureFlagService, - GeographicLevelService, + GeoLevelResource, CATCHMENT_POPULATION_CALC_AUTO_FEATURE_FLAG ) { const isCatchmentPopulationAutoCalc = featureFlagService.get(CATCHMENT_POPULATION_CALC_AUTO_FEATURE_FLAG); - const lowestLevel = GeographicLevelService.getTheLowestLevelNumber(); + let lowestLevel = null; + + new GeoLevelResource().getTheLowestGeoLevel() + .then(function(geoLevel) { + lowestLevel = geoLevel.levelNumber; + }); return { isEditable: function(geographicZone) { diff --git a/src/referencedata-geographic-level/geo-level-resource.js b/src/referencedata-geographic-level/geo-level-resource.js index 63701dc9..bbe44f9e 100644 --- a/src/referencedata-geographic-level/geo-level-resource.js +++ b/src/referencedata-geographic-level/geo-level-resource.js @@ -33,6 +33,24 @@ }); } + GeoLevelResource.prototype.getTheLowestGeoLevel = getTheLowestGeoLevel; + return GeoLevelResource; + + function getTheLowestGeoLevel() { + return new Promise(function(resolve) { + new GeoLevelResource().query() + .then(function(resource) { + const lowestGeoLevel = resource.content.reduce(function(lowestGeoLevel, geoLevel) { + // Compare and keep the one with the highest `levelNumber` value + return geoLevel.levelNumber > lowestGeoLevel.levelNumber + ? geoLevel : lowestGeoLevel; + }); + + resolve(lowestGeoLevel); + }); + }); + } + } })(); diff --git a/src/referencedata-geographic-level/geographic-level.service.js b/src/referencedata-geographic-level/geographic-level.service.js deleted file mode 100644 index 221d896a..00000000 --- a/src/referencedata-geographic-level/geographic-level.service.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This program is part of the OpenLMIS logistics management information system platform software. - * Copyright © 2017 VillageReach - * - * This program 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, or (at your option) any later version. - *   - * This program 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 this program. If not, see - * http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org.  - */ - -(function() { - - 'use strict'; - - angular - .module('referencedata-geographic-level') - .service('GeographicLevelService', GeographicLevelService); - - /** - * @ngdoc object - * @name referencedata-geographic-level.GEOGRAPHIC_LEVEL - * - * @description - * This constant provides the geographic level number. - */ - var GEOGRAPHIC_LEVEL = { - NATIONAL: 1, - PROVINCE: 2, - DISTRICT: 3 - }; - - function GeographicLevelService() { - return { - /** - * @ngdoc function - * @name referencedata-geographic-level.service:getTheLowestLevelNumber - * - * @description - * This function returns the lowest level number. Lowest in this case means the most detailed level. - */ - getTheLowestLevelNumber: function() { - return GEOGRAPHIC_LEVEL.DISTRICT; - } - }; - } - -})();