Deliver centralized role management feature
[portal.git] / ecomp-portal-FE-common / client / app / views / portal-admin / portal-admin-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 PortalAdminsCtrl {
23         constructor($log, portalAdminsService, ngDialog, confirmBoxService, $modal) {
24
25             let updateTableData = () => {
26                 this.isLoadingTable = true;
27                 portalAdminsService.getPortalAdmins().then(result=> {
28                     $log.debug('PortalAdminsCtrl::updateTableData: result: ' + JSON.stringify(result));
29                     if (!result || !result.length) {
30                         $log.info('PortalAdminsCtrl::updateTableData: no Portal Admins err handling');
31                         this.portalAdminsTableData = [];
32                         return;
33                     }
34                     this.portalAdminsTableData = result;
35                 }).catch(err=> {
36                     $log.error('PortalAdminsCtrl::updateTableData error :',err);
37                     confirmBoxService.showInformation('There was a problem retrieving the portal admins. ' +
38                         'Please try again later. Error: ' + err.status).then(isConfirmed => {});
39
40                 }).finally(() => {
41                     this.isLoadingTable = false;
42                 });
43             };
44
45             let init = () => {
46                 $log.info('portalAdminsService.getPortalAdmins::initializing...');
47                 this.isLoadingTable = false;
48
49                 /*Table general configuration params*/
50                 this.searchString= '';
51                 /*Table data*/
52                 this.portalAdminsTableHeaders = ['First Name', 'Last Name', 'User ID', 'Delete'];
53                 this.portalAdminsTableData = [];
54                 updateTableData();
55             };
56
57             init();
58
59             this.removePortalAdmin = pAdmin => {
60                 $log.debug('PortalAdminsCtrl::removePortalAdmin: pAdmin = ' + JSON.stringify(pAdmin));
61                 confirmBoxService.deleteItem(pAdmin.firstName + ' ' + pAdmin.lastName )
62                     .then(isConfirmed => {
63                     if(isConfirmed){
64                         if(!pAdmin || !pAdmin.userId){
65                             $log.error('PortalAdminsCtrl::removePortalAdmin No portal admin or ID... cannot delete');
66                             return;
67                         }
68                         portalAdminsService.removePortalAdmin(pAdmin.userId,pAdmin.loginId).then(() => {
69                             $log.info("PortalAdminsCtrl::removePortalAdmin removed admin");
70                             init();
71                         }).catch(err => {
72                             $log.error('PortalAdminsCtrl::removePortalAdmin.deleteItem error: '+ err);
73                             confirmBoxService.showInformation('There was a problem deleting this portal admins. ' +
74                                 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
75                         });
76                     }
77                 }).catch(err => {
78                     $log.error('PortalAdminsCtrl::removePortalAdmin.deleteItem error: '+ err);
79                 });
80             };
81             
82             this.openAddNewPortalAdminModal = (user) => {
83                 let data = null;
84                 if(user){
85                     data = {
86                         dialogState: 2,
87                         selectedUser:{
88                             orgUserId: user.orgUserId,
89                             firstName: user.firstName,
90                             lastName: user.lastName
91                         }
92                     }
93                 }
94                 
95                 var modalInstance = $modal.open({
96                     templateUrl: 'app/views/portal-admin/new-portal-admin/new-portal-admin.modal.html',
97                     controller: 'NewPortalAdminModalCtrl as newPortalAdmin',
98                     sizeClass: 'modal-medium',
99                     data: data
100                 })
101                 
102                 modalInstance.result.finally(function () {
103                         $log.debug('PortalAdminsCtrl::openAddNewPortalAdminModal: updating Portal Admin table data...');         
104                     updateTableData();
105                 });
106                 
107             };
108         }
109     }
110     PortalAdminsCtrl.$inject = ['$log', 'portalAdminsService', 'ngDialog', 'confirmBoxService', '$modal'];
111     angular.module('ecompApp').controller('PortalAdminsCtrl', PortalAdminsCtrl);
112 })();