Skip to content

Commit

Permalink
Merge pull request #168 from OpenLMIS-Angola/OAM-291
Browse files Browse the repository at this point in the history
OAM-291: made receive and issue and soh work offline
  • Loading branch information
dszafranek authored Aug 12, 2024
2 parents c8e4a3c + 598e1df commit 3dfcb38
Show file tree
Hide file tree
Showing 25 changed files with 2,397 additions and 195 deletions.
1 change: 0 additions & 1 deletion src/admin-facility-view/admin-facility-view.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
zoneId: facility.geographicZone.id,
sort: 'code,asc',
type: WARDS_CONSTANTS.WARD_TYPE_CODE

};

return wardService.getWardsByFacility(searchParams).then(function(response) {
Expand Down
519 changes: 519 additions & 0 deletions src/openlmis-cached-repository/openlmis-cached-resource.js

Large diffs are not rendered by default.

929 changes: 929 additions & 0 deletions src/openlmis-cached-repository/openlmis-cached-resource.spec.js

Large diffs are not rendered by default.

30 changes: 22 additions & 8 deletions src/openlmis-local-storage/local-storage.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,39 @@
* @ngdoc method
* @methodOf openlmis-local-storage.localStorageFactory
* @name putAll
*
*
* @description
* Stores all objects in local storage.
*
*
* @param {Array} collectionToStore Array of objects to store
*/
function putAll(collectionToStore) {
if (Array.isArray(collectionToStore) && collectionToStore.length > 0) {
executeWithStorageUpdate(function() {
items = collectionToStore.map(function(item) {
if (item && item.id) {
removeItemBy('id', item.id);
}
return typeof item === 'object' ? JSON.parse(JSON.stringify(item)) : item;
});
items = mergeArraysByID(items, collectionToStore);
});
}
}

function mergeArraysByID(originalArray, newArray) {
var idMap = {};
newArray.forEach(function(obj) {
idMap[obj.id] = obj;
});
return originalArray.map(function(originalObj) {
var newObj = idMap[originalObj.id];
if (newObj) {
delete idMap[originalObj.id];
return Object.assign({}, originalObj, newObj);
}
return originalObj;
}).concat(
newArray.filter(function(newObj) {
return idMap.hasOwnProperty(newObj.id);
})
);
}

/**
* @ngdoc method
* @methodOf openlmis-local-storage.localStorageFactory
Expand Down
3 changes: 2 additions & 1 deletion src/openlmis-local-storage/local-storage.factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ describe('localStorageFactory', function() {
it('should update storage', function() {
this.itemsLocalStorage.putAll(this.putAllItems);

expect(this.localStorageService.add).toHaveBeenCalledWith('items', angular.toJson(this.putAllItems));
expect(this.localStorageService.add)
.toHaveBeenCalledWith('items', angular.toJson(this.items.concat(this.putAllItems)));
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@

loginService.registerPostLogoutAction(function() {
return $q.all([
facilityService.clearMinimalFacilitiesCache(),
facilityService.clearFacilitiesWithoutWardsCache()
facilityService.clearMinimalFacilitiesCache()
]);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('referencedata-facilities-cache run', function() {
spyOn(this.facilityService, 'cacheAllMinimal');
spyOn(this.facilityService, 'getFacilitiesWithoutWards');
spyOn(this.facilityService, 'clearMinimalFacilitiesCache');
spyOn(this.facilityService, 'clearFacilitiesWithoutWardsCache');
});

describe('run block', function() {
Expand Down
46 changes: 46 additions & 0 deletions src/referencedata-facility/facility-resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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';

/**
* @ngdoc service
* @name referencedata-facility.FacilityResource
*
* @description
* Implementation of the FacilityResource interface. Communicates with the REST API of the OpenLMIS
* server.
*/
angular
.module('referencedata-facility')
.factory('FacilityResource', FacilityResource);

FacilityResource.$inject = ['OpenlmisCachedResource', 'classExtender'];

function FacilityResource(OpenlmisCachedResource, classExtender) {

classExtender.extend(FacilityResource, OpenlmisCachedResource);

return FacilityResource;

function FacilityResource() {
this.super('/api/facilities', 'facilities', {
versioned: false
});
}
}
})();
40 changes: 40 additions & 0 deletions src/referencedata-facility/facility-resource.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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]
*/

describe('FacilityResource', function() {

beforeEach(function() {
this.OpenlmisCachedResourceMock = jasmine.createSpy('OpenlmisCachedResource');

var OpenlmisCachedResourceMock = this.OpenlmisCachedResourceMock;
module('referencedata-facility', function($provide) {
$provide.factory('OpenlmisCachedResource', function() {
return OpenlmisCachedResourceMock;
});
});

inject(function($injector) {
this.FacilityResource = $injector.get('FacilityResource');
});
});

it('should extend OpenlmisCachedResource', function() {
new this.FacilityResource();

expect(this.OpenlmisCachedResourceMock).toHaveBeenCalledWith('/api/facilities', 'facilities', {
versioned: false
});
});
});
99 changes: 19 additions & 80 deletions src/referencedata-facility/facility.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,16 @@
.service('facilityService', service);

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

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

