Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AO-898: Fix for viewing requisition view page offline #193

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/referencedata-requisition-group/requisition-group.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
.module('referencedata-requisition-group')
.factory('requisitionGroupService', service);

service.$inject = ['referencedataUrlFactory', '$resource', '$q'];
service.$inject = ['referencedataUrlFactory', '$resource', '$q', 'offlineService', 'localStorageFactory',
'localStorageService'];

function service(referencedataUrlFactory, $resource, $q) {
function service(referencedataUrlFactory, $resource, $q, offlineService, localStorageFactory,
localStorageService) {

var offlineRequisitionGroup = localStorageFactory('requisitionGroups');
var resource = $resource(referencedataUrlFactory('/api/requisitionGroups/:id'), {}, {
getAll: {
url: referencedataUrlFactory('/api/requisitionGroups'),
Expand All @@ -49,6 +52,10 @@
// AO-849: ends here
});

this.cacheRequisitionGroups = cacheRequisitionGroups;
this.getRequisitionGroups = getRequisitionGroups;
this.clearRequisitionGroups = clearRequisitionGroups;

return {
get: get,
getAll: getAll,
Expand Down Expand Up @@ -85,7 +92,14 @@
* @return {Promise} Array of all programs
*/
function getAll() {
return resource.getAll().$promise;
if (offlineService.isOffline()) {
return $q.resolve(getRequisitionGroups());
}
return resource.getAll().$promise.then(function(response) {
clearRequisitionGroups();
cacheRequisitionGroups(response);
return $q.resolve(response);
});
}

/**
Expand Down Expand Up @@ -147,5 +161,42 @@
}
// SELV-476: ends here

/**
* @ngdoc method
* @methodOf referencedata-requisition-group.requisitionGroupService
* @name cacheRequisitionGroups
*
* @description
* Caches given requisition groups in the local storage.
*
* @param {Object} requisitionGroups the requisition groups to be cached
*/
function cacheRequisitionGroups(requisitionGroups) {
offlineRequisitionGroup.putAll(requisitionGroups);
}

/**
* @ngdoc method
* @methodOf referencedata-requisition-group.requisitionGroupService
* @name clearRequisitionGroups
*
* @description
* Deletes Requisition Groups stored in the browser cache.
*/
function clearRequisitionGroups() {
localStorageService.remove('requisitionGroups');
}

/**
* @ngdoc method
* @methodOf referencedata-requisition-group.requisitionGroupService
* @name getRequisitionGroups
*
* @description
* Gets all Requisition Groups stored in the browser cache.
*/
function getRequisitionGroups() {
return $q.resolve(offlineRequisitionGroup.getAll());
}
}
})();
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@
describe('requisitionGroupService', function() {

beforeEach(function() {
module('referencedata-requisition-group');
this.offlineService = jasmine.createSpyObj('offlineService', ['isOffline', 'checkConnection']);
this.requisitionGroupStorage = jasmine.createSpyObj('requisitionGroups', ['putAll', 'getAll', 'remove']);

var offlineService = this.offlineService,
requisitionGroupStorage = this.requisitionGroupStorage;

module('referencedata-requisition-group', function($provide) {
$provide.service('offlineService', function() {
return offlineService;
});

$provide.service('localStorageFactory', function() {
return jasmine.createSpy('localStorageFactory').andReturn(requisitionGroupStorage);
});
});

inject(function($injector) {
this.$httpBackend = $injector.get('$httpBackend');
Expand All @@ -25,6 +39,8 @@ describe('requisitionGroupService', function() {
this.requisitionGroupService = $injector.get('requisitionGroupService');
this.RequisitionGroupDataBuilder = $injector.get('RequisitionGroupDataBuilder');
this.PageDataBuilder = $injector.get('PageDataBuilder');
this.$q = $injector.get('$q');
this.localStorageService = $injector.get('localStorageService');
});

this.requisitionGroups = [
Expand All @@ -35,6 +51,8 @@ describe('requisitionGroupService', function() {
this.requisitionGroupsPage = new this.PageDataBuilder()
.withContent(this.requisitionGroups)
.build();

this.offlineService.isOffline.andReturn(false);
});

describe('get', function() {
Expand All @@ -59,7 +77,8 @@ describe('requisitionGroupService', function() {

describe('getAll', function() {

it('should resolve to requisition groups', function() {
it('should resolve to requisition groups and save it to storage', function() {
this.offlineService.isOffline.andReturn(false);
this.$httpBackend
.expectGET(this.referencedataUrlFactory('/api/requisitionGroups'))
.respond(200, this.requisitionGroups);
Expand All @@ -72,6 +91,21 @@ describe('requisitionGroupService', function() {
this.$rootScope.$apply();

expect(angular.toJson(result)).toEqual(angular.toJson(this.requisitionGroups));
expect(this.requisitionGroupStorage.putAll).toHaveBeenCalled();
});

it('should resolve to requisition groups when offline', function() {
this.offlineService.isOffline.andReturn(true);
this.requisitionGroupStorage.getAll.andReturn(this.requisitionGroups);

var result;
this.requisitionGroupService.getAll().then(function(data) {
result = data;
});
this.$rootScope.$apply();

expect(angular.toJson(result)).toEqual(angular.toJson(this.requisitionGroups));
expect(this.requisitionGroupStorage.getAll).toHaveBeenCalled();
});
});

Expand Down
Loading
Loading