2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
23 class NewUserModalCtrl {
24 constructor($scope, $log, usersService, applicationsService, confirmBoxService) {
26 $log.info('NewUserModalCtrl::init');
27 this.isSaving = false;
28 this.anyChanges = false;
29 this.isGettingAdminApps = false;
30 if($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState){
31 this.selectedUser = $scope.ngDialogData.selectedUser;
32 this.dialogState = $scope.ngDialogData.dialogState;
33 this.isShowBack = false;
34 if(this.dialogState === 3){
35 this.getUserAppsRoles();
38 this.isShowBack = true;
39 this.selectedUser = null;
44 this.appChanged = (index) => {
45 let myApp = this.adminApps[index];
46 $log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
47 myApp.isChanged = true;
48 this.anyChanges = true;
51 this.deleteApp = (app) => {
52 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
53 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
56 this.anyChanges = true;
58 app.appRoles.forEach(function(role){
59 role.isApplied = false;
63 $log.error('NewUserModalCtrl::deleteApp error: ',err);
67 this.getUserAppsRoles = () => {
68 if (!this.selectedUser || !this.selectedUser.orgUserId) {
69 $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
73 $log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
74 this.isGettingAdminApps = true;
75 applicationsService.getAdminAppsSimpler().then((apps) => {
76 $log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
77 this.isGettingAdminApps = false;
78 if (!apps || !apps.length) {
79 $log.error('NewUserModalCtrl::getUserAppsRoles error: no apps found');
82 $log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
83 $log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
84 this.adminApps = apps;
86 this.userAppRoles = {};
87 this.numberAppsProcessed = 0;
88 this.isLoading = true;
90 $log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
91 app.isChanged = false;
94 app.isDeleted = false;
95 app.printNoChanges = false;
96 app.isUpdating = false;
97 app.isErrorUpdating = false;
98 app.isDoneUpdating = false;
99 app.errorMessage = "";
100 usersService.getUserAppRoles(app.id, this.selectedUser.orgUserId).then((userAppRolesResult) => {
101 $log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
102 app.appRoles = userAppRolesResult;
103 app.isLoading = false;
108 app.isLoading = false;
109 app.errorMessage = err.headers('FEErrorString');
110 $log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
112 this.numberAppsProcessed++;
113 if (this.numberAppsProcessed == this.adminApps.length) {
114 this.isLoading = false;
125 this.getAdminApps = () => {
126 if (!this.selectedUser || !this.selectedUserorgUserId) {
127 $log.error('NewUserModalCtrl::getAdminApps: No user is selected');
128 this.dialogState = 1;
131 applicationsService.getAdminApps().promise().then(apps => {
132 if (!apps || !apps.length) {
133 $log.error('NewUserModalCtrl::getAdminApps: no apps found');
136 $log.debug('NewUserModalCtrl::getAdminApps: admin apps: ', apps);
137 this.adminApps = apps;
138 this.dialogState = 3;
141 $log.error('NewUserModalCtrl::getAdminApps: ', err);
146 this.updateUserAppsRoles = () => {
147 $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
148 if(!this.selectedUser || !this.selectedUser.orgUserId || !this.adminApps){
149 $log.debug('NewUserModalCtrl::updateUserAppsRoles: returning early');
152 this.isSaving = true;
153 $log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.orgUserId);
154 this.numberAppsProcessed = 0;
155 this.numberAppsSucceeded = 0;
156 this.adminApps.forEach(app => {
158 $log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
159 app.isUpdating = true;
160 usersService.updateUserAppRoles({orgUserId: this.selectedUser.orgUserId, appId: app.id, appRoles: app.appRoles})
162 $log.debug('NewUserModalCtrl::updateUserAppsRoles: User app roles updated successfully on app: ',app.id);
163 app.isUpdating = false;
164 app.isDoneUpdating = true;
165 this.numberAppsSucceeded++;
168 app.isErrorUpdating = true;
170 this.numberAppsProcessed++;
171 if (this.numberAppsProcessed == this.adminApps.length) {
172 this.isSaving = false;
174 if (this.numberAppsSucceeded == this.adminApps.length) {
175 $scope.closeThisDialog(true);
179 $log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
180 app.noChanges = true;
182 this.numberAppsProcessed++;
183 this.numberAppsSucceeded++;
184 if (this.numberAppsProcessed == this.adminApps.length) {
185 this.isSaving = false;
187 if (this.numberAppsSucceeded == this.adminApps.length) {
188 $scope.closeThisDialog(true);
194 this.navigateBack = () => {
195 if (this.dialogState === 1) {
197 if (this.dialogState === 3) {
198 this.dialogState = 1;
204 $scope.$on('$stateChangeStart', e => {
209 NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService'];
210 angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);