[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / 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.error('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                 //if(!res || Object.prototype.toString.call(res) !== '[object Array]'){
59                 //    this.availableApps = [allPortalsFilterObject];
60                 //    return;
61                 //}
62                 //this part overrides index param to fix ABS select bug
63                 // (index has to be the same as location in array)
64                 // todo:change BE object to contain only id and name
65                 this.availableApps = [allPortalsFilterObject];
66                 var res1 = res.sort(getSortOrder("title"));
67                 var realAppIndex = 1;
68                 for(let i=1; i<=res1.length; i++){
69                     if (!res1[i-1].restrictedApp) {
70                         //$log.debug('AdminsCtrl:getAvailableApps:: pushing: {index: ', realAppIndex, 'title: ', res1[i - 1].title,
71                          //   '| value: ', res1[i -1].value, '}');
72                         this.availableApps.push({
73                             index: realAppIndex,
74                             title: res1[i - 1].title,
75                             value: res1[i - 1].value
76                         });
77                         realAppIndex = realAppIndex + 1;
78                     } else {
79                         // $log.debug('AdminsCtrl:getAvailableApps:: Restricted/URL only App will not be used = ' + res1[i - 1].title);
80                     }
81                 }
82             }).catch(err=> {
83                 $log.error('AdminsCtrl::ctor', err);
84                 this.availableApps = [allPortalsFilterObject];
85             });
86
87             // Refactor this into a directive
88             let getSortOrder = (prop) => {
89                 return function(a, b) {
90                     // $log.debug('a = ' + JSON.stringify(a) + "| b = " + JSON.stringify(b));
91                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
92                         return 1;
93                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
94                         return -1;
95                     }
96                     return 0;
97                 }
98             }
99
100             init();
101
102             //Filter function
103             this.portalsRowFilter = (input) => {
104                 if (this.filterByApp.value === '') {
105                     return true;
106                 }
107                 return _.find(input.apps, {appName: this.filterByApp.value}) !== undefined;
108             };
109
110             this.openAddNewAdminModal = (user) => {
111                 let data = null;
112                 if(user){
113                     data = {
114                         dialogState: 2,
115                         selectedUser:{
116                             orgUserId: user.orgUserId,
117                             firstName: user.firstName,
118                             lastName: user.lastName
119                         }
120                     }
121                 }
122                 ngDialog.open({
123                     templateUrl: 'app/views/admins/add-admin-dialogs/new-admin.modal.html',
124                     controller: 'NewAdminModalCtrl',
125                     controllerAs: 'newAdmin',
126                     data: data
127                 }).closePromise.then(needUpdate => {
128                     if(needUpdate.value === true){
129                         // $log.debug('AdminsCtrl:openAddNewAdminModal:: updating table data...');
130                         updateTableData();
131                     }
132                 });
133             };
134             
135             this.openEditUserModal = (loginId) => {
136                 var data = {
137                                 loginId : loginId,
138                         updateRemoteApp : false,
139                         appId : this.selectedApp!=null?this.selectedApp.id:''
140                 }
141                 var modalInstance = ngDialog.open({
142                     templateUrl: 'app/views/header/user-edit/edit-user.tpl.html',
143                     controller: 'editUserController',
144                     data: data,
145                     resolve: {
146                         message: function message() {
147                             var message = {
148                                 type: 'Contact',
149                             };
150                             return message;
151                         }
152                     }
153                 }).closePromise.then(needUpdate => {                     
154                         updateTableData();
155                     });       
156             }
157         }
158     }
159     AdminsCtrl.$inject = ['$log', 'adminsService', 'applicationsService', 'ngDialog'];
160     angular.module('ecompApp').controller('AdminsCtrl', AdminsCtrl);
161 })();