summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-FE-common/client/app/views/admins/admins.controller.js
blob: 28144ed7d6277b7ddf850ac64dc6b1ee1115cd37 (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
/*-
 * ================================================================================
 * 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 AdminsCtrl {
        constructor($log, adminsService, applicationsService, ngDialog, $modal) {

            let allPortalsFilterObject = {index: 0, title: 'All applications', value: ''};

            let updateTableData = () => {
                this.isLoadingTable = true;
                adminsService.getAccountAdmins().then(res=> {
                    if (!res || !res.length) {
                        $log.error('AdminsCtrl::updateTableData: no admins err handling');
                        this.adminsTableData = [];
                        return;
                    }
                    this.adminsTableData = res;
                }).catch(err=> {
                    $log.error('AdminsCtrl::updateTableData error: ', err);
                }).finally(() => {
                    this.isLoadingTable = false;
                });
            };

            let init = () => {
                //$log.info('AdminsCtrl:: ::initializing...');
                this.isLoadingTable = false;
                this.availableApps = [allPortalsFilterObject];
                this.filterByApp = this.availableApps[0];

                /*Table general configuration params*/
                this.searchString= '';
                /*Table data*/
                this.adminsTableHeaders = ['First Name', 'Last Name', 'User ID', 'Applications'];
                this.adminsTableData = [];
                updateTableData();
            };

            applicationsService.getAvailableApps().then(res=> {
                //if(!res || Object.prototype.toString.call(res) !== '[object Array]'){
                //    this.availableApps = [allPortalsFilterObject];
                //    return;
                //}
                //this part overrides index param to fix ABS select bug
                // (index has to be the same as location in array)
                // todo:change BE object to contain only id and name
                this.availableApps = [allPortalsFilterObject];
                var res1 = res.sort(getSortOrder("title"));
                var realAppIndex = 1;
                for(let i=1; i<=res1.length; i++){
                    if (!res1[i-1].restrictedApp) {
                        //$log.debug('AdminsCtrl:getAvailableApps:: pushing: {index: ', realAppIndex, 'title: ', res1[i - 1].title,
                         //   '| value: ', res1[i -1].value, '}');
                        this.availableApps.push({
                            index: realAppIndex,
                            title: res1[i - 1].title,
                            value: res1[i - 1].value
                        });
                        realAppIndex = realAppIndex + 1;
                    } else {
                        // $log.debug('AdminsCtrl:getAvailableApps:: Restricted/URL only App will not be used = ' + res1[i - 1].title);
                    }
                }
            }).catch(err=> {
                $log.error('AdminsCtrl::ctor', err);
                this.availableApps = [allPortalsFilterObject];
            });

            // Refactor this into a directive
            let getSortOrder = (prop) => {
                return function(a, b) {
                    // $log.debug('a = ' + JSON.stringify(a) + "| b = " + JSON.stringify(b));
                    if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
                        return 1;
                    } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
                        return -1;
                    }
                    return 0;
                }
            }

            init();

            //Filter function
            this.portalsRowFilter = (input) => {
                if (this.filterByApp.value === '') {
                    return true;
                }
                return _.find(input.apps, {appName: this.filterByApp.value}) !== undefined;
            };

            this.openAddNewAdminModal = (user) => {
                let data = null;
                if(user){
                    data = {
                        dialogState: 2,
                        selectedUser:{
                            orgUserId: user.orgUserId,
                            firstName: user.firstName,
                            lastName: user.lastName
                        }
                    }
                }
                var modalInstance = $modal.open({
                    templateUrl: 'app/views/admins/add-admin-dialogs/new-admin.modal.html',
                    controller: 'NewAdminModalCtrl as newAdmin',
                    sizeClass: 'modal-medium', 
                    resolve: {
    					items: function () {
    						return data;
    			        }
    		        }
                });
                
                modalInstance.result.finally(function () {
                	updateTableData();  
     	        });
            };
        }
    }
    AdminsCtrl.$inject = ['$log', 'adminsService', 'applicationsService', 'ngDialog', '$modal'];
    angular.module('ecompApp').controller('AdminsCtrl', AdminsCtrl);
})();