[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / portal-admin / new-portal-admin / new-portal-admin.controller.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 /**\r
21  * Created by nnaffar on 12/8/15.\r
22  */\r
23 'use strict';\r
24 (function () {\r
25     class NewPortalAdminModalCtrl {\r
26         constructor($log, portalAdminsService, $scope, confirmBoxService) {\r
27 \r
28             let init = () => {\r
29                 this.isSaving = false;\r
30                 /* istanbul ignore if */\r
31                 if($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState){\r
32                     this.selectedUser = $scope.ngDialogData.selectedUser;\r
33                     this.dialogState = $scope.ngDialogData.dialogState;\r
34                 }else{\r
35                     this.selectedUser = null;\r
36                     this.dialogState = 1;\r
37                 }\r
38                 //this.searchUsersInProgress = false;\r
39                 //this.showNewAdminAppDropdown = false;\r
40                 $log.info('NewPortalAdminModalCtrl:: initiated');\r
41             };\r
42 \r
43             this.addNewPortalAdmin = () => {\r
44                 confirmBoxService.makeAdminChanges('Are you sure you want to add "' + this.selectedUser.firstName + ' ' + this.selectedUser.lastName + '" as a Portal Admin?')\r
45                     .then(isConfirmed => {\r
46                         if(isConfirmed) {\r
47                             if (!this.selectedUser || !this.selectedUser.orgUserId) {\r
48                                 $log.error('NewPortalAdminModalCtrl::makeAdminChanges: No portal admin or ID... cannot add');\r
49                                 return;\r
50                             }\r
51                             portalAdminsService.addPortalAdmin(this.selectedUser.orgUserId)\r
52                                 .then(() => {\r
53                                     $log.debug("NewPortalAdminModalCtrl::addNewPortalAdmin: portal admin added successfully");\r
54                                     $scope.closeThisDialog(true);\r
55                                 }).catch(err => {\r
56                                     if(err.status === 409) {    //Conflict\r
57                                         confirmBoxService.showInformation('This user already exists as a portal admin!').then(function (isConfirmed) {\r
58                                             $scope.closeThisDialog(true);\r
59                                         });\r
60                                     } else {\r
61                                         confirmBoxService.showInformation('There was a unknown problem adding the portal admin. ' + 'Please try again later. Error Status: '+ err.status).then(function (isConfirmed)  {\r
62                                             $scope.closeThisDialog(true);\r
63                                         });\r
64                                     }\r
65                             });\r
66                         }\r
67                     }).catch(err => {\r
68                         confirmBoxService.showInformation('There was a unknown problem adding the portal admin. ' + 'Please try again later. Error Status: '+ err.status).then(function (isConfirmed)  {\r
69                             $scope.closeThisDialog(true);\r
70                         });\r
71                         $log.error('portalAdminsService.addPortalAdmin error status: '+ err.status);\r
72                 });\r
73             };\r
74 \r
75             /**\r
76              * this function set the selected user\r
77              * @param user: selected user object\r
78              */\r
79             this.setSelectedUser = (user) => {\r
80                 $log.debug('NewPortalAdminModalCtrl::setSelectedUser: selected user: ', user);\r
81                 this.selectedUser = user;\r
82             };\r
83 \r
84             init();\r
85 \r
86             $scope.$on('$stateChangeStart', e => {\r
87                 //Disable navigation when modal is opened\r
88                 //**Nabil - note: this will cause the history back state to be replaced with current state\r
89                 e.preventDefault();\r
90             });\r
91         }\r
92     }\r
93     NewPortalAdminModalCtrl.$inject = ['$log', 'portalAdminsService', '$scope', 'confirmBoxService'];\r
94     angular.module('ecompApp').controller('NewPortalAdminModalCtrl', NewPortalAdminModalCtrl);\r
95 })();\r