Deliver centralized role management feature
[portal.git] / ecomp-portal-FE-common / client / app / views / portal-admin / new-portal-admin / new-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 /**
21  * Created by nnaffar on 12/8/15.
22  */
23 'use strict';
24 (function () {
25     class NewPortalAdminModalCtrl {
26         constructor($log, portalAdminsService, $scope, confirmBoxService) {
27
28             let init = () => {
29                 this.isSaving = false;
30                 /* istanbul ignore if */
31                 if($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState){
32                     this.selectedUser = $scope.ngDialogData.selectedUser;
33                     this.dialogState = $scope.ngDialogData.dialogState;
34                 }else{
35                     this.selectedUser = null;
36                     this.dialogState = 1;
37                 }
38                 //this.searchUsersInProgress = false;
39                 //this.showNewAdminAppDropdown = false;
40                 $log.info('NewPortalAdminModalCtrl:: initiated');
41             };
42
43             this.addNewPortalAdmin = () => {
44                 confirmBoxService.makeAdminChanges('Are you sure you want to add "' + this.selectedUser.firstName + ' ' + this.selectedUser.lastName + '" as a Portal Admin?')
45                     .then(isConfirmed => {
46                         if(isConfirmed) {
47                             if (!this.selectedUser || !this.selectedUser.orgUserId) {
48                                 $log.error('NewPortalAdminModalCtrl::makeAdminChanges: No portal admin or ID... cannot add');
49                                 return;
50                             }
51                             portalAdminsService.addPortalAdmin(this.selectedUser.orgUserId)
52                                 .then(() => {
53                                     $log.debug("NewPortalAdminModalCtrl::addNewPortalAdmin: portal admin added successfully");
54                                      $scope.$dismiss('cancel');
55                                 }).catch(err => {
56                                     if(err.status === 409) {    //Conflict
57                                         confirmBoxService.showInformation('This user already exists as a portal admin!').then(function (isConfirmed) {
58                                              $scope.$dismiss('cancel');
59                                         });
60                                     } else {
61                                         confirmBoxService.showInformation('There was a unknown problem adding the portal admin. ' + 'Please try again later. Error Status: '+ err.status).then(function (isConfirmed)  {
62                                              $scope.$dismiss('cancel');
63                                         });
64                                     }
65                             });
66                         }
67                     }).catch(err => {
68                         confirmBoxService.showInformation('There was a unknown problem adding the portal admin. ' + 'Please try again later. Error Status: '+ err.status).then(function (isConfirmed)  {
69                              $scope.$dismiss('cancel');
70                         });
71                         $log.error('portalAdminsService.addPortalAdmin error status: '+ err.status);
72                 });
73             };
74
75             /**
76              * this function set the selected user
77              * @param user: selected user object
78              */
79             this.setSelectedUser = (user) => {
80                 $log.debug('NewPortalAdminModalCtrl::setSelectedUser: selected user: ', user);
81                 this.selectedUser = user;
82             };
83
84             init();
85
86             $scope.$on('$stateChangeStart', e => {
87                 //Disable navigation when modal is opened
88                 //**Nabil - note: this will cause the history back state to be replaced with current state
89                 e.preventDefault();
90             });
91         }
92     }
93     NewPortalAdminModalCtrl.$inject = ['$log', 'portalAdminsService', '$scope', 'confirmBoxService'];
94     angular.module('ecompApp').controller('NewPortalAdminModalCtrl', NewPortalAdminModalCtrl);
95 })();