From ce3288eacb7c71613a39083b7f8de343903441e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandra=20So=C5=82tys?= Date: Thu, 10 Oct 2024 11:09:31 +0200 Subject: [PATCH] ANGOLASUP-931: Retrieving facilities from the cache on the SoH and Requisition Create/Authorize page (#184) --- src/admin-user-form/admin-user-form.routes.js | 2 +- .../openlmis-cached-resource.js | 6 ++++- .../openlmis-cached-resource.spec.js | 27 +++++++++++++++++++ .../facility-program-cache.service.js | 2 +- .../facility-program-cache.service.spec.js | 2 +- .../referencedata-facilities-cache.run.js | 2 +- ...referencedata-facilities-cache.run.spec.js | 4 ++- .../facility-resource.js | 5 ++-- .../facility-resource.spec.js | 5 ++-- .../facility.service.js | 7 ++--- 10 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/admin-user-form/admin-user-form.routes.js b/src/admin-user-form/admin-user-form.routes.js index 04fef04..168a35a 100644 --- a/src/admin-user-form/admin-user-form.routes.js +++ b/src/admin-user-form/admin-user-form.routes.js @@ -29,7 +29,7 @@ accessRights: [ADMINISTRATION_RIGHTS.USERS_MANAGE], resolve: { facilities: function(facilityService) { - return facilityService.getFacilitiesWithoutWards(); + return facilityService.getFacilitiesWithoutWards(false); }, user: function(UserService, $stateParams) { return new UserService().get($stateParams.id); diff --git a/src/openlmis-cached-repository/openlmis-cached-resource.js b/src/openlmis-cached-repository/openlmis-cached-resource.js index f486d04..707a76e 100644 --- a/src/openlmis-cached-repository/openlmis-cached-resource.js +++ b/src/openlmis-cached-repository/openlmis-cached-resource.js @@ -260,7 +260,7 @@ * rejected if request fails */ function getAll(params) { - if (offlineService.isOffline()) { + if (offlineService.isOffline() || getDataFromCache(this.config)) { var deferred = $q.defer(); this.database.getAll().then(function(response) { deferred.resolve(response); @@ -434,6 +434,10 @@ return !config || config.versioned || config.versioned === undefined; } + function getDataFromCache(config) { + return config && config.getDataFromCache ? config.getDataFromCache : false; + } + function getLastModifiedDate(database) { return database.get('_local/lastModified') .then(function(result) { diff --git a/src/openlmis-cached-repository/openlmis-cached-resource.spec.js b/src/openlmis-cached-repository/openlmis-cached-resource.spec.js index 6ffbed5..6df82ff 100644 --- a/src/openlmis-cached-repository/openlmis-cached-resource.spec.js +++ b/src/openlmis-cached-repository/openlmis-cached-resource.spec.js @@ -913,6 +913,33 @@ describe('OpenlmisCachedResource', function() { expect(this.LocalDatabase.prototype.put).toHaveBeenCalled(); }); + it('should resolve for offline', function() { + this.offlineService.isOffline.andReturn(true); + this.openlmisCachedResource.isVersioned = false; + spyOn(this.LocalDatabase.prototype, 'getAll').andReturn(this.getDeferred.promise); + spyOn(this.LocalDatabase.prototype, 'put').andReturn(this.response); + + this.openlmisCachedResource.getAll(undefined); + this.$rootScope.$apply(); + + expect(this.LocalDatabase.prototype.getAll).toHaveBeenCalled(); + expect(this.LocalDatabase.prototype.put).not.toHaveBeenCalled(); + }); + + it('should resolve for online and when config.getDataFromCache is true', function() { + this.offlineService.isOffline.andReturn(false); + this.openlmisCachedResource.isVersioned = false; + this.config.getDataFromCache = true; + spyOn(this.LocalDatabase.prototype, 'getAll').andReturn(this.getDeferred.promise); + spyOn(this.LocalDatabase.prototype, 'put').andReturn(this.response); + + this.openlmisCachedResource.getAll(undefined); + this.$rootScope.$apply(); + + expect(this.LocalDatabase.prototype.getAll).toHaveBeenCalled(); + expect(this.LocalDatabase.prototype.put).not.toHaveBeenCalled(); + }); + }); describe('throwMethodNotSupported', function() { diff --git a/src/openlmis-facility-program-select/facility-program-cache.service.js b/src/openlmis-facility-program-select/facility-program-cache.service.js index cdfd9ca..1d834d9 100644 --- a/src/openlmis-facility-program-select/facility-program-cache.service.js +++ b/src/openlmis-facility-program-select/facility-program-cache.service.js @@ -164,7 +164,7 @@ var userId = authorizationService.getUser().user_id; loadingModalService.open(); return $q.all([ - facilityService.getFacilitiesWithoutWards(), + facilityService.getFacilitiesWithoutWards(true), programService.getUserPrograms(userId), permissionService.load(userId), currentUserService.getUserInfo() diff --git a/src/openlmis-facility-program-select/facility-program-cache.service.spec.js b/src/openlmis-facility-program-select/facility-program-cache.service.spec.js index 10fc8f4..ea9e621 100644 --- a/src/openlmis-facility-program-select/facility-program-cache.service.spec.js +++ b/src/openlmis-facility-program-select/facility-program-cache.service.spec.js @@ -91,7 +91,7 @@ describe('facilityProgramCacheService', function() { }); it('should call facilityService', function() { - expect(this.facilityService.getFacilitiesWithoutWards).toHaveBeenCalled(); + expect(this.facilityService.getFacilitiesWithoutWards).toHaveBeenCalledWith(true); }); it('should call authorizationService', function() { diff --git a/src/referencedata-facilities-cache/referencedata-facilities-cache.run.js b/src/referencedata-facilities-cache/referencedata-facilities-cache.run.js index 6e67640..6e7a069 100644 --- a/src/referencedata-facilities-cache/referencedata-facilities-cache.run.js +++ b/src/referencedata-facilities-cache/referencedata-facilities-cache.run.js @@ -28,7 +28,7 @@ loginService.registerPostLoginAction(function() { return $q.all([ facilityService.cacheAllMinimal(), - facilityService.getFacilitiesWithoutWards() + facilityService.getFacilitiesWithoutWards(false) ]); }); diff --git a/src/referencedata-facilities-cache/referencedata-facilities-cache.run.spec.js b/src/referencedata-facilities-cache/referencedata-facilities-cache.run.spec.js index 4d12bea..001cd78 100644 --- a/src/referencedata-facilities-cache/referencedata-facilities-cache.run.spec.js +++ b/src/referencedata-facilities-cache/referencedata-facilities-cache.run.spec.js @@ -59,6 +59,7 @@ describe('referencedata-facilities-cache run', function() { it('should set up rights', function() { this.facilityService.cacheAllMinimal.andReturn(this.$q.resolve()); + this.facilityService.getFacilitiesWithoutWards.andReturn(this.$q.resolve()); var success; this.postLoginAction() @@ -68,7 +69,8 @@ describe('referencedata-facilities-cache run', function() { this.$rootScope.$apply(); expect(success).toBe(true); - expect(this.facilityService.cacheAllMinimal).toHaveBeenCalledWith(); + expect(this.facilityService.cacheAllMinimal).toHaveBeenCalled(); + expect(this.facilityService.getFacilitiesWithoutWards).toHaveBeenCalledWith(false); }); }); diff --git a/src/referencedata-facility/facility-resource.js b/src/referencedata-facility/facility-resource.js index e543e73..2d79493 100644 --- a/src/referencedata-facility/facility-resource.js +++ b/src/referencedata-facility/facility-resource.js @@ -37,9 +37,10 @@ return FacilityResource; - function FacilityResource() { + function FacilityResource(getDataFromCache) { this.super('/api/facilities', 'facilities', { - versioned: false + versioned: false, + getDataFromCache: getDataFromCache }); } } diff --git a/src/referencedata-facility/facility-resource.spec.js b/src/referencedata-facility/facility-resource.spec.js index 2f759cc..88c475a 100644 --- a/src/referencedata-facility/facility-resource.spec.js +++ b/src/referencedata-facility/facility-resource.spec.js @@ -31,10 +31,11 @@ describe('FacilityResource', function() { }); it('should extend OpenlmisCachedResource', function() { - new this.FacilityResource(); + new this.FacilityResource(true); expect(this.OpenlmisCachedResourceMock).toHaveBeenCalledWith('/api/facilities', 'facilities', { - versioned: false + versioned: false, + getDataFromCache: true }); }); }); \ No newline at end of file diff --git a/src/referencedata-facility/facility.service.js b/src/referencedata-facility/facility.service.js index a3823a4..608fd93 100644 --- a/src/referencedata-facility/facility.service.js +++ b/src/referencedata-facility/facility.service.js @@ -37,7 +37,7 @@ function service($q, $resource, referencedataUrlFactory, permissionService, FacilityResource, localStorageService, WARDS_CONSTANTS) { var - facilityResource = new FacilityResource(), + facilityResource = new FacilityResource(false), resource = $resource(referencedataUrlFactory('/api/facilities/:id'), {}, { getAllMinimal: { url: referencedataUrlFactory('/api/facilities/minimal'), @@ -91,10 +91,11 @@ * @description * Retrieves all facilities that are not a ward type * + * @param {boolean} getDataFromCache if data is retrieved from the cache or from the server * @return {Promise} Array of facilities */ - function getFacilitiesWithoutWards() { - return facilityResource.getAll() + function getFacilitiesWithoutWards(getDataFromCache) { + return new FacilityResource(getDataFromCache).getAll() .then(function(response) { var facilitiesWithoutWards = response.filter(function(facility) { return facility.type.code !== WARDS_CONSTANTS.WARD_TYPE_CODE;