nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / admins / admins.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 AdminsCtrl {
23         constructor($log, adminsService, applicationsService, ngDialog) {
24
25             let allPortalsFilterObject = {index: 0, title: 'All applications', value: ''};
26
27             let updateTableData = () => {
28                 this.isLoadingTable = true;
29                 adminsService.getAccountAdmins().then(res=> {
30                     if (!res || !res.length) {
31                         $log.info('AdminsCtrl::updateTableData: no admins err handling');
32                         this.adminsTableData = [];
33                         return;
34                     }
35                     this.adminsTableData = res;
36                 }).catch(err=> {
37                     $log.error('AdminsCtrl::updateTableData error: ', err);
38                 }).finally(() => {
39                     this.isLoadingTable = false;
40                 });
41             };
42
43             let init = () => {
44                 $log.info('AdminsCtrl:: ::initializing...');
45                 this.isLoadingTable = false;
46                 this.availableApps = [allPortalsFilterObject];
47                 this.filterByApp = this.availableApps[0];
48
49                 /* Table general configuration params*/
50                 this.searchString= '';
51                 /*Table data*/
52                 this.adminsTableHeaders = ['First Name', 'Last Name', 'User ID', 'Applications'];
53                 this.adminsTableData = [];
54                 updateTableData();
55             };
56
57             applicationsService.getAvailableApps().then(res=> {
58                 this.availableApps = [allPortalsFilterObject];
59                 var res1 = res.sort(getSortOrder("title"));
60                 var realAppIndex = 1;
61                 for(let i=1; i<=res1.length; i++){
62                     if (!res1[i-1].restrictedApp) {
63                         $log.debug('AdminsCtrl:getAvailableApps:: pushing: {index: ', realAppIndex, 'title: ', res1[i - 1].title,
64                             '| value: ', res1[i -1].value, '}');
65                         this.availableApps.push({
66                             index: realAppIndex,
67                             title: res1[i - 1].title,
68                             value: res1[i - 1].value
69                         });
70                         realAppIndex = realAppIndex + 1;
71                     } else {
72                         $log.debug('AdminsCtrl:getAvailableApps:: Restricted/URL only App will not be used = ' + res1[i - 1].title);
73                     }
74                 }
75             }).catch(err=> {
76                 $log.error(err);
77                 this.availableApps = [allPortalsFilterObject];
78             });
79
80             let getSortOrder = (prop) => {
81                 return function(a, b) {
82                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
83                         return 1;
84                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
85                         return -1;
86                     }
87                     return 0;
88                 }
89             }
90
91             init();
92
93             this.portalsRowFilter = (input) => {
94                 if (this.filterByApp.value === '') {
95                     return true;
96                 }
97                 return _.find(input.apps, {appName: this.filterByApp.value}) !== undefined;
98             };
99
100             this.openAddNewAdminModal = (user) => {
101                 let data = null;
102                 if(user){
103                     data = {
104                         dialogState: 2,
105                         selectedUser:{
106                                 orgUserId: user.orgUserId,
107                             firstName: user.firstName,
108                             lastName: user.lastName
109                         }
110                     }
111                 }
112                 ngDialog.open({
113                     templateUrl: 'app/views/admins/add-admin-dialogs/new-admin.modal.html',
114                     controller: 'NewAdminModalCtrl',
115                     controllerAs: 'newAdmin',
116                     data: data
117                 }).closePromise.then(needUpdate => {
118                     if(needUpdate.value === true){
119                         $log.debug('AdminsCtrl:openAddNewAdminModal:: updating table data...');
120                         updateTableData();
121                     }
122                 });
123             };
124             
125             this.openEditUserModal = (loginId) => {
126                 var data = {
127                                 loginId : loginId,
128                         updateRemoteApp : false,
129                         appId : this.selectedApp!=null?this.selectedApp.id:''
130                 }
131                 var modalInstance = ngDialog.open({
132                     templateUrl: 'app/views/header/user-edit/edit-user.tpl.html',
133                     controller: 'editUserController',
134                     data: data,
135                     resolve: {
136                         message: function message() {
137                             var message = {
138                                 type: 'Contact',
139                             };
140                             return message;
141                         }
142                     }
143                 }).closePromise.then(needUpdate => {                     
144                         updateTableData();
145                     });       
146             }
147         }
148     }
149     AdminsCtrl.$inject = ['$log', 'adminsService', 'applicationsService', 'ngDialog'];
150     angular.module('ecompApp').controller('AdminsCtrl', AdminsCtrl);
151 })();