Skip to content

Commit

Permalink
Merge pull request #165 from OpenLMIS-Angola/OAM-290
Browse files Browse the repository at this point in the history
OAM-290: Made issue work offline
  • Loading branch information
olewandowski1 authored Aug 7, 2024
2 parents 63ab98c + b47b362 commit efa617c
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@
onSubmit();
} catch (error) {
loadingModalService.close();
// eslint-disable-next-line no-console
console.error(error.message);
alertService.error('openlmisStateChangeError.internalApplicationError.message');
}
Expand Down
47 changes: 45 additions & 2 deletions src/stock-adjustment-creation/source-destination.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
this.getDestinationAssignments = getDestinationAssignments;
this.clearSourcesCache = clearSourcesCache;
this.clearDestinationsCache = clearDestinationsCache;
this.cacheDestinationsAndSources = cacheDestinationsAndSources;

function getSourceAssignments(programIds, facilityId) {
var resource = $resource(stockmanagementUrlFactory('/api/validSources'));
Expand All @@ -58,7 +59,6 @@
page: 0,
size: 2147483647
}).$promise.then(function(validSourcesPage) {
cacheSources(validSourcesPage.content, facilityId);
return $q.resolve(validSourcesPage.content);
});
}
Expand All @@ -80,7 +80,6 @@
page: 0,
size: 2147483647
}).$promise.then(function(validDestinationsPage) {
cacheDestinations(validDestinationsPage.content, facilityId);
return $q.resolve(validDestinationsPage.content);
});
}
Expand All @@ -93,6 +92,50 @@
return $q.resolve(array);
}

function cacheDestinationsAndSources(programIds, facilityId) {
var destinationsResource = $resource(stockmanagementUrlFactory('/api/validDestinations'));
var sourcesResource = $resource(stockmanagementUrlFactory('/api/validSources'));

var sourcesPormises = programIds.map(function(programId) {
return sourcesResource.get({
programId: programId,
facilityId: facilityId,
page: 0,
size: 2147483647
}).$promise;
});

var destinationsPormises = programIds.map(function(programId) {
return destinationsResource.get({
programId: programId,
facilityId: facilityId,
page: 0,
size: 2147483647
}).$promise;
});

$q.all(sourcesPormises.concat(destinationsPormises)).then(function(responses) {
cacheResponses(responses, facilityId);
});
}

function cacheResponses(responses, facilityId) {
var threshold = Math.floor(responses.length / 2);
var sourcesToStore = [];
var destinationsToStore = [];

responses.forEach(function(response, index) {
var pageContent = response.content;
if (index < threshold) {
sourcesToStore = sourcesToStore.concat(pageContent);
return;
}
destinationsToStore = destinationsToStore.concat(pageContent);
});
cacheSources(sourcesToStore, facilityId);
cacheDestinations(destinationsToStore, facilityId);
}

function cacheSources(sources, facilityId) {
var sourcesToStore = sources.map(function(source) {
source.facilityId = facilityId;
Expand Down
51 changes: 51 additions & 0 deletions src/stock-valid-sources/source-cache.run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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';

angular
.module('stock-valid-sources')
.run(routes);

routes.$inject = ['loginService', 'sourceDestinationService', 'facilityFactory', '$q', 'programService',
'authorizationService'];

function routes(loginService, sourceDestinationService, facilityFactory, $q, programService,
authorizationService) {

loginService.registerPostLoginAction(function() {
var userId = authorizationService.getUser().user_id;
var userProgramsPromise = programService.getUserPrograms(userId);
var homeFacilityPromise = facilityFactory.getUserHomeFacility();

$q.all([userProgramsPromise, homeFacilityPromise]).then(function(responses) {
var userProgramsIds = responses[0].map(function(program) {
return program.id;
});
var homeFacility = responses[1];
sourceDestinationService.cacheDestinationsAndSources(userProgramsIds, homeFacility.id);
});
});

loginService.registerPostLogoutAction(function() {
return $q.all([
sourceDestinationService.clearSourcesCache(),
sourceDestinationService.clearDestinationsCache()
]);
});
}
})();
91 changes: 91 additions & 0 deletions src/stock-valid-sources/source-cache.run.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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('source-cache run', function() {

beforeEach(function() {
var context = this;
module('stock-valid-sources');
module('referencedata-user', function($provide) {
context.loginServiceSpy = jasmine.createSpyObj('loginService', [
'registerPostLoginAction', 'registerPostLogoutAction'
]);
$provide.value('loginService', context.loginServiceSpy);
});

inject(function($injector) {
this.$rootScope = $injector.get('$rootScope');
this.$q = $injector.get('$q');
this.sourceDestinationService = $injector.get('sourceDestinationService');
this.facilityFactory = $injector.get('facilityFactory');
this.ProgramDataBuilder = $injector.get('ProgramDataBuilder');
this.FacilityDataBuilder = $injector.get('FacilityDataBuilder');
this.UserDataBuilder = $injector.get('UserDataBuilder');
});

this.program1 = new this.ProgramDataBuilder().build();
this.program2 = new this.ProgramDataBuilder().build();

this.programs = [
this.program1,
this.program2
];

this.homeFacility = new this.FacilityDataBuilder()
.withSupportedPrograms(this.programs)
.build();

this.user = new this.UserDataBuilder().build();

this.sourceAssignments = [{
id: 'source-one',
facilityTypeId: 'fac-type-id-one',
programid: 'program-id-one'
}, {
id: 'source-two',
facilityTypeId: 'fac-type-id-two',
programid: 'program-id-two'
}];

this.postLoginAction = getLastCall(this.loginServiceSpy.registerPostLoginAction).args[0];

spyOn(this.facilityFactory, 'getUserHomeFacility').andReturn(this.$q.resolve(this.homeFacility));
spyOn(this.sourceDestinationService, 'cacheDestinationsAndSources')
.andReturn(this.$q.resolve(this.sourceAssignments));
spyOn(this.sourceDestinationService, 'clearSourcesCache');
});

describe('run block', function() {

it('should register post login action', function() {
expect(this.loginServiceSpy.registerPostLoginAction).toHaveBeenCalled();
});

});

describe('post login action', function() {
it('should get user home facility', function() {
this.postLoginAction(this.user);
this.$rootScope.$apply();

expect(this.facilityFactory.getUserHomeFacility).toHaveBeenCalled();
});
});

function getLastCall(method) {
return method.calls[method.calls.length - 1];
}

});

0 comments on commit efa617c

Please sign in to comment.