Skip to content

Commit

Permalink
OAM-190: create valid source and destination when creating organization
Browse files Browse the repository at this point in the history
  • Loading branch information
olewandowski1 authored and DominikNoga committed Jun 3, 2024
1 parent 7e584ac commit fe07899
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 38 deletions.
9 changes: 7 additions & 2 deletions src/organization-add/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"organizationAdd.create": "Create",
"organizationAdd.organizationName": "Organization Name",
"organizationAdd.disabled": "Disabled",
"organizationAdd.confirmationPrompt": "Are you sure you want to create organization?",
"organizationAdd.confirmationPrompt": "Are you sure you want to create this organization? This action will also automatically create a Valid Destination and a Valid Source.",
"organizationAdd.organizationCreated": "Organization created successfully.",
"organizationAdd.organizationCreateError": "Error while creating organization."
"organizationAdd.organizationCreateError": "Error while creating organization.",
"organizationAdd.validSourceCreateError": "Error while creating Valid Source.",
"organizationAdd.validDestinationCreateError": "Error while creating Valid Destination.",
"organizationAdd.program": "Program",
"organizationAdd.facilityType": "Facility Type",
"organizationAdd.geoLevelAffinity": "Geo Level Affinity"
}
194 changes: 185 additions & 9 deletions src/organization-add/organization-add.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@
.controller('OrganizationAddController', OrganizationAddController);

OrganizationAddController.$inject = [
'$q',
'ValidDestinationResource',
'ValidSourceResource',
'stateTrackerService',
'confirmService',
'organizationService',
'notificationService',
'loadingModalService'
'loadingModalService',
'programs',
'facilityTypes',
'geoLevels'
];