var facilitiesOffline = localStorageFactory('facilities'),
facilitiesWithoutWardsOffline = localStorageFactory('facilitiesWithoutWards'),
facilitiesPromise,
function service($q, $resource, referencedataUrlFactory, permissionService,
FacilityResource, localStorageService, WARDS_CONSTANTS) {
var
facilityResource = new FacilityResource(),
resource = $resource(referencedataUrlFactory('/api/facilities/:id'), {}, {
getAll: {
url: referencedataUrlFactory('/api/facilities/'),
method: 'GET'
},
getFacilitiesWithoutWards: {
url: referencedataUrlFactory('/api/facilities/'),
method: 'GET'
},
query: {
url: referencedataUrlFactory('/api/facilities/'),
method: 'GET'
},
getAllMinimal: {
url: referencedataUrlFactory('/api/facilities/minimal'),
method: 'GET'
Expand All @@ -66,9 +51,6 @@
search: {
url: referencedataUrlFactory('/api/facilities/search'),
method: 'POST'
},
update: {
method: 'PUT'
}
});

Expand All @@ -79,10 +61,9 @@
this.getUserFacilitiesForRight = getUserFacilitiesForRight;
this.getFulfillmentFacilities = getFulfillmentFacilities;
this.search = search;
this.clearFacilitiesCache = clearFacilitiesCache;
this.getFacilitiesWithoutWards = getFacilitiesWithoutWards;
this.getAllMinimalWithoutWards = getAllMinimalWithoutWards;
this.clearFacilitiesWithoutWardsCache = clearFacilitiesWithoutWardsCache;
this.clearCache = clearCache;

/**
* @ngdoc method
Expand All @@ -97,19 +78,10 @@
* @return {Promise} facility promise
*/
function get(facilityId) {
var cachedFacility = facilitiesOffline.getBy('id', facilityId);

if (cachedFacility) {
facilitiesPromise = $q.resolve(angular.fromJson(cachedFacility));
} else {
facilitiesPromise = new FacilityResource().get(facilityId)
.then(function(facility) {
facilitiesOffline.put(facility);
return $q.resolve(facility);
});
}

return facilitiesPromise;
return facilityResource.get(facilityId)
.then(function(facility) {
return $q.resolve(facility);
});
}

/**
Expand All @@ -123,16 +95,11 @@
* @return {Promise} Array of facilities
*/
function getFacilitiesWithoutWards() {
if (offlineService.isOffline()) {
return $q.resolve(facilitiesWithoutWardsOffline.getAll());
}

return resource.getAll().$promise
return facilityResource.getAll()
.then(function(response) {
var facilitiesWithoutWards = response.content.filter(function(facility) {
var facilitiesWithoutWards = response.filter(function(facility) {
return facility.type.code !== WARDS_CONSTANTS.WARD_TYPE_CODE;
});
facilitiesWithoutWardsOffline.putAll(facilitiesWithoutWards);
return facilitiesWithoutWards;
});
}
Expand All @@ -147,27 +114,20 @@
* When user is offline it gets facilities from offline storage.
* If user is online it stores all facilities into offline storage.
*
* @param {String} queryParams the pagination parameters
* @param {String} queryParams the search parameters
* @return {Promise} Array of facilities
*/
function query(paginationParams, queryParams) {
if (offlineService.isOffline()) {
return $q.resolve(facilitiesOffline.getAll());
}
return resource.query(_.extend({}, queryParams, paginationParams)).$promise
function query(queryParams) {
return facilityResource.query(queryParams)
.then(function(page) {
page.content.forEach(function(facility) {
facilitiesOffline.put(facility);
});
return page;
});
}

function getAll() {
return resource.getAll().$promise
return facilityResource.getAll()
.then(function(response) {
return response.content;
return response.content ? response.content : response;
});
}

Expand Down Expand Up @@ -300,29 +260,8 @@
});
}

/**
* @ngdoc method
* @methodOf referencedata-facility.facilityService
* @name clearFacilitiesCache
*
* @description
* Deletes facilities stored in the browser cache.
*/
function clearFacilitiesCache() {
facilitiesPromise = undefined;
localStorageService.remove('facilities');
}

/**
* @ngdoc method
* @methodOf referencedata-facility.facilityService
* @name clearFacilitiesWithoutWardsCache
*
* @description
* Deletes facilities without wards stored in the browser cache.
*/
function clearFacilitiesWithoutWardsCache() {
localStorageService.remove('facilitiesWithoutWards');
function clearCache() {
return facilityResource.destroy();
}
}
})();
Loading

0 comments on commit 3dfcb38

Please sign in to comment.