Skip to content

Commit

Permalink
Merge pull request #39 from OpenLMIS/OLMIS-7954
Browse files Browse the repository at this point in the history
OLMIS-7954: Improved the scalability of local storage and login performance for /api/requisitionGroups request
  • Loading branch information
mdulko committed Jul 4, 2024
2 parents 0f95505 + 20e643b commit 50ae5d3
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
5.6.13-SNAPSHOT / WIP
==================
Improvements:
* [OLMIS-7954](https://openlmis.atlassian.net/browse/OLMIS-7954): Improved the scalability of local storage and login performance for /api/requisitionGroups request

5.6.12 / 2024-04-19
==================
New functionality that are backwards-compatible:
Expand Down
4 changes: 1 addition & 3 deletions src/referencedata-lot/lot.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@

return lotResource.query(queryParams)
.then(function(lots) {
lots.content.forEach(function(lot) {
lotsOffline.put(lot);
});
lotsOffline.putAll(lots.content);
return lots;
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/referencedata-lot/lot.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('lotService', function() {
beforeEach(function() {

this.offlineService = jasmine.createSpyObj('offlineService', ['isOffline', 'checkConnection']);
this.lotsStorage = jasmine.createSpyObj('lotsStorage', ['put', 'getBy', 'search']);
this.lotsStorage = jasmine.createSpyObj('lotsStorage', ['put', 'putAll', 'getBy', 'search']);

var offlineService = this.offlineService,
lotsStorage = this.lotsStorage;
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('lotService', function() {
this.$rootScope.$apply();

expect(result.content).toEqual(this.lots);
expect(this.lotsStorage.put.callCount).toEqual(4);
expect(this.lotsStorage.putAll.callCount).toEqual(1);
});

it('should get a proper lot from local storage', function() {
Expand Down
19 changes: 11 additions & 8 deletions src/referencedata-orderable-fulfills/orderable-fulfills.run.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,23 @@

loginService.registerPostLoginAction(function() {
orderableFulfillsService.clearOrderableFulfillsOffline();
var homeFacility,
orderableFulfills = [];
var homeFacility;

return facilityFactory.getUserHomeFacility()
.then(function(facility) {
homeFacility = facility;
var programs = homeFacility.supportedPrograms;
programs.forEach(function(program) {
orderableFulfills.push(orderableFulfillsService.query({
programId: program.id ? program.id : program,
facilityId: homeFacility.id ? homeFacility.id : homeFacility
}));
return orderableFulfills;

var supportedProgramsIds = programs.map(function(program) {
return program.id ? program.id : program;
});

var orderableFulfills = orderableFulfillsService.query({
facilityId: homeFacility.id ? homeFacility.id : homeFacility,
programId: supportedProgramsIds
});

return orderableFulfills;
})
.catch(function() {
return $q.resolve();
Expand Down
18 changes: 13 additions & 5 deletions src/referencedata-orderable-fulfills/orderable-fulfills.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,25 @@

return orderableFulfillsResource.query(queryParams)
.then(function(orderableFulfills) {
Object.keys(orderableFulfills).forEach(function(item) {
var orderableFulfillsToStore = Object.keys(
orderableFulfills
).map(function(item) {
if (!isPromiseAttribute(item)) {
var orderableFulfillJson = {
id: item
};
Object.entries(orderableFulfills[item]).forEach(function(entry) {
orderableFulfillJson[entry[0]] = entry[1];
});
orderableFulfillsOffline.put(orderableFulfillJson);
Object.entries(orderableFulfills[item]).forEach(
function(entry) {
orderableFulfillJson[entry[0]] = entry[1];
}
);

return orderableFulfillJson;
}
});

orderableFulfillsOffline.putAll(orderableFulfillsToStore);

return orderableFulfills;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('orderableFulfillsService', function() {
beforeEach(function() {

this.offlineService = jasmine.createSpyObj('offlineService', ['isOffline', 'checkConnection']);
this.orderableFulfillsStorage = jasmine.createSpyObj('orderableFulfillsStorage', ['put', 'getBy']);
this.orderableFulfillsStorage = jasmine.createSpyObj('orderableFulfillsStorage', ['put', 'putAll', 'getBy']);

var offlineService = this.offlineService,
orderableFulfillsStorage = this.orderableFulfillsStorage;
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('orderableFulfillsService', function() {

expect(result['idOne']).toEqual(this.orderableFulfills['idOne']);
expect(result['idTwo']).toEqual(this.orderableFulfills['idTwo']);
expect(this.orderableFulfillsStorage.put.callCount).toEqual(2);
expect(this.orderableFulfillsStorage.putAll.callCount).toEqual(1);
});

it('should reject if orderable fulfills not found in the local storage', function() {
Expand Down
18 changes: 10 additions & 8 deletions src/referencedata-orderable/orderable.run.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
loginService.registerPostLoginAction(function(user) {
return programService.getUserPrograms(user.userId)
.then(function(programs) {
programs.forEach(function(program) {
var params = {
program: program.code
};
return new OrderableResource().getAll(params)
.then(function(orderables) {
return orderables;
});
var supportedProgramsCodes = programs.map(function(program) {
return program.code;
});

return new OrderableResource()
.getAll({
program: supportedProgramsCodes
})
.then(function(orderables) {
return orderables;
});
});
});
}
Expand Down
7 changes: 5 additions & 2 deletions src/referencedata-program/program.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,13 @@
})
.$promise
.then(function(programs) {
programs.forEach(function(program) {
var programsToStore = programs.map(function(program) {
program.userIdOffline = userId;
userProgramsCache.put(program);
return program;
});

userProgramsCache.putAll(programsToStore);

return programs;
})
.catch(function() {
Expand Down
12 changes: 6 additions & 6 deletions src/referencedata-program/program.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
describe('programService', function() {

beforeEach(function() {
this.programsStorage = jasmine.createSpyObj('programsStorage', ['getBy', 'getAll', 'put', 'search']);
this.programsStorage = jasmine.createSpyObj('programsStorage', ['getBy', 'putAll', 'getAll', 'put', 'search']);
this.offlineService = jasmine.createSpyObj('offlineService', ['isOffline', 'checkConnection']);

var programsStorage = this.programsStorage,
Expand Down Expand Up @@ -145,9 +145,6 @@ describe('programService', function() {
expectedProgram1,
expectedProgram2
]));

expect(angular.toJson(this.programsStorage.put.calls[0].args[0])).toEqual(angular.toJson(expectedProgram1));
expect(angular.toJson(this.programsStorage.put.calls[1].args[0])).toEqual(angular.toJson(expectedProgram2));
});

it('should update program and save it to storage', function() {
Expand Down Expand Up @@ -199,8 +196,11 @@ describe('programService', function() {
this.$httpBackend.flush();
this.$rootScope.$apply();

expect(angular.toJson(this.programsStorage.put.calls[0].args[0]))
.toEqual(angular.toJson(_.extend({}, this.program1, this.userIdOffline)));
expect(angular.toJson(this.programsStorage.putAll.calls[0].args[0]))
.toEqual(angular.toJson([
_.extend({}, this.program1, this.userIdOffline),
_.extend({}, this.program2, this.userIdOffline)
]));
});

it('will not cache an unsuccessful request', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
describe('ProgramService getUserPrograms decorator', function() {

beforeEach(function() {
this.cache = jasmine.createSpyObj('cache', ['getBy', 'put', 'clearAll', 'getAll', 'search']);
this.cache = jasmine.createSpyObj('cache', ['getBy', 'put', 'putAll', 'clearAll', 'getAll', 'search']);
var cache = this.cache;
module('referencedata-user');
module('referencedata-user-programs-cache', function($provide) {
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('ProgramService getUserPrograms decorator', function() {
this.$httpBackend.flush();
this.$rootScope.$apply();

expect(this.cache.put.callCount).toEqual(2);
expect(this.cache.putAll.callCount).toEqual(1);
expect(angular.toJson(result)).toEqual(angular.toJson([
_.extend({}, this.programs[0], this.userIdOffline),
_.extend({}, this.programs[1], this.userIdOffline)
Expand Down

0 comments on commit 50ae5d3

Please sign in to comment.