diff --git a/src/referencedata-orderable-fulfills/orderable-fulfills.run.js b/src/referencedata-orderable-fulfills/orderable-fulfills.run.js deleted file mode 100644 index d4a86f9..0000000 --- a/src/referencedata-orderable-fulfills/orderable-fulfills.run.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 info@OpenLMIS.org.  - */ - -(function() { - - 'use strict'; - - angular - .module('referencedata-orderable-fulfills') - .run(run); - - run.$inject = ['loginService', 'orderableFulfillsService', 'facilityFactory', '$q']; - - function run(loginService, orderableFulfillsService, facilityFactory, $q) { - - loginService.registerPostLoginAction(function() { - orderableFulfillsService.clearOrderableFulfillsOffline(); - var homeFacility; - - return facilityFactory.getUserHomeFacility() - .then(function(facility) { - homeFacility = facility; - var programs = homeFacility.supportedPrograms; - - 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(); - }); - }); - } - -})(); diff --git a/src/referencedata-orderable-fulfills/orderable-fulfills.run.spec.js b/src/referencedata-orderable-fulfills/orderable-fulfills.run.spec.js deleted file mode 100644 index 28f5063..0000000 --- a/src/referencedata-orderable-fulfills/orderable-fulfills.run.spec.js +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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 info@OpenLMIS.org.  - */ - -describe('referencedata-orderable-fulfills run', function() { - - beforeEach(function() { - var context = this; - module('referencedata-orderable-fulfills'); - 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.orderableFulfillsService = $injector.get('orderableFulfillsService'); - this.PageDataBuilder = $injector.get('PageDataBuilder'); - this.facilityFactory = $injector.get('facilityFactory'); - this.programService = $injector.get('programService'); - 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.orderableFulfills = { - idOne: { - canFulfillForMe: [1, 2], - canBeFulfilledByMe: [] - }, - idTwo: { - canFulfillForMe: [], - canBeFulfilledByMe: [] - } - }; - - this.user = new this.UserDataBuilder().build(); - - this.postLoginAction = getLastCall(this.loginServiceSpy.registerPostLoginAction).args[0]; - - spyOn(this.facilityFactory, 'getUserHomeFacility').andReturn(this.$q.resolve(this.homeFacility)); - spyOn(this.orderableFulfillsService, 'query').andReturn(this.$q.resolve(this.orderableFulfills)); - spyOn(this.orderableFulfillsService, 'clearOrderableFulfillsOffline'); - }); - - describe('run block', function() { - - it('should register post login action', function() { - expect(this.loginServiceSpy.registerPostLoginAction).toHaveBeenCalled(); - }); - - }); - - describe('post login action', function() { - - it('should clear orderable fulfills cache', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.orderableFulfillsService.clearOrderableFulfillsOffline).toHaveBeenCalled(); - }); - - it('should get user home facility', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.facilityFactory.getUserHomeFacility).toHaveBeenCalled(); - }); - - it('should get orderable fulfills', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.orderableFulfillsService.query).toHaveBeenCalled(); - }); - }); - - function getLastCall(method) { - return method.calls[method.calls.length - 1]; - } - -}); \ No newline at end of file diff --git a/src/stock-adjustment-creation/source-destination.service.js b/src/stock-adjustment-creation/source-destination.service.js index 667b3f7..ba18887 100644 --- a/src/stock-adjustment-creation/source-destination.service.js +++ b/src/stock-adjustment-creation/source-destination.service.js @@ -94,17 +94,21 @@ } function cacheSources(sources, facilityId) { - sources.forEach(function(source) { + var sourcesToStore = sources.map(function(source) { source.facilityId = facilityId; - offlineSources.put(source); + return source; }); + + offlineSources.putAll(sourcesToStore); } function cacheDestinations(destinations, facilityId) { - destinations.forEach(function(destination) { + var destinationsToStore = destinations.map(function(destination) { destination.facilityId = facilityId; - offlineDestinations.put(destination); + return destination; }); + + offlineDestinations.putAll(destinationsToStore); } function clearSourcesCache() { diff --git a/src/stock-adjustment-creation/source-destination.service.spec.js b/src/stock-adjustment-creation/source-destination.service.spec.js index 46435d7..09f5a27 100644 --- a/src/stock-adjustment-creation/source-destination.service.spec.js +++ b/src/stock-adjustment-creation/source-destination.service.spec.js @@ -18,7 +18,7 @@ describe('sourceDestinationService', function() { beforeEach(function() { this.offlineService = jasmine.createSpyObj('offlineService', ['isOffline', 'checkConnection']); - this.storage = jasmine.createSpyObj('offlineStorage', ['put', 'search', 'getAll']); + this.storage = jasmine.createSpyObj('offlineStorage', ['put', 'putAll', 'search', 'getAll']); var storage = this.storage, offlineService = this.offlineService; diff --git a/src/stock-reason/stock-reasons.factory.js b/src/stock-reason/stock-reasons.factory.js index 99f8c42..fd15db2 100644 --- a/src/stock-reason/stock-reasons.factory.js +++ b/src/stock-reason/stock-reasons.factory.js @@ -146,7 +146,7 @@ function getReasons(programIds, facilityType, reasonType) { return $q.resolve(getReasonsPromise(programIds, facilityType, reasonType) .then(function(reasonAssignments) { - return reasonAssignments + var filteredReasons = reasonAssignments .filter(function(reasonAssignment) { return !reasonAssignment.hidden; }) @@ -154,11 +154,14 @@ if (result.indexOf(reasonAssignment.reason) < 0) { result.push(reasonAssignment.reason); } - if (!offlineService.isOffline()) { - cacheReasons(reasonAssignment, programIds, facilityType); - } return result; }, []); + + if (!offlineService.isOffline()) { + cacheReasons(filteredReasons, programIds, facilityType); + } + + return filteredReasons; })); } @@ -183,18 +186,20 @@ }); } - function cacheReasons(reasonAssignment, programIds, facilityType) { - var reason = angular.copy(reasonAssignment.reason); - var reasonToCache = { - id: reasonAssignment.id, - programId: programIds, - facilityType: facilityType, - reasonType: reason.reasonType, - reason: reason, - hidden: reasonAssignment.hidden - }; + function cacheReasons(filteredReasons, programIds, facilityType) { + var reasonsToCache = filteredReasons.map(function(reason) { + var reasonCopy = angular.copy(reason); + return { + id: reason.id, + programId: programIds, + facilityType: facilityType, + reasonType: reasonCopy.reasonType, + reason: reasonCopy, + hidden: reason.hidden + }; + }); - offlineReasons.put(reasonToCache); + offlineReasons.putAll(reasonsToCache); } function clearReasonsCache() { diff --git a/src/stock-reason/stock-reasons.factory.spec.js b/src/stock-reason/stock-reasons.factory.spec.js index 39d38e3..3e0d53b 100644 --- a/src/stock-reason/stock-reasons.factory.spec.js +++ b/src/stock-reason/stock-reasons.factory.spec.js @@ -20,7 +20,7 @@ describe('stockReasonsFactory', function() { beforeEach(function() { module('stock-reasons-modal', function($provide) { validReasonResourceMock = jasmine.createSpyObj('validReasonResource', ['query']); - reasonsStorage = jasmine.createSpyObj('offlineReasons', ['search', 'getAll', 'put']); + reasonsStorage = jasmine.createSpyObj('offlineReasons', ['search', 'getAll', 'putAll', 'put']); reasonsStorage.getAll.andReturn([false]); $provide.factory('ValidReasonResource', function() { diff --git a/src/stock-valid-destinations/destination-cache.run.js b/src/stock-valid-destinations/destination-cache.run.js deleted file mode 100644 index c3a8b24..0000000 --- a/src/stock-valid-destinations/destination-cache.run.js +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 info@OpenLMIS.org.  - */ - -(function() { - - 'use strict'; - - angular - .module('stock-valid-destinations') - .run(routes); - - routes.$inject = ['loginService', 'sourceDestinationService', 'facilityFactory', '$q']; - - function routes(loginService, sourceDestinationService, facilityFactory, $q) { - - loginService.registerPostLoginAction(function() { - sourceDestinationService.clearDestinationsCache(); - var homeFacility; - - return facilityFactory.getUserHomeFacility() - .then(function(facility) { - homeFacility = facility; - var programs = homeFacility.supportedPrograms; - var supportedProgramsIds = programs.map(function(program) { - return program.id ? program.id : program; - }); - - var destinations = sourceDestinationService.getDestinationAssignments( - supportedProgramsIds, - homeFacility.id ? homeFacility.id : homeFacility - ); - - return destinations; - }) - .catch(function() { - return $q.resolve(); - }); - }); - } - -})(); \ No newline at end of file diff --git a/src/stock-valid-destinations/destination-cache.run.spec.js b/src/stock-valid-destinations/destination-cache.run.spec.js deleted file mode 100644 index 1b72a1e..0000000 --- a/src/stock-valid-destinations/destination-cache.run.spec.js +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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 info@OpenLMIS.org.  - */ - -describe('destination-cache run', function() { - - beforeEach(function() { - var context = this; - module('stock-valid-destinations'); - 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.destinationAssignments = [{ - id: 'dest-one', - facilityTypeId: 'fac-type-id-one', - programid: 'program-id-one' - }, { - id: 'dest-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, 'getDestinationAssignments') - .andReturn(this.$q.resolve(this.destinationAssignments)); - spyOn(this.sourceDestinationService, 'clearDestinationsCache'); - }); - - describe('run block', function() { - - it('should register post login action', function() { - expect(this.loginServiceSpy.registerPostLoginAction).toHaveBeenCalled(); - }); - - }); - - describe('post login action', function() { - - it('should clear valid destinations cache', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.sourceDestinationService.clearDestinationsCache).toHaveBeenCalled(); - }); - - it('should get user home facility', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.facilityFactory.getUserHomeFacility).toHaveBeenCalled(); - }); - - it('should not get destinations if user has no home facility', function() { - this.facilityFactory.getUserHomeFacility.andReturn(this.$q.reject()); - - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.sourceDestinationService.getDestinationAssignments).not.toHaveBeenCalled(); - }); - - it('should get valid destinations', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.sourceDestinationService.getDestinationAssignments).toHaveBeenCalled(); - }); - - }); - - function getLastCall(method) { - return method.calls[method.calls.length - 1]; - } - -}); \ No newline at end of file diff --git a/src/stock-valid-sources/source-cache.run.js b/src/stock-valid-sources/source-cache.run.js deleted file mode 100644 index f873ccf..0000000 --- a/src/stock-valid-sources/source-cache.run.js +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 info@OpenLMIS.org.  - */ - -(function() { - - 'use strict'; - - angular - .module('stock-valid-sources') - .run(routes); - - routes.$inject = ['loginService', 'sourceDestinationService', 'facilityFactory', '$q']; - - function routes(loginService, sourceDestinationService, facilityFactory, $q) { - - loginService.registerPostLoginAction(function() { - sourceDestinationService.clearSourcesCache(); - var homeFacility; - - return facilityFactory.getUserHomeFacility() - .then(function(facility) { - homeFacility = facility; - var programs = homeFacility.supportedPrograms; - var supportedProgramsIds = programs.map(function(program) { - return program.id ? program.id : program; - }); - - var sources = sourceDestinationService.getSourceAssignments( - supportedProgramsIds, - homeFacility.id ? homeFacility.id : homeFacility - ); - - return sources; - }) - .catch(function() { - return $q.resolve(); - }); - }); - } - -})(); \ No newline at end of file diff --git a/src/stock-valid-sources/source-cache.run.spec.js b/src/stock-valid-sources/source-cache.run.spec.js deleted file mode 100644 index 1410515..0000000 --- a/src/stock-valid-sources/source-cache.run.spec.js +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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 info@OpenLMIS.org.  - */ - -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, 'getSourceAssignments') - .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 clear valid sources cache', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.sourceDestinationService.clearSourcesCache).toHaveBeenCalled(); - }); - - it('should get user home facility', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.facilityFactory.getUserHomeFacility).toHaveBeenCalled(); - }); - - it('should not get sources if user has no home facility', function() { - this.facilityFactory.getUserHomeFacility.andReturn(this.$q.reject()); - - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.sourceDestinationService.getSourceAssignments).not.toHaveBeenCalled(); - }); - - it('should get valid sources', function() { - this.postLoginAction(this.user); - this.$rootScope.$apply(); - - expect(this.sourceDestinationService.getSourceAssignments).toHaveBeenCalled(); - }); - - }); - - function getLastCall(method) { - return method.calls[method.calls.length - 1]; - } - -}); \ No newline at end of file