[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / users / users.controller.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 'use strict';\r
21 (function () {\r
22     class UsersCtrl {\r
23         constructor($log, applicationsService, usersService, confirmBoxService, $scope, ngDialog) {\r
24             this.$log = $log;\r
25             $scope.adminAppsIsNull = false;\r
26             $scope.appsIsDown = false;\r
27             $scope.noUsersInApp = false;\r
28             $scope.multiAppAdmin = false;\r
29 \r
30             $log.info('UsersCtrl:: initializing...');\r
31             /**\r
32              * Handle all active HTTP requests\r
33              * activeRequests @type {Array[requests with cancel option]}\r
34              */\r
35             let activeRequests = [];\r
36             let clearReq = (req) => {\r
37                 activeRequests.splice(activeRequests.indexOf(req), 1);\r
38             };\r
39 \r
40             let init = () => {\r
41                 this.isLoadingTable = false;\r
42                 this.selectedApp = null;\r
43                 this.isAppSelectDisabled = false;\r
44                 this.selectApp = 'Select application';\r
45                 this.adminApps = [{index: 0, id: 0, value: this.selectApp, title: this.selectApp}];\r
46                 getAdminApps();\r
47 \r
48                 /*Table general configuration params*/\r
49                 this.searchString = '';\r
50                 /*Table data*/\r
51                 this.usersTableHeaders = ['First Name', 'Last Name', 'User ID', 'Roles'];\r
52                 this.accountUsers = [];\r
53             };\r
54 \r
55             let getAdminApps = () => {\r
56                 $log.debug('UsersCtrl::getAdminApps: - Starting getAdminApps');\r
57                 try {\r
58                     this.isLoadingTable = true;\r
59                     let adminAppsReq = applicationsService.getAdminApps();\r
60                     adminAppsReq.promise().then(apps => {\r
61                         if (!apps || !apps.length) {\r
62                             $log.error('UsersCtrl::getAdminApps:  - no apps found');\r
63                             return null;\r
64                         }\r
65                         $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(apps));\r
66                         if (apps.length >= 2) {\r
67                             $log.info('UsersCtrl::getAdminApps:  - more than one app for this admin:', apps.length, ' apps');\r
68                             $scope.multiAppAdmin = true;\r
69                         } else {\r
70                             this.adminApps = [] ;\r
71                         }\r
72                         let sortedApps = apps.sort(getSortOrder("name"));\r
73                         let realAppIndex = 1;\r
74                         for(let i=1; i<=sortedApps.length; i++){\r
75                             this.adminApps.push({\r
76                                 index: realAppIndex,\r
77                                 id: sortedApps[i - 1].id,\r
78                                 value: sortedApps[i - 1].name,\r
79                                 title: sortedApps[i - 1].name\r
80                             });\r
81                             realAppIndex = realAppIndex + 1;\r
82                         }\r
83 \r
84                         $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(this.adminApps));\r
85 \r
86                         this.selectedApp = this.adminApps[0];\r
87                         clearReq(adminAppsReq);\r
88                         $scope.adminAppsIsNull = false;\r
89                         }).catch(e => {\r
90                             $scope.adminAppsIsNull = true;\r
91                             $log.error('UsersCtrl::getAdminApps:  - getAdminApps() failed = '+ e.message);\r
92                             clearReq(adminAppsReq);\r
93                             confirmBoxService.showInformation('There was a problem retrieving the applications. ' +\r
94                                 'Please try again later.').then(isConfirmed => {});\r
95 \r
96                     }).finally(() => {\r
97                             this.isLoadingTable = false;\r
98                         });\r
99                     } catch (e) {\r
100                         $scope.adminAppsIsNull = true;\r
101                         $log.error('UsersCtrl::getAdminApps:  - getAdminApps() failed!');\r
102                         this.isLoadingTable = false;\r
103                     }\r
104             };\r
105 \r
106             let getSortOrder = (prop) => {\r
107                 return function(a, b) {\r
108                     if (a[prop] > b[prop]) {\r
109                         return 1;\r
110                     } else if (a[prop] < b[prop]) {\r
111                         return -1;\r
112                     }\r
113                     return 0;\r
114                 }\r
115             }\r
116 \r
117             this.updateUsersList = () => {\r
118                 $scope.appsIsDown = false;\r
119                 $scope.noUsersInApp = false;\r
120                 // $log.debug('UsersCtrl::updateUsersList: Starting updateUsersList');\r
121                 //reset search string\r
122                 this.searchString = '';\r
123                 //should i disable this too in case of moving between tabs?\r
124                 this.isAppSelectDisabled = true;\r
125                 //activate spinner\r
126                 this.isLoadingTable = true;\r
127                 \r
128                 if(this.adminApps!=null && this.selectedApp!=null){\r
129                          var tempSelected = null;\r
130                          for(let i=0; i<=this.adminApps.length; i++){\r
131                         if(typeof this.adminApps[i] != 'undefined' && this.selectedApp.value==this.adminApps[i].value){\r
132                                 tempSelected=_.clone(this.adminApps[i]);\r
133                         }\r
134                      }\r
135                      if(tempSelected!=null){\r
136                         this.selectedApp= tempSelected;\r
137                      }\r
138                 }\r
139                \r
140                 if (this.selectedApp.title != this.selectApp) { // 'Select Application'\r
141                     usersService.getAccountUsers(this.selectedApp.id)\r
142                         .then(accountUsers => {\r
143                             $log.debug('UsersCtrl::updateUsersList accountUsers: '+ accountUsers);\r
144                             if (angular.isObject(accountUsers)===false) {\r
145                                 $log.error('UsersCtrl::updateUsersList accountUsers: App is down!');\r
146                                 $scope.appsIsDown = true;\r
147                             }\r
148                             $log.debug('UsersCtrl::updateUsersList length: '+ Object.keys(accountUsers).length);\r
149                             this.isAppSelectDisabled = false;\r
150                             this.accountUsers = accountUsers;\r
151                             if (angular.isObject(accountUsers) && Object.keys(accountUsers).length === 0) {\r
152                                 $log.debug('UsersCtrl::updateUsersList accountUsers: App has no users.');\r
153                                 $scope.noUsersInApp = true;\r
154                             }\r
155                         }).catch(err => {\r
156                             this.isAppSelectDisabled = false;\r
157                             $log.error('UsersCtrl::updateUsersList error: ' + err);\r
158                             confirmBoxService.showInformation('There was a problem updating the users List. ' +\r
159                                 'Please try again later.').then(isConfirmed => {});\r
160                             $scope.appsIsDown = true;\r
161                         }).finally(() => {\r
162                             this.isLoadingTable = false;\r
163                             $scope.noAppSelected = false;\r
164                     });\r
165                 } else {\r
166                     // this.selectedApp = this.adminApps[0];\r
167                     this.isAppSelectDisabled = false;\r
168                     this.isLoadingTable = false;\r
169                     $scope.noUsersInApp = false;\r
170                     $scope.noAppSelected = true;\r
171                 }\r
172             };\r
173 \r
174 \r
175             this.openAddNewUserModal = (user) => {\r
176                 let data = null;\r
177                 if (user) {\r
178                     data = {\r
179                         dialogState: 3,\r
180                         selectedUser: {\r
181                             orgUserId: user.orgUserId,\r
182                             firstName: user.firstName,\r
183                             lastName: user.lastName\r
184                         }\r
185                     }\r
186                 }\r
187                 ngDialog.open({\r
188                     templateUrl: 'app/views/users/new-user-dialogs/new-user.modal.html',\r
189                     controller: 'NewUserModalCtrl',\r
190                     controllerAs: 'newUser',\r
191                     data: data\r
192                 }).closePromise.then(needUpdate => {\r
193                     if (needUpdate.value === true) {\r
194                         $log.debug('UsersCtrl::openAddNewUserModal updating table data...');\r
195                         this.updateUsersList();\r
196                     }\r
197                 });\r
198             };\r
199             \r
200             this.openBulkUserUploadModal = (adminApps) => {\r
201                 let data = null;\r
202                 if (adminApps) {\r
203                     data = {\r
204                         dialogState: 3,\r
205                         selectedApplication: {\r
206                             appid: adminApps[0].appid,\r
207                             appName: adminApps[0].appName\r
208                         }\r
209                     }\r
210                 }\r
211                 ngDialog.open({\r
212                     templateUrl: 'app/views/users/new-user-dialogs/bulk-user.modal.html',\r
213                     controller: 'BulkUserModalCtrl',\r
214                     controllerAs: 'bulkUser',\r
215                     data: data\r
216                 }).closePromise.then(needUpdate => {\r
217                         this.updateUsersList();\r
218                 });\r
219             };\r
220 \r
221 \r
222             $scope.$watch('users.selectedApp.value', (newVal, oldVal) => {\r
223                 if (!newVal || _.isEqual(newVal, oldVal)) {\r
224                     return;\r
225                 }\r
226                 $log.debug('UsersCtrl::openAddNewUserModal:$watch selectedApp -> Fire with: ', newVal);\r
227                 this.accountUsers = []; //reset table and show swirl here\r
228                 this.updateUsersList();\r
229             });\r
230 \r
231             $scope.$on('$destroy', () => {\r
232                 //cancel all active requests when closing the modal\r
233                 activeRequests.forEach(req => {\r
234                     req.cancel();\r
235                 });\r
236             });\r
237 \r
238             init();\r
239         }\r
240     }\r
241     UsersCtrl.$inject = ['$log', 'applicationsService', 'usersService', 'confirmBoxService', '$scope', 'ngDialog'];\r
242     angular.module('ecompApp').controller('UsersCtrl', UsersCtrl);\r
243 })();\r