[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-FE-common / 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  * Created by nnaffar on 12/20/15.
22  */
23 'use strict';
24 (function () {
25     class NewUserModalCtrl {
26         constructor($scope, $log, usersService, applicationsService, confirmBoxService) {
27             var extRequestValue = false;
28             let init = () => {
29                 //$log.info('NewUserModalCtrl::init');
30                 this.isSaving = false;
31                 this.anyChanges = false;
32                 this.adminApps = [];
33                 this.isGettingAdminApps = false;
34                 if($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState){
35                     this.selectedUser = $scope.ngDialogData.selectedUser;
36                     this.dialogState = $scope.ngDialogData.dialogState;
37                     this.isShowBack = false;
38                     if(this.dialogState === 3){
39                         this.getUserAppsRoles();
40                     }
41                 }else{
42                     this.isShowBack = true;
43                     this.selectedUser = null;
44                     this.dialogState = 1;
45                 }
46             };
47
48             this.appChanged = (index) => {
49                 let myApp = this.adminApps[index];
50                 //$log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
51                 myApp.isChanged = true;
52                 this.anyChanges = true;
53             }
54
55             this.deleteApp = (app) => {
56                 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
57                 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
58                     if(isConfirmed){
59                         this.anyChanges = true;
60                         app.isChanged = true;
61                         app.isDeleted = true; // use this to hide the app in the display
62                         app.appRoles.forEach(function(role){
63                             role.isApplied = false;
64                         });
65                     }
66                 }).catch(err => {
67                     $log.error('NewUserModalCtrl::deleteApp error: ',err);
68                     confirmBoxService.showInformation('There was a problem deleting the the applications. ' +
69                         'Please try again later. Error: ' + err.status).then(isConfirmed => {});
70                 });
71             };
72
73             this.getUserAppsRoles = () => {
74                 if (!this.selectedUser || !this.selectedUser.orgUserId) {
75                     $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
76                     this.dialogState = 1;
77                     return;
78                 }
79                 //$log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
80                 this.isGettingAdminApps = true;
81                 applicationsService.getAdminAppsSimpler().then((apps) => {
82                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
83                     this.isGettingAdminApps = false;
84                     if (!apps || !apps.length) {
85                         $log.error('NewUserModalCtrl::getUserAppsRoles error: no admin apps found');
86                         return null;
87                     }
88                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
89                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
90                     this.adminApps = apps;
91                     this.dialogState = 3;
92                     this.userAppRoles = {};
93                     this.numberAppsProcessed = 0;
94                     this.isLoading = true;
95                     apps.forEach(app => {
96                         //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
97                         // Keep track of which app has changed, so we know which apps to update using a BE API
98                         app.isChanged = false;
99                         // Each of these specifies a state, which corresponds to a different message and style that gets displayed
100                         app.isLoading = true;
101                         app.isError = false;
102                         app.isDeleted = false;
103                         app.printNoChanges = false;
104                         app.isUpdating = false;
105                         app.isErrorUpdating = false;
106                         app.isDoneUpdating = false;
107                         app.errorMessage = "";
108                         usersService.getUserAppRoles(app.id, this.selectedUser.orgUserId, extRequestValue).promise().then((userAppRolesResult) => {
109                             //$log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
110                             app.appRoles = userAppRolesResult;
111                             app.isLoading = false;
112
113                         }).catch(err => {
114                             $log.error(err);
115                             app.isError = true;
116                             app.isLoading = false;
117                             app.errorMessage = err.headers('FEErrorString');
118                             //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
119                         }).finally(()=>{
120                             this.numberAppsProcessed++;
121                             if (this.numberAppsProcessed === this.adminApps.length) {
122                                 this.isLoading = false;
123                             }
124                         });
125                     })
126                     return;
127                 }).catch(err => {
128                     $log.error(err);
129                 })
130
131             }
132
133             /**
134              * Update the selected user apps with the new roles.
135              * If no roles remain, set the user to inactive.
136              */
137             this.updateUserAppsRoles = () => {
138                 // $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
139                 if(!this.selectedUser || !this.selectedUser.orgUserId || !this.adminApps){
140                     $log.error('NewUserModalCtrl::updateUserAppsRoles: mmissing arguments');
141                     return;
142                 }
143                 this.isSaving = true;
144                 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.orgUserId);
145                 this.numberAppsProcessed = 0;
146                 this.numberAppsSucceeded = 0;
147                 this.adminApps.forEach(app => {
148                     if (app.isChanged) {
149                         //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
150                         app.isUpdating = true;
151                         var newUserAppRoles = {
152                                 orgUserId: this.selectedUser.orgUserId,
153                                 appId: app.id, 
154                                 appRoles: app.appRoles,
155                                 appName: app.name
156                         }; 
157                         usersService.updateUserAppRoles(newUserAppRoles).promise()
158                         .then(res => {
159                             //$log.debug('NewUserModalCtrl::updateUserAppsRoles: User app roles updated successfully on app: ',app.id);
160                             app.isUpdating = false;
161                             app.isDoneUpdating = true;
162                             this.numberAppsSucceeded++;
163                         }).catch(err => {
164                             $log.error(err);
165                             app.isErrorUpdating = true;
166                             confirmBoxService.showInformation(
167                                         'Failed to update the user application roles: ' + err.status)
168                                         .then(isConfirmed => {});
169                         }).finally(()=>{
170                             this.numberAppsProcessed++;
171                             if (this.numberAppsProcessed === this.adminApps.length) {
172                                 this.isSaving = false; // hide the spinner
173                             }
174                             if (this.numberAppsSucceeded === this.adminApps.length) {
175                                 $scope.closeThisDialog(true);//close and resolve dialog promise with true (to update the table)
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; //remove the error message; just show the No Changes messages
182                         this.numberAppsProcessed++;
183                         this.numberAppsSucceeded++;
184                         if (this.numberAppsProcessed === this.adminApps.length) {
185                             this.isSaving = false; // hide the spinner
186                         }
187                         if (this.numberAppsSucceeded === this.adminApps.length) {
188                             $scope.closeThisDialog(true);//close and resolve dialog promise with true (to update the table)
189                         }
190                     }
191                 });
192             };
193
194             /**
195              * Navigate between dialog screens using step number: 1,2,...
196              */
197             this.navigateBack = () => {
198                 if (this.dialogState === 1) {
199                     //back from 1st screen?
200                 }
201                 if (this.dialogState === 3) {
202                     this.dialogState = 1;
203                 }
204             };
205
206             init();
207
208             $scope.$on('$stateChangeStart', e => {
209                 //Disable navigation when modal is opened
210                 //**Nabil - note: this will cause the history back state to be replaced with current state
211                 e.preventDefault();
212             });
213         }
214     }
215     NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService'];
216     angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);
217 })();