/**
Expand All @@ -37,18 +43,50 @@
* Controller for adding organizations.
*/
function OrganizationAddController(
$q,
ValidDestinationResource,
ValidSourceResource,
stateTrackerService,
confirmService,
organizationService,
notificationService,
loadingModalService
loadingModalService,
programs,
facilityTypes,
geoLevels
) {

var vm = this;

vm.$onInit = onInit;
vm.onSubmit = onSubmit;
vm.createOrganization = createOrganization;
vm.createValidSource = createValidSource;
vm.createValidDestination = createValidDestination;
vm.goToPreviousState = stateTrackerService.goToPreviousState;

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
* @type {ValidSourceResource}
* @name validSourceResource
*
* @description
* Resource for handling valid sources.
*/
vm.validSourceResource = null;

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
* @type {ValidDestinationResource}
* @name validDestinationResource
*
* @description
* Resource for handling valid destinations.
*/
vm.validDestinationResource = null;

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
Expand All @@ -57,7 +95,89 @@
* @description
* Organization to be created.
*/
vm.organization = null;
vm.createdOrganization = null;

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
* @type {Array}
* @name programs
*
* @description
* List of available programs.
*/
vm.programs = [];

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
* @type {Array}
* @name facilityTypes
*
* @description
* List of available facility types.
*/
vm.facilityTypes = [];

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
* @type {Array}
* @name geoLevels
*
* @description
* List of available geo levels.
*/
vm.geoLevels = [];

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
* @type {Object}
* @name program
*
* @description
* Selected program.
*/
vm.program = null;

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
* @type {Object}
* @name facilityType
*
* @description
* Selected facility type.
*/
vm.facilityType = null;

/**
* @ngdoc property
* @propertyOf organization-add.controller:OrganizationAddController
* @type {Object}
* @name geoLevel
*
* @description
* Selected geo level.
*/
vm.geoLevel = null;

/**
* @ngdoc method
* @methodOf organization-add.controller:OrganizationAddController
* @name onInit
*
* @description
* Initialization method of the OrganizationAddController.
*/
function onInit() {
vm.validSourceResource = new ValidSourceResource();
vm.validDestinationResource = new ValidDestinationResource();
vm.programs = programs;
vm.facilityTypes = facilityTypes;
vm.geoLevels = geoLevels;
}

/**
* @ngdoc method
Expand All @@ -68,19 +188,75 @@
* Creates the organization.
*/
function createOrganization() {
return organizationService.createNewOrganization(vm.createdOrganization)
.then(function(organization) {
return organization;
})
.catch(function() {
notificationService.error('organizationAdd.organizationCreateError');
});
}

function createPayload(refId) {
var payload = {
programId: vm.program.id,
facilityTypeId: vm.facilityType.id,
node: {
referenceId: refId
}
};

if (vm.geoLevel) {
payload.geoLevelAffinityId = vm.geoLevel.id;
}

return payload;
}

function createValidSource(refId) {
var payload = createPayload(refId);

return vm.validSourceResource.create(payload)
.then(function(validSource) {
return validSource;
})
.catch(function() {
notificationService.error('organizationAdd.validSourceCreateError');
});
}

function createValidDestination(refId) {
var payload = createPayload(refId);

return vm.validDestinationResource.create(payload)
.then(function(validDestination) {
return validDestination;
})
.catch(function() {
notificationService.error('organizationAdd.validDestinationCreateError');
});
}

function onSubmit() {
confirmService.confirm('organizationAdd.confirmationPrompt', 'organizationAdd.create')
.then(function() {
loadingModalService.open();

organizationService.createNewOrganization(vm.createdOrganization)
.then(function() {
notificationService.success('organizationAdd.organizationCreated');
loadingModalService.close();
stateTrackerService.goToPreviousState();
createOrganization()
.then(function(organization) {
$q.all([
createValidSource(organization.id),
createValidDestination(organization.id)
])
.then(function() {
notificationService.success('organizationAdd.organizationCreated');
loadingModalService.close();
stateTrackerService.goToPreviousState();
});
})
.catch(function() {
loadingModalService.close();
notificationService.error('organizationAdd.organizationCreateError');
loadingModalService.close();
});
});
}
Expand Down
90 changes: 90 additions & 0 deletions src/organization-add/organization-add.controller.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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('OrganizationAddController', function() {

beforeEach(function() {

module('organization-add');

inject(function($injector) {
this.$controller = $injector.get('$controller');
this.$rootScope = $injector.get('$rootScope');
this.$state = $injector.get('$state');
this.$q = $injector.get('$q');
this.organizationService = $injector.get('organizationService');
this.confirmService = $injector.get('confirmService');
this.loadingModalService = $injector.get('loadingModalService');
this.notificationService = $injector.get('notificationService');
this.stateTrackerService = $injector.get('stateTrackerService');
});

this.programs = ['Program 1'];
this.facilityTypes = [{
id: 1
}];
this.geoLevels = [{
id: 1
}];

this.controller = this.$controller('OrganizationAddController', {
programs: this.programs,
facilityTypes: this.facilityTypes,
geoLevels: this.geoLevels
});
});

describe('onSubmit', function() {

beforeEach(function() {
this.organizationId = '123';
this.validSource = {
id: '456'
};
this.validDestination = {
id: '789'
};

this.confirmDeferred = this.$q.defer();
this.saveDeferred = this.$q.defer();
this.loadingDeferred = this.$q.defer();

spyOn(this.confirmService, 'confirm').andReturn(this.confirmDeferred.promise);
spyOn(this.stateTrackerService, 'goToPreviousState').andCallFake(this.loadingDeferred.resolve);
spyOn(this.$state, 'go');
spyOn(this.loadingModalService, 'open').andReturn(this.loadingDeferred.promise);
spyOn(this.loadingModalService, 'close').andCallFake(this.loadingDeferred.resolve);
spyOn(this.notificationService, 'success');
spyOn(this.notificationService, 'error');
});

it('should show notification if organization creation has failed', function() {
this.controller.program = this.programs[0];
this.controller.facilityType = this.facilityTypes[0];
this.controller.geoLevel = null;
this.controller.$onInit();

this.controller.onSubmit();

this.confirmDeferred.resolve();
this.saveDeferred.reject();
this.$rootScope.$apply();

expect(this.notificationService.error).toHaveBeenCalledWith('organizationAdd.organizationCreateError');
expect(this.loadingModalService.close).toHaveBeenCalled();
});
});

});
Loading

0 comments on commit fe07899

Please sign in to comment.