Bulk upload changes and music health check apis
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-create-edit-popup-controller.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 app.controller('roleCreateEditController',function($scope, conf, $http, $modalInstance, confirmBoxService, message, RoleService) {
39         $scope.isGlobalRole = false; 
40         $scope.availableRoles = message.availableRoles;
41         $scope.roleFunctions = message.availableRoleFunctions;
42         $scope.appId = message.appId;
43         if($scope.appId == 1){
44                 $scope.isGlobalRole = true;
45         }
46         $scope.role = message.role;
47         $scope.isGlobalRoleChecked={
48                         isChecked : false
49         }
50         if(message.role.name)
51                 $scope.isGlobalRoleChecked.isChecked=(message.role.name.indexOf('global_')==-1)?false:true;             
52         $scope.availableRoleFunctions=[];
53         $scope.finalSelectedRoleFunctions=[];
54
55         if($scope.roleFunctions)
56                 for(var i=0; i< $scope.roleFunctions.length; i++){
57                         var availableRoleFunction = $scope.roleFunctions[i];
58                         availableRoleFunction.selected = false;
59                         for(var j=0; j< $scope.role.roleFunctions.length; j++){
60                                 if($scope.roleFunctions[i].code === $scope.role.roleFunctions[j].code
61                                                 && $scope.roleFunctions[i].type === $scope.role.roleFunctions[j].type
62                                                 && $scope.roleFunctions[i].action === $scope.role.roleFunctions[j].action) {
63                                         availableRoleFunction.selected = true;
64                                         console.log(availableRoleFunction.selected);
65                                 }
66                         }
67                         $scope.availableRoleFunctions.push(availableRoleFunction);          
68                 }
69         
70                 $scope.toggleRoleFunction = function(selected,selectedRoleFunction){
71                         if($scope.roleFunctions){
72                                 for(var i=0; i< $scope.roleFunctions.length; i++){
73                                         var availableRoleFunction = $scope.roleFunctions[i];                                    
74                                                 if(availableRoleFunction.selected){
75                                                         $scope.finalSelectedRoleFunctions.push(availableRoleFunction);
76                                                 }
77                                 }
78                         }
79                         if(!selected) {
80                                 for(var i=0; i<$scope.finalSelectedRoleFunctions.length; i++){
81                                         var availableRoleFunction = $scope.finalSelectedRoleFunctions[i];
82                                         if(availableRoleFunction.code == selectedRoleFunction.code
83                                                         && availableRoleFunction.type == selectedRoleFunction.type
84                                                         && availableRoleFunction.action == selectedRoleFunction.action){
85                                                 $scope.finalSelectedRoleFunctions.splice(i, 1);
86                                         }
87                                 }                                       
88                         }
89                 }
90                 
91                 
92                 $scope.saveRole = function() {                  
93                         var exists = false,x;   
94                         for(x in $scope.availableRoles){
95                                 if($scope.availableRoles[x].name==$scope.role.name){
96                                         exists = true;
97                                 }
98                         }
99                         if (exists) {
100                                 confirmBoxService.showInformation( "Role already exists.");
101                         }                       
102                         else {  
103                                 console.log($scope.role);
104                                 var uuu = conf.api.saveRole.replace(':appId', $scope.appId);
105                                 if($scope.isGlobalRoleChecked.isChecked ){
106                                         $scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
107                                         saveOrUpdateRole(uuu);
108                                 }else{
109                                         var roleName = $scope.role.name.toLowerCase();
110                                         if(roleName.includes('global_')){
111                                                 confirmBoxService.showInformation('Global prefix:"global_" can only be used when the global flag is checked for the role name:' +$scope.role.name+ '. Please try again!' ).then(confirmed => {
112                                                 });
113                                         } else{
114                                                 saveOrUpdateRole(uuu);
115                                         }
116                                 }
117                         }
118                 };
119                 
120         let saveOrUpdateRole = (uuu) => {
121                         //overriding the final list of rolefunctions to role
122                         if($scope.finalSelectedRoleFunctions.length > 0)
123                                 $scope.role.roleFunctions = $scope.finalSelectedRoleFunctions;
124                         var postData = {
125                                         role: $scope.role,
126                                         childRoles: $scope.role.childRoles,
127                                         roleFunctions :  $scope.role.roleFunctions
128                         };
129                         
130                         $http.post(uuu, postData).then(function(res) {
131                                 if (res && res.data && res.data.role){
132                                         confirmBoxService.showInformation("Update Successful.").then(confirmed => {
133                                                 $modalInstance.close("confirmed");
134                                         });
135                                 }
136                                 
137                                 else{
138                                         confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
139                                 }
140                         },
141                         function(res){
142                                 console.log('post failed', res.data);
143                                 if (res.data.includes('BAD_REQUEST')){
144                                         confirmBoxService.showInformation("Error while saving." + "BAD_REQUEST");
145                                         }
146                                 else{
147                                         confirmBoxService.showInformation("Error while saving." + res.data.error);
148                                         }                                       
149                         }
150                         );
151                 }
152                 
153 })