nexus site path corrected
[portal.git] / ecomp-portal-FE / 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 'use strict';
21 (function () {
22     class NewPortalAdminModalCtrl {
23         constructor($log, portalAdminsService, $scope, confirmBoxService) {
24
25             let init = () => {
26                 this.isSaving = false;
27                 if($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState){
28                     this.selectedUser = $scope.ngDialogData.selectedUser;
29                     this.dialogState = $scope.ngDialogData.dialogState;
30                 }else{
31                     this.selectedUser = null;
32                     this.dialogState = 1;
33                 }
34                 $log.info('NewPortalAdminModalCtrl:: initiated');
35             };
36
37             this.addNewPortalAdmin = () => {
38                 portalAdminsService.getPortalAdmins().then(result=> {
39                     var dupNameCheck = JSON.stringify(result).search(this.selectedUser.orgUserId);
40                     if (dupNameCheck != -1) {
41                         $log.error("NewPortalAdminModalCtrl::addNewPortalAdmin: userId already exists as a portal admin! dupNameCheck=",dupNameCheck);
42                         confirmBoxService.showInformation('This user already exists as a portal admin!').then(function (isConfirmed) {
43                             $scope.closeThisDialog(true);
44                         });
45                     } else {
46                         confirmBoxService.makeAdminChanges('Are you sure you want to add "' + this.selectedUser.firstName + ' ' + this.selectedUser.lastName + '" as a Portal Admin?')
47                             .then(isConfirmed => {
48                                 if(isConfirmed) {
49                                     if (!this.selectedUser || !this.selectedUser.orgUserId) {
50                                         $log.error('NewPortalAdminModalCtrl::makeAdminChanges: No portal admin or ID... cannot add');
51                                         return;
52                                     }
53                                     portalAdminsService.addPortalAdmin(this.selectedUser.orgUserId)
54                                         .then(() => {
55                                             $log.debug("NewPortalAdminModalCtrl::addNewPortalAdmin: portal admin added successfully");
56                                             $scope.closeThisDialog(true);
57                                         }).catch(err => {
58                                         $log.error('NewPortalAdminModalCtrl::addNewPortalAdmin error status: ' + err.status);
59                                         confirmBoxService.showInformation('There was a unknown problem adding the portal admin. ' + 'Please try again later. Error Status: '+ err.status).then(function (isConfirmed) {});
60                                     });
61                                 }
62                             }).catch(err => {
63                             $log.error('portalAdminsService.addPortalAdmin error status: '+ err.status);
64                         });
65                     }
66                 }).catch(err=> {
67                     $log.error('NewPortalAdminModalCtrl::addNewPortalAdmin error getting portal admins list:',err);
68                 });
69             };
70
71             this.setSelectedUser = (user) => {
72                 $log.debug('NewPortalAdminModalCtrl::setSelectedUser: selected user: ', user);
73                 this.selectedUser = user;
74             };
75
76             init();
77
78             $scope.$on('$stateChangeStart', e => {
79                 e.preventDefault();
80             });
81         }
82     }
83     NewPortalAdminModalCtrl.$inject = ['$log', 'portalAdminsService', '$scope', 'confirmBoxService'];
84     angular.module('ecompApp').controller('NewPortalAdminModalCtrl', NewPortalAdminModalCtrl);
85 })();