nexus site path corrected
[portal.git] / ecomp-portal-FE / 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                 $log.info('NewAdminModalCtrl::initiated');
44                 this.appsOrder = [];
45             };
46
47             let orderList = (apps) => {
48                 this.appsOrder = [];
49                 for (var i = 0; i < apps.length; i++) {
50                     if (apps[i].isAdmin) {
51                         this.appsOrder.push(apps[i].id);
52                     }
53                 }
54             };
55
56             this.orderFilter = app => {
57                 if (!app || !app.id || !this.appsOrder.length) {
58                     return;
59                 }
60                 return this.appsOrder.indexOf(app.id);
61             };
62
63             this.getAdminAppsRoles = () => {
64                 if (!this.selectedUser || !this.selectedUser.orgUserId) {
65                     $log.error('No user is selected / searchUsers is InProgress');
66                     this.dialogState = 1;
67                     return;
68                 }
69                 adminsService.getAdminAppsRoles(this.selectedUser.orgUserId).then(roles => {
70                     $log.debug('apps roles res: ', JSON.stringify(roles));
71                     if (!roles.appsRoles) {
72                         return;
73                     }
74
75                     this.adminAppsRoles = [];
76                     for (var i = 0; i < roles.appsRoles.length; i++) {
77                         if (!roles.appsRoles[i].restrictedApp) {
78                             $log.debug('pushing: {id: ', roles.appsRoles[i].id,
79                                 'name: ', roles.appsRoles[i].appName,
80                                 'restrictedApp: ', roles.appsRoles[i].restrictedApp,
81                                 'isAdmin: ', roles.appsRoles[i].isAdmin, '}');
82                             this.adminAppsRoles.push({
83                                 id: roles.appsRoles[i].id,
84                                 appName: roles.appsRoles[i].appName,
85                                 isAdmin: roles.appsRoles[i].isAdmin,
86                                 restrictedApp: roles.appsRoles[i].restrictedApp
87                             });
88                         }
89                     }
90                     this.dialogState = 2;
91                     this.adminAppsRoles = this.adminAppsRoles.sort(getSortOrder("appName"));
92
93                     orderList(roles.appsRoles);
94                     if (this.appsOrder != null) {
95                         for (var j = 0; j < this.appsOrder.length; j++) {
96                             this.originalApps.push(this.appsOrder[j]);
97                         }
98                     }
99                 }).catch(err => {
100                     $log.error(err);
101                 });
102             };
103
104             let getSortOrder = (prop) => {
105                 return function (a, b) {
106                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
107                         return 1;
108                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
109                         return -1;
110                     }
111                     return 0;
112                 }
113             }
114
115             this.setSelectedUser = (user) => {
116                 $log.debug('selected user: ', user);
117                 this.selectedUser = user;
118             };
119
120             this.unadminApp = (app) => {
121                 confirmBoxService.deleteItem(app.appName).then(confirmed => {
122                     if (confirmed === true) {
123                         app.isAdmin = false;
124                         this.appsOrder.splice(this.appsOrder.indexOf(app.id), 1);
125                     }
126                 }).catch(err => {
127                     $log(err);
128                 });
129             };
130
131             this.updateAdminAppsRoles = () => {
132                 if (!this.selectedUser || !this.selectedUser.orgUserId || !this.adminAppsRoles) {
133                     return;
134                 }
135                 this.isSaving = true;
136                 $log.debug('going to update user: ' + this.selectedUser.orgUserId + ' with app roles: ' + JSON.stringify(this.adminAppsRoles));
137                 confirmBoxService.makeAdminChanges('Are you sure you want to make these admin changes?')
138                     .then(confirmed => {
139                         adminsService.updateAdminAppsRoles({
140                                 orgUserId: this.selectedUser.orgUserId,
141                                 appsRoles: this.adminAppsRoles
142                             })
143                             .then(res => {
144                                 $log.debug('Admin apps roles updated successfully!', res);
145                                 this.remindToAddUserIfNecessary();
146                                 $scope.closeThisDialog(true);
147                             }).catch(err => {
148                             $log.error('NewAdminModalCtrl.updateAdminAppsRoles:: Failed - ' + err);
149                         }).finally(()=> {
150                             this.isSaving = false;
151                         })
152                     });
153             };
154
155             this.navigateBack = () => {
156                 if (this.dialogState === 1) {
157                 }
158                 if (this.dialogState === 2) {
159                     this.dialogState = 1;
160                 }
161             };
162
163             init();
164
165             $scope.$watch('newAdmin.selectedNewApp', (newVal) => {
166                 if (!newVal || newVal.isAdmin === undefined) {
167                     return;
168                 }
169                 let app = _.find(this.adminAppsRoles, {id: newVal.id});
170                 if (app) {
171                     app.isAdmin = true;
172                     this.appsOrder.push(app.id);
173                 }
174                 this.selectedNewApp = null;
175             });
176
177             $scope.$on('$stateChangeStart', e => {
178                 e.preventDefault();
179             });
180
181             this.remindToAddUserIfNecessary = () => {
182                 var adminAddedToNewApp = false;
183                 if ((this.originalApps != null) && (this.originalApps.length > 0)) {
184                     for (var i = 0; i < this.appsOrder.length; i++) {
185                         var foundApp = false;
186                         for (var j = 0; j < this.originalApps.length; j++) {
187                             if (this.originalApps[j] == this.appsOrder[i]) {
188                                 foundApp = true;
189                             }
190                         }
191                         if (foundApp == false) {
192                             adminAddedToNewApp = true;
193                             break;
194                         }
195                     }
196                 } else {
197                     adminAddedToNewApp = true;
198                 }
199
200                 if (adminAddedToNewApp == true) {
201                     confirmBoxService.confirm('Add this person as an application user? This allows them to access the application from OpenECOMP Portal. Press OK to go to the Add Users page.')
202                         .then(confirmed => {
203                             if (confirmed == true) {
204                                 $location.path('/users');
205                             }
206                         });
207                 }
208             }
209
210         }
211     }
212     NewAdminModalCtrl.$inject = ['$log', 'adminsService', '$scope', 'confirmBoxService', 'utilsService', '$location'];
213     angular.module('ecompApp').controller('NewAdminModalCtrl', NewAdminModalCtrl);
214 })();