nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / users / new-user-dialogs / new-user.controller.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20
21 'use strict';
22 (function () {
23     class NewUserModalCtrl {
24         constructor($scope, $log, usersService, applicationsService, confirmBoxService) {
25             let init = () => {
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();
36                     }
37                 }else{
38                     this.isShowBack = true;
39                     this.selectedUser = null;
40                     this.dialogState = 1;
41                 }
42             };
43
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;
49             }
50
51             this.deleteApp = (app) => {
52                 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
53                 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
54                     if(isConfirmed){
55                         app.isChanged = true;
56                         this.anyChanges = true;
57                         app.isDeleted = true;
58                         app.appRoles.forEach(function(role){
59                             role.isApplied = false;
60                         });
61                     }
62                 }).catch(err => {
63                     $log.error('NewUserModalCtrl::deleteApp error: ',err);
64                 });
65             };
66
67             this.getUserAppsRoles = () => {
68                 if (!this.selectedUser || !this.selectedUser.orgUserId) {
69                     $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
70                     this.dialogState = 1;
71                     return;
72                 }
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');
80                         return null;
81                     }
82                     $log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
83                     $log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
84                     this.adminApps = apps;
85                     this.dialogState = 3;
86                     this.userAppRoles = {};
87                     this.numberAppsProcessed = 0;
88                     this.isLoading = true;
89                     apps.forEach(app => {
90                         $log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
91                         app.isChanged = false;
92                         app.isLoading = true;
93                         app.isError = 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;
104
105                         }).catch(err => {
106                             $log.error(err);
107                             app.isError = true;
108                             app.isLoading = false;
109                             app.errorMessage = err.headers('FEErrorString');
110                             $log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
111                         }).finally(()=>{
112                             this.numberAppsProcessed++;
113                             if (this.numberAppsProcessed == this.adminApps.length) {
114                                 this.isLoading = false;
115                             }
116                         });
117                     })
118                     return;
119                 }).catch(err => {
120                     $log.error(err);
121                 })
122
123             }
124
125             this.getAdminApps = () => {
126                 if (!this.selectedUser || !this.selectedUserorgUserId) {
127                     $log.error('NewUserModalCtrl::getAdminApps: No user is selected');
128                     this.dialogState = 1;
129                     return;
130                 }
131                 applicationsService.getAdminApps().promise().then(apps => {
132                     if (!apps || !apps.length) {
133                         $log.error('NewUserModalCtrl::getAdminApps: no apps found');
134                         return null;
135                     }
136                     $log.debug('NewUserModalCtrl::getAdminApps: admin apps: ', apps);
137                     this.adminApps = apps;
138                     this.dialogState = 3;
139                     return;
140                 }).catch(err => {
141                     $log.error('NewUserModalCtrl::getAdminApps: ', err);
142                 })
143
144             }
145
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');
150                     return;
151                 }
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 => {
157                     if (app.isChanged) {
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})
161                         .then(res => {
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++;
166                         }).catch(err => {
167                             $log.error(err);
168                             app.isErrorUpdating = true;
169                         }).finally(()=>{
170                             this.numberAppsProcessed++;
171                             if (this.numberAppsProcessed == this.adminApps.length) {
172                                 this.isSaving = false;
173                             }
174                             if (this.numberAppsSucceeded == this.adminApps.length) {
175                                 $scope.closeThisDialog(true);
176                             }
177                         })
178                     } else {
179                         $log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
180                         app.noChanges = true;
181                         app.isError = false;
182                         this.numberAppsProcessed++;
183                         this.numberAppsSucceeded++;
184                         if (this.numberAppsProcessed == this.adminApps.length) {
185                             this.isSaving = false;
186                         }
187                         if (this.numberAppsSucceeded == this.adminApps.length) {
188                             $scope.closeThisDialog(true);
189                         }
190                     }
191                 });
192             };
193
194             this.navigateBack = () => {
195                 if (this.dialogState === 1) {
196                 }
197                 if (this.dialogState === 3) {
198                     this.dialogState = 1;
199                 }
200             };
201
202             init();
203
204             $scope.$on('$stateChangeStart', e => {
205                 e.preventDefault();
206             });
207         }
208     }
209     NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService'];
210     angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);
211 })();