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

OAM-218: merge requests on preload #143

Merged
merged 4 commits into from
Jun 27, 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
54 changes: 54 additions & 0 deletions src/referencedata-orderable-fulfills/orderable-fulfills.run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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('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();
});
});
}

})();
108 changes: 108 additions & 0 deletions src/referencedata-orderable-fulfills/orderable-fulfills.run.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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('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];
}

});
46 changes: 46 additions & 0 deletions src/referencedata-orderable/orderable.run.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';

angular
.module('referencedata-orderable')
.run(routes);

routes.$inject = ['loginService', 'OrderableResource', 'programService'];

function routes(loginService, OrderableResource, programService) {

loginService.registerPostLoginAction(function(user) {
return programService.getUserPrograms(user.userId)
.then(function(programs) {
var supportedProgramsCodes = programs.map(function(program) {
return program.code;
});

return new OrderableResource()
.getAll({
program: supportedProgramsCodes
})
.then(function(orderables) {
return orderables;
});
});
});
}

})();
89 changes: 89 additions & 0 deletions src/referencedata-orderable/orderable.run.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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('orderable run', function() {

beforeEach(function() {
var context = this;
module('referencedata-user');
module('referencedata-orderable', 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.OrderableResource = $injector.get('OrderableResource');
this.OrderableDataBuilder = $injector.get('OrderableDataBuilder');
this.programService = $injector.get('programService');
this.ProgramDataBuilder = $injector.get('ProgramDataBuilder');
this.UserDataBuilder = $injector.get('UserDataBuilder');
});

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

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

this.orderables = [
new this.OrderableDataBuilder().build(),
new this.OrderableDataBuilder().build()
];

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

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

spyOn(this.programService, 'getUserPrograms').andReturn(this.$q.resolve(this.programs));
spyOn(this.OrderableResource.prototype, 'getAll').andReturn(this.$q.when(this.orderables));
});

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 programs', function() {
this.user.userId = this.user.id;
this.postLoginAction(this.user);
this.$rootScope.$apply();

expect(this.programService.getUserPrograms).toHaveBeenCalledWith(this.user.id);
});

it('should get orderables', function() {
this.postLoginAction(this.user);
this.$rootScope.$apply();

expect(this.OrderableResource.prototype.getAll).toHaveBeenCalled();
});
});

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

});
Loading
Loading