Skip to content

Commit

Permalink
Merge pull request #157 from OpenLMIS-Angola/feature/OAM-263-2
Browse files Browse the repository at this point in the history
OAM-263: add caching for wards, fix soh offline
  • Loading branch information
DominikNoga authored Jul 30, 2024
2 parents 8a9eedb + 8b15bef commit 22ccb7d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/admin-facility-view/admin-facility-view.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/admin-facility-view/ward-cache.run.js
Original file line number Diff line number Diff line change
@@ -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 [email protected]
*/

(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();
});
}

})();
53 changes: 29 additions & 24 deletions src/admin-facility-view/ward.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand All @@ -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;
}

/**
Expand All @@ -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();
}
}
})();
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/stock-card-summary-list/stock-card-summary-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ <h2 ng-hide="vm.facility && vm.program">{{'stockCardSummaryList.stockOnHand' | m
</tr>
</tbody>
</table>
<openlmis-pagination ng-if="!vm.offline()" list="vm.displayStockCardSummaries" paged-list="vm.pagedList" pagination-id="'stockCardList'"/>
<openlmis-pagination ng-if="vm.offline()" list="vm.stockCardSummaries" paged-list="vm.pagedList"></openlmis-pagination>
<openlmis-pagination list="vm.displayStockCardSummaries" paged-list="vm.pagedList" pagination-id="'stockCardList'"/>
</section>
<!-- AO-816: Add prices to the Stock On Hand view -->
<aside class="requisition-summary">
Expand Down
2 changes: 1 addition & 1 deletion src/stock-receive-creation/receive-creation.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
};

return wardService.getWardsByFacility(searchParams).then(function(response) {
var wards = response.content;
var wards = response.content ? response.content : response;
var wardsOrderableGroupPromises = wards.map(function(ward) {
return orderableGroupService
.findAvailableProductsAndCreateOrderableGroups(program.id, ward.id, true);
Expand Down

0 comments on commit 22ccb7d

Please sign in to comment.