Skip to content

Commit

Permalink
ANGOLASUP-931: Retrieving facilities from the cache on the SoH and Re…
Browse files Browse the repository at this point in the history
…quisition Create/Authorize page (#184)
  • Loading branch information
saleksandra authored Oct 10, 2024
1 parent 781b365 commit ce3288e
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/admin-user-form/admin-user-form.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/openlmis-cached-repository/openlmis-cached-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions src/openlmis-cached-repository/openlmis-cached-resource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
loginService.registerPostLoginAction(function() {
return $q.all([
facilityService.cacheAllMinimal(),
facilityService.getFacilitiesWithoutWards()
facilityService.getFacilitiesWithoutWards(false)
]);
});

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

});
Expand Down
5 changes: 3 additions & 2 deletions src/referencedata-facility/facility-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@

return FacilityResource;

function FacilityResource() {
function FacilityResource(getDataFromCache) {
this.super('/api/facilities', 'facilities', {
versioned: false
versioned: false,
getDataFromCache: getDataFromCache
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/referencedata-facility/facility-resource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
});
7 changes: 4 additions & 3 deletions src/referencedata-facility/facility.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ce3288e

Please sign in to comment.