diff --git a/src/admin-facility-view/admin-facility-view.routes.js b/src/admin-facility-view/admin-facility-view.routes.js index 89e56fd..fc4f5bc 100644 --- a/src/admin-facility-view/admin-facility-view.routes.js +++ b/src/admin-facility-view/admin-facility-view.routes.js @@ -70,10 +70,10 @@ }; return wardService.getWardsByFacility(searchParams).then(function(response) { - return response. - content.filter(function(responseFacility) { - return responseFacility.id !== facility.id; - }); + var wards = response.content ? response.content : response; + return wards.filter(function(responseFacility) { + return responseFacility.id !== facility.id; + }); }); } } diff --git a/src/admin-facility-view/ward-cache.run.js b/src/admin-facility-view/ward-cache.run.js new file mode 100644 index 0000000..3e4a5db --- /dev/null +++ b/src/admin-facility-view/ward-cache.run.js @@ -0,0 +1,42 @@ +/* + * 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('admin-facility-view') + .run(routes); + + routes.$inject = ['loginService', 'wardService', 'WARDS_CONSTANTS', 'facilityFactory']; + + function routes(loginService, wardService, WARDS_CONSTANTS, facilityFactory) { + loginService.registerPostLoginAction(function() { + facilityFactory.getUserHomeFacility().then(function(facility) { + wardService.getWardsByFacility({ + zoneId: facility.geographicZone.id, + sort: 'code,asc', + type: WARDS_CONSTANTS.WARD_TYPE_CODE + }); + }); + }); + + loginService.registerPostLogoutAction(function() { + return wardService.clearWardsByFacilityOffline(); + }); + } + +})(); \ No newline at end of file diff --git a/src/admin-facility-view/ward.service.js b/src/admin-facility-view/ward.service.js index 7fd7cea..b9e34e1 100644 --- a/src/admin-facility-view/ward.service.js +++ b/src/admin-facility-view/ward.service.js @@ -28,28 +28,26 @@ .module('admin-facility-view') .service('wardService', service); - service.$inject = ['$resource', 'referencedataUrlFactory']; + service.$inject = ['$resource', 'referencedataUrlFactory', 'offlineService', 'localStorageFactory', '$q']; - function service($resource, referencedataUrlFactory) { + function service($resource, referencedataUrlFactory, offlineService, localStorageFactory, $q) { - var resource = $resource(referencedataUrlFactory('/api/facilities/'), {}, { - getWardsByFacility: { - method: 'GET', - url: referencedataUrlFactory('/api/facilities/full') - }, - updateFacilityWard: { - url: referencedataUrlFactory('/api/facilities/:id'), - method: 'PUT' - }, - addNewWard: { - method: 'PUSH' - } - }); + var wardsByFacilityOffline = localStorageFactory('wardsByFacility'), + resource = $resource(referencedataUrlFactory('/api/facilities/'), {}, { + getWardsByFacility: { + method: 'GET', + url: referencedataUrlFactory('/api/facilities/full') + }, + updateFacilityWard: { + url: referencedataUrlFactory('/api/facilities/:id'), + method: 'PUT' + } + }); return { getWardsByFacility: getWardsByFacility, updateFacilityWard: updateFacilityWard, - addNewWard: addNewWard + clearWardsByFacilityOffline: clearWardsByFacilityOffline }; /** @@ -64,7 +62,16 @@ * @return {Promise} Array of wards */ function getWardsByFacility(queryParams) { - return resource.getWardsByFacility(queryParams).$promise; + if (offlineService.isOffline()) { + return $q.resolve(wardsByFacilityOffline.getAll()); + } + + var wardsByFacilityPromise = resource.getWardsByFacility(queryParams).$promise.then(function(response) { + wardsByFacilityOffline.putAll(response.content); + return response.$promise; + }); + + return wardsByFacilityPromise; } /** @@ -86,15 +93,13 @@ /** * @ngdoc method * @methodOf admin-facility-view.wardService - * @name addNewWard - * + * @name clearWardsByFacilityOffline + * * @description - * Creates a new Ward - * - * @return {Promise} + * Clears wards by facility offline data. */ - function addNewWard(ward) { - return resource.addNewWard(ward).$promise; + function clearWardsByFacilityOffline() { + wardsByFacilityOffline.clearAll(); } } })(); diff --git a/src/openlmis-facility-program-select/openlmis-facility-program-select.controller.js b/src/openlmis-facility-program-select/openlmis-facility-program-select.controller.js index b5de5ed..03bfd59 100644 --- a/src/openlmis-facility-program-select/openlmis-facility-program-select.controller.js +++ b/src/openlmis-facility-program-select/openlmis-facility-program-select.controller.js @@ -83,7 +83,8 @@ return wardService.getWardsByFacility(searchParams) .then(function(response) { - return response.content.filter(function(ward) { + var wards = response.content ? response.content : response; + return wards.filter(function(ward) { return ward.enabled; }); }); diff --git a/src/stock-adjustment-creation/adjustment-creation.controller.js b/src/stock-adjustment-creation/adjustment-creation.controller.js index 3c598e7..e4bd4ec 100644 --- a/src/stock-adjustment-creation/adjustment-creation.controller.js +++ b/src/stock-adjustment-creation/adjustment-creation.controller.js @@ -1038,7 +1038,7 @@ sort: 'code,asc', type: WARDS_CONSTANTS.WARD_TYPE_CODE }).then(function(response) { - var wards = response.content; + var wards = response.content ? response.content : response; var disabledWardsNames = []; vm.homeFacilityWards = wards.filter(function(ward) { diff --git a/src/stock-card-summary-list/stock-card-summary-list.controller.js b/src/stock-card-summary-list/stock-card-summary-list.controller.js index c556adb..8e59a76 100644 --- a/src/stock-card-summary-list/stock-card-summary-list.controller.js +++ b/src/stock-card-summary-list/stock-card-summary-list.controller.js @@ -164,7 +164,7 @@ checkCanFulFillIsEmpty(); unitOfOrderableService.getAll().then(function(response) { - vm.allUnitsOfOrderable = response.content; + vm.allUnitsOfOrderable = response.content ? response.content : response; vm.assignUnitsToCanFulfill(vm.displayStockCardSummaries); }); @@ -177,7 +177,7 @@ $scope.$watchCollection(function() { return vm.pagedList; }, function(newList) { - if (vm.offline()) { + if (vm.offline() && !!newList) { vm.displayStockCardSummaries = newList; vm.assignUnitsToCanFulfill(vm.displayStockCardSummaries); } diff --git a/src/stock-card-summary-list/stock-card-summary-list.html b/src/stock-card-summary-list/stock-card-summary-list.html index 00f4da0..c85829e 100644 --- a/src/stock-card-summary-list/stock-card-summary-list.html +++ b/src/stock-card-summary-list/stock-card-summary-list.html @@ -126,8 +126,7 @@