summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-FE-common/client/app/views/users/users.controller.js
blob: ac223ed6263f23a175bd545902bdbd14e049366a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*-
 * ================================================================================
 * ECOMP Portal
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
'use strict';
(function () {
    class UsersCtrl {
        constructor($log, applicationsService, usersService, confirmBoxService, $scope, ngDialog) {
            this.$log = $log;
            $scope.adminAppsIsNull = false;
            $scope.appsIsDown = false;
            $scope.noUsersInApp = false;
            $scope.multiAppAdmin = false;

            $log.info('UsersCtrl:: initializing...');
            /**
             * Handle all active HTTP requests
             * activeRequests @type {Array[requests with cancel option]}
             */
            let activeRequests = [];
            let clearReq = (req) => {
                activeRequests.splice(activeRequests.indexOf(req), 1);
            };

            let init = () => {
                this.isLoadingTable = false;
                this.selectedApp = null;
                this.isAppSelectDisabled = false;
                this.selectApp = 'Select application';
                this.adminApps = [{index: 0, id: 0, value: this.selectApp, title: this.selectApp}];
                getAdminApps();

                /*Table general configuration params*/
                this.searchString = '';
                /*Table data*/
                this.usersTableHeaders = ['First Name', 'Last Name', 'User ID', 'Roles'];
                this.accountUsers = [];
            };

            let getAdminApps = () => {
                $log.debug('UsersCtrl::getAdminApps: - Starting getAdminApps');
                try {
                    this.isLoadingTable = true;
                    let adminAppsReq = applicationsService.getAdminApps();
                    adminAppsReq.promise().then(apps => {
                        if (!apps || !apps.length) {
                            $log.error('UsersCtrl::getAdminApps:  - no apps found');
                            return null;
                        }
                        $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(apps));
                        if (apps.length >= 2) {
                            $log.info('UsersCtrl::getAdminApps:  - more than one app for this admin:', apps.length, ' apps');
                            $scope.multiAppAdmin = true;
                        } else {
                            this.adminApps = [] ;
                        }
                        let sortedApps = apps.sort(getSortOrder("name"));
                        let realAppIndex = 1;
                        for(let i=1; i<=sortedApps.length; i++){
                            this.adminApps.push({
                                index: realAppIndex,
                                id: sortedApps[i - 1].id,
                                value: sortedApps[i - 1].name,
                                title: sortedApps[i - 1].name
                            });
                            realAppIndex = realAppIndex + 1;
                        }

                        $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(this.adminApps));

                        this.selectedApp = this.adminApps[0];
                        clearReq(adminAppsReq);
                        $scope.adminAppsIsNull = false;
                        }).catch(e => {
                            $scope.adminAppsIsNull = true;
                            $log.error('UsersCtrl::getAdminApps:  - getAdminApps() failed = '+ e.message);
                            clearReq(adminAppsReq);
                            confirmBoxService.showInformation('There was a problem retrieving the applications. ' +
                                'Please try again later.').then(isConfirmed => {});

                    }).finally(() => {
                            this.isLoadingTable = false;
                        });
                    } catch (e) {
                        $scope.adminAppsIsNull = true;
                        $log.error('UsersCtrl::getAdminApps:  - getAdminApps() failed!');
                        this.isLoadingTable = false;
                    }
            };

            let getSortOrder = (prop) => {
                return function(a, b) {
                    if (a[prop] > b[prop]) {
                        return 1;
                    } else if (a[prop] < b[prop]) {
                        return -1;
                    }
                    return 0;
                }
            }

            this.updateUsersList = () => {
                $scope.appsIsDown = false;
                $scope.noUsersInApp = false;
                // $log.debug('UsersCtrl::updateUsersList: Starting updateUsersList');
                //reset search string
                this.searchString = '';
                //should i disable this too in case of moving between tabs?
                this.isAppSelectDisabled = true;
                //activate spinner
                this.isLoadingTable = true;
                
                if(this.adminApps!=null && this.selectedApp!=null){
                	 var tempSelected = null;
                	 for(let i=0; i<=this.adminApps.length; i++){
                     	if(typeof this.adminApps[i] != 'undefined' && this.selectedApp.value==this.adminApps[i].value){
                     		tempSelected=_.clone(this.adminApps[i]);
                     	}
                     }
                     if(tempSelected!=null){
                     	this.selectedApp= tempSelected;
                     }
                }
               
                if (this.selectedApp.title != this.selectApp) { // 'Select Application'
                    usersService.getAccountUsers(this.selectedApp.id)
                        .then(accountUsers => {
                            $log.debug('UsersCtrl::updateUsersList accountUsers: '+ accountUsers);
                            if (angular.isObject(accountUsers)===false) {
                                $log.error('UsersCtrl::updateUsersList accountUsers: App is down!');
                                $scope.appsIsDown = true;
                            }
                            $log.debug('UsersCtrl::updateUsersList length: '+ Object.keys(accountUsers).length);
                            this.isAppSelectDisabled = false;
                            this.accountUsers = accountUsers;
                            if (angular.isObject(accountUsers) && Object.keys(accountUsers).length === 0) {
                                $log.debug('UsersCtrl::updateUsersList accountUsers: App has no users.');
                                $scope.noUsersInApp = true;
                            }
                        }).catch(err => {
                            this.isAppSelectDisabled = false;
                            $log.error('UsersCtrl::updateUsersList error: ' + err);
                            confirmBoxService.showInformation('There was a problem updating the users List. ' +
                                'Please try again later.').then(isConfirmed => {});
                            $scope.appsIsDown = true;
                        }).finally(() => {
                            this.isLoadingTable = false;
                            $scope.noAppSelected = false;
                    });
                } else {
                    // this.selectedApp = this.adminApps[0];
                    this.isAppSelectDisabled = false;
                    this.isLoadingTable = false;
                    $scope.noUsersInApp = false;
                    $scope.noAppSelected = true;
                }
            };


            this.openAddNewUserModal = (user) => {
                let data = null;
                if (user) {
                    data = {
                        dialogState: 3,
                        selectedUser: {
                            orgUserId: user.orgUserId,
                            firstName: user.firstName,
                            lastName: user.lastName,

                        }
                    }
                }
                ngDialog.open({
                    templateUrl: 'app/views/users/new-user-dialogs/new-user.modal.html',
                    controller: 'NewUserModalCtrl',
                    controllerAs: 'newUser',
                    data: data
                }).closePromise.then(needUpdate => {
                    if (needUpdate.value === true) {
                        $log.debug('UsersCtrl::openAddNewUserModal updating table data...');
                        this.updateUsersList();
                    }
                });
            };
            
            this.openBulkUserUploadModal = (adminApps) => {
                let data = null;
                if (adminApps) {
                    data = {
                        dialogState: 3,
                        selectedApplication: {
                            appid: adminApps[0].appid,
                            appName: adminApps[0].appName
                        }
                    }
                }
                ngDialog.open({
                    templateUrl: 'app/views/users/new-user-dialogs/bulk-user.modal.html',
                    controller: 'BulkUserModalCtrl',
                    controllerAs: 'bulkUser',
                    data: data
                }).closePromise.then(needUpdate => {
                	this.updateUsersList();
                });
            };


            $scope.$watch('users.selectedApp.value', (newVal, oldVal) => {
                if (!newVal || _.isEqual(newVal, oldVal)) {
                    return;
                }
                $log.debug('UsersCtrl::openAddNewUserModal:$watch selectedApp -> Fire with: ', newVal);
                this.accountUsers = []; //reset table and show swirl here
                this.updateUsersList();
            });

            $scope.$on('$destroy', () => {
                //cancel all active requests when closing the modal
                activeRequests.forEach(req => {
                    req.cancel();
                });
            });

            init();
        }
    }
    UsersCtrl.$inject = ['$log', 'applicationsService', 'usersService', 'confirmBoxService', '$scope', 'ngDialog'];
    angular.module('ecompApp').controller('UsersCtrl', UsersCtrl);
})();