nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / users / users.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 'use strict';
21 (function () {
22     class UsersCtrl {
23         constructor($log, applicationsService, usersService, $scope, ngDialog,$timeout) {
24             this.$log = $log;
25             $scope.adminAppsIsNull = false;
26             $scope.appsIsDown = false;
27
28             $log.info('UsersCtrl:: initializing...');
29             let activeRequests = [];
30             let clearReq = (req) => {
31                 activeRequests.splice(activeRequests.indexOf(req), 1);
32             };
33
34             let getAdminApps = () => {
35                 $log.debug('UsersCtrl::getAdminApps: - Starting getAdminApps');
36                 try {
37                     this.isLoadingTable = true;
38                     var adminAppsReq = applicationsService.getAdminApps();
39
40                     activeRequests.push(adminAppsReq);
41                     adminAppsReq.promise().then(apps => {
42                         $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(apps));
43                         $log.debug('UsersCtrl::getAdminApps: Apps length: ' + apps.length);
44                         var res1 = apps.sort(getSortOrder("name"));
45                         if (!res1 || !res1.length) {
46                             $log.error('UsersCtrl::getAdminApps:  - no apps found');
47                             return null;
48                         }
49                         for (let i = 0; i < apps.length; i++) {
50                             res1[i].index = i;
51                             res1[i].value = apps[i].name;
52                             res1[i].title = apps[i].name;
53                         }
54
55                         this.adminApps = apps;
56                         this.selectedApp = apps[0];
57                         clearReq(adminAppsReq);
58                         $scope.adminAppsIsNull = false;
59                         }).catch(e => {
60                             $scope.adminAppsIsNull = true;
61                             $log.error('UsersCtrl::getAdminApps:  - getAdminApps() failed = '+ e.message);
62                             clearReq(adminAppsReq);
63                         }).finally(() => {
64                             this.isLoadingTable = false;
65                         });
66                     } catch (e) {
67                         $scope.adminAppsIsNull = true;
68                         $log.error('UsersCtrl::getAdminApps:  - getAdminApps() failed!');
69                         this.isLoadingTable = false;
70                     }
71             };
72
73             let getSortOrder = (prop) => {
74                 return function(a, b) {
75                     if (a[prop] > b[prop]) {
76                         return 1;
77                     } else if (a[prop] < b[prop]) {
78                         return -1;
79                     }
80                     return 0;
81                 }
82             }
83
84             this.updateUsersList = () => {
85                 $scope.appsIsDown = false;
86                 $log.debug('UsersCtrl::updateUsersList: Starting updateUsersList');
87                 this.searchString = '';
88                 this.isAppSelectDisabled = true;
89                 this.isLoadingTable = true;
90                 usersService.getAccountUsers(this.selectedApp.id)
91                     .then(accountUsers => {
92                         $log.debug('UsersCtrl::updateUsersList length: '+ Object.keys(accountUsers).length);
93                         this.isAppSelectDisabled = false;
94                         this.accountUsers = accountUsers;
95                     }).catch(err => {
96                         this.isAppSelectDisabled = false;
97                         $log.error('UsersCtrl::updateUsersList: ' + err);
98                         $scope.appsIsDown = true;
99                     }).finally(() => {
100                         this.isLoadingTable = false;
101                 });
102             };
103
104
105             let init = () => {
106                 this.isLoadingTable = false;
107                 this.selectedApp = null;
108                 this.isAppSelectDisabled = false;
109                 getAdminApps();
110
111                 this.searchString = '';
112                 this.usersTableHeaders = ['First Name', 'Last Name', 'User ID', 'Roles'];
113                 this.accountUsers = [];
114             };
115
116             this.openAddNewUserModal = (user) => {
117                 let data = null;
118                 if (user) {
119                     data = {
120                         dialogState: 3,
121                         selectedUser: {
122                             orgUserId: user.orgUserId,
123                             firstName: user.firstName,
124                             lastName: user.lastName
125                         }
126                     }
127                 }
128                 ngDialog.open({
129                     templateUrl: 'app/views/users/new-user-dialogs/new-user.modal.html',
130                     controller: 'NewUserModalCtrl',
131                     controllerAs: 'newUser',
132                     data: data
133                 }).closePromise.then(needUpdate => {
134                     if (needUpdate.value === true) {
135                         $log.debug('UsersCtrl::openAddNewUserModal updating table data...');
136                         this.updateUsersList();
137                     }
138                 });
139             };
140             
141             this.openEditUserModal = (loginId) => {
142                 var data = {
143                                 loginId : loginId,
144                         updateRemoteApp : true,
145                         appId : this.selectedApp!=null?this.selectedApp.id:''
146                 }
147                 var modalInstance = ngDialog.open({
148                     templateUrl: 'app/views/header/user-edit/edit-user.tpl.html',
149                     controller: 'editUserController',
150                     data: data,
151                     resolve: {
152                         message: function message() {
153                             var message = {
154                                 type: 'Contact',
155                             };
156                             return message;
157                         }
158                     }
159                 }).closePromise.then(needUpdate => {
160                         //update selected app's database for this user.
161                         console.log("'''''''''''''''''' now updating user list after update remote server");
162                         $timeout(this.updateUsersList, 1500);
163                     });       
164             }
165             
166             
167             $scope.$watch('users.selectedApp', (newVal, oldVal) => {
168                 if (!newVal || _.isEqual(newVal, oldVal)) {
169                     return;
170                 }
171                 $log.debug('UsersCtrl::openAddNewUserModal:$watch selectedApp -> Fire with: ', newVal);
172                 this.accountUsers = [];
173                 this.updateUsersList();
174             });
175
176             $scope.$on('$destroy', () => {
177                 activeRequests.forEach(req => {
178                     req.cancel();
179                 });
180             });
181
182             init();
183         }
184     }
185     UsersCtrl.$inject = ['$log', 'applicationsService', 'usersService', '$scope', 'ngDialog','$timeout'];
186     angular.module('ecompApp').controller('UsersCtrl', UsersCtrl);
187 })();