9aec9b4f8babdc980ad1ad60637c450c2025876d
[portal.git] / ecomp-portal-FE-common / client / app / views / admins / add-admin-dialogs / new-admin.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 NewAdminModalCtrl {
24         constructor($log, adminsService, $scope, confirmBoxService, utilsService, $location) {
25
26             let init = () => {
27                 this.isSaving = false;
28                 this.originalApps = [];
29                 /* istanbul ignore if */
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 === 2) {
35                         this.getAdminAppsRoles();
36                     }
37                 } else {
38                     this.isShowBack = true;
39                     this.selectedUser = null;
40                     this.dialogState = 1;
41                 }
42
43                 //this.searchUsersInProgress = false;
44                 //this.showNewAdminAppDropdown = false;
45                 $log.info('NewAdminModalCtrl::initiated');
46                 this.appsOrder = [];
47             };
48
49             let orderList = (apps) => {
50                 this.appsOrder = [];
51                 for (var i = 0; i < apps.length; i++) {
52                     if (apps[i].isAdmin) {
53                         this.appsOrder.push(apps[i].id);
54                     }
55                 }
56             };
57
58             this.orderFilter = app => {
59                 if (!app || !app.id || !this.appsOrder.length) {
60                     return;
61                 }
62                 return this.appsOrder.indexOf(app.id);
63             };
64
65             /**
66              * this function get the selected admin apps roles
67              */
68             this.getAdminAppsRoles = () => {
69                 if (!this.selectedUser || !this.selectedUser.orgUserId) {
70                     $log.error('No user is selected / searchUsers is InProgress');
71                     this.dialogState = 1;
72                     return;
73                 }
74                 adminsService.getAdminAppsRoles(this.selectedUser.orgUserId).then(roles => {
75                     $log.debug('apps roles res: ', JSON.stringify(roles));
76                     if (!roles.appsRoles) {
77                         return;
78                     }
79
80                     this.adminAppsRoles = [];
81                     for (var i = 0; i < roles.appsRoles.length; i++) {
82                         if (!roles.appsRoles[i].restrictedApp) {
83                             $log.debug('pushing: {id: ', roles.appsRoles[i].id,
84                                 'name: ', roles.appsRoles[i].appName,
85                                 'restrictedApp: ', roles.appsRoles[i].restrictedApp,
86                                 'isAdmin: ', roles.appsRoles[i].isAdmin, '}');
87                             this.adminAppsRoles.push({
88                                 id: roles.appsRoles[i].id,
89                                 appName: roles.appsRoles[i].appName,
90                                 isAdmin: roles.appsRoles[i].isAdmin,
91                                 restrictedApp: roles.appsRoles[i].restrictedApp
92                             });
93                         }
94                     }
95                     this.dialogState = 2;
96                     this.adminAppsRoles = this.adminAppsRoles.sort(getSortOrder("appName"));
97
98                     orderList(roles.appsRoles);
99                     if (this.appsOrder != null) {
100                         for (var j = 0; j < this.appsOrder.length; j++) {
101                             this.originalApps.push(this.appsOrder[j]);
102                         }
103                     }
104                 }).catch(err => {
105                     $log.error(err);
106                 });
107             };
108
109             // Refactor this into a directive
110             let getSortOrder = (prop) => {
111                 return function (a, b) {
112                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
113                         return 1;
114                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
115                         return -1;
116                     }
117                     return 0;
118                 }
119             }
120
121             /**
122              * this function set the selected user
123              * @param user: selected user object
124              */
125             this.setSelectedUser = (user) => {
126                 $log.debug('selected user: ', user);
127                 this.selectedUser = user;
128             };
129
130             /**
131              * Mark the user as not admin of the selected app
132              * @param app: selected app object
133              */
134             this.unadminApp = (app) => {
135                 confirmBoxService.deleteItem(app.appName).then(confirmed => {
136                     if (confirmed === true) {
137                         app.isAdmin = false;
138                         this.appsOrder.splice(this.appsOrder.indexOf(app.id), 1);
139                     }
140                 }).catch(err => {
141                     $log(err);
142                 });
143             };
144
145             /**
146              * update the selected admin app with the new roles
147              */
148             this.updateAdminAppsRoles = () => {
149                 if (!this.selectedUser || !this.selectedUser.orgUserId || !this.adminAppsRoles) {
150                     return;
151                 }
152                 this.isSaving = true;
153                 $log.debug('going to update user: ' + this.selectedUser.orgUserId + ' with app roles: ' + JSON.stringify(this.adminAppsRoles));
154                 confirmBoxService.makeAdminChanges('Are you sure you want to make these admin changes?')
155                     .then(confirmed => {
156                         if(confirmed === true){
157                         adminsService.updateAdminAppsRoles({
158                                 orgUserId: this.selectedUser.orgUserId,
159                                 appsRoles: this.adminAppsRoles
160                             })
161                             .then(res => {
162                                 $log.debug('Admin apps roles updated successfully!', res);
163                                 //close and resolve dialog promise with true (to update the table)
164                                 this.remindToAddUserIfNecessary();
165                                 $scope.closeThisDialog(true);
166                             }).catch(err => {
167                             $log.error('NewAdminModalCtrl.updateAdminAppsRoles:: Failed - ' + err);
168                         }).finally(()=> {
169                             this.isSaving = false;
170                         })
171                         }else{
172                                 this.isSaving = false;
173                         }
174                     });
175             };
176
177             /**
178              * Navigate between dialog screens using step number: 1,2,...
179              */
180             this.navigateBack = () => {
181                 if (this.dialogState === 1) {
182                     //back from 1st screen?
183                 }
184                 if (this.dialogState === 2) {
185                     this.dialogState = 1;
186                 }
187             };
188
189             init();
190
191             /**
192              * each time new app is selected in the drop down,
193              * add it to the user administrated apps list
194              */
195             $scope.$watch('newAdmin.selectedNewApp.value', (newVal) => {
196                 var newVal= JSON.parse(newVal);
197                 if (!newVal || newVal.isAdmin === undefined) {
198                     return;
199                 }
200                 //newVal.isAdmin = true; - track by ruined this, here is the workaround:
201                 let app = _.find(this.adminAppsRoles, {id: newVal.id});
202                 if (app) {
203                     app.isAdmin = true;
204                     this.appsOrder.push(app.id);
205                 }
206                 this.selectedNewApp = null;
207                 //this.showNewAdminAppDropdown = false;
208             });
209
210             $scope.$on('$stateChangeStart', e => {
211                 //Disable navigation when modal is opened
212                 //**Nabil - note: this will cause the history back state to be replaced with current state
213                 e.preventDefault();
214             });
215
216             /**
217              * If an Admin was added for an application remind the portal admin to add the admin as a user
218              */
219             this.remindToAddUserIfNecessary = () => {
220
221                 var adminAddedToNewApp = false;
222                 if ((this.originalApps != null) && (this.originalApps.length > 0)) {
223                     for (var i = 0; i < this.appsOrder.length; i++) {
224                         var foundApp = false;
225                         for (var j = 0; j < this.originalApps.length; j++) {
226                             if (this.originalApps[j] === this.appsOrder[i]) {
227                                 foundApp = true;
228                             }
229                         }
230                         if (foundApp === false) {
231                             adminAddedToNewApp = true;
232                             break;
233                         }
234                     }
235                 } else {
236                     adminAddedToNewApp = true;
237                 }
238
239                 if (adminAddedToNewApp === true) {
240                     confirmBoxService.confirm('Add this person as an application user? This allows them to access the application from ECOMP Portal. Press OK to go to the Add Users page.')
241                         .then(confirmed => {
242                             if (confirmed === true) {
243                                 $location.path('/users');
244                             }
245                         });
246                 }
247             }
248
249         }
250     }
251     NewAdminModalCtrl.$inject = ['$log', 'adminsService', '$scope', 'confirmBoxService', 'utilsService', '$location'];
252     angular.module('ecompApp').controller('NewAdminModalCtrl', NewAdminModalCtrl);
253 })();