Deliver centralized role management feature
[portal.git] / ecomp-portal-FE-common / client / app / views / account-onboarding / account-add-details / account-add-details.js
1 'use strict';
2 (function () {
3     class AccountAddDetailsCtrl {
4         constructor($scope, $log, $interval, basicAuthAccountService, errorMessageByCode, ECOMP_URL_REGEX, $window, confirmBoxService, $cookies,items) {
5          
6            this.addEndpoint = () => {
7                   this.account.endpointList.push({
8                           valid: true
9                   }); 
10            }
11             let init = () => {
12                 this.account = [];
13                 this.account.endpointList = [];
14                 this.passwordMatched = true;
15                 this.dupliateName = false;
16                 this.emptyAccountName = false;
17                 this.emptyAccountUsername = false;
18                 this.accountList = items.list;
19                 
20                 if (items&& items.account) {
21                     this.isEditMode = true;
22                     this.account = _.clone(items.account);
23                     this.account.repassword = this.account.password;
24                     this.account.endpointList = this.account.endpoints;
25                     if(this.account.isActive == 'Y')
26                         this.account.active = true;
27                     else
28                         this.account.active = false;
29                 } else {
30                     this.isEditMode = false;
31                     this.account.active = true;
32                 } 
33                 console.log(this.account);
34             };
35             
36             let resetConflict = fieldName => {
37                 delete this.conflictMessages[fieldName];
38                 if($scope.widgetForm[fieldName]){
39                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
40                 }
41             };
42
43             
44           /*  this.closeThisDialog = () => {
45                 $scope.closeThisDialog(true);
46             }*/
47             
48             this.removeEndpointItem = (endpoint) => {
49                         for(var i = 0; i < this.account.endpointList.length; i++){
50                                 if(this.account.endpointList[i].name == endpoint.name){
51                                         this.account.endpointList.splice(i, 1);
52                                         return; 
53                                 }
54                         }
55             }
56             
57             this.updateUsername = () => {
58                 this.emptyAccountUsername = false;
59             }
60             
61             this.updateAccountName = () => {
62                 this.dupliateName = false;
63                 for(var i = 0; i < this.accountList.length; i++){
64                         if(this.accountList[i].applicationName == this.account.applicationName){
65                                 this.dupliateName = true;
66                                 return;
67                         }
68                 }
69             }
70             
71             this.updateAccountEndpoint = (endpoint) => {
72                 endpoint.valid = true;
73             }
74            
75             this.saveChanges = () => {
76
77                 var isValid = true;
78                 var r = /\/[^ "]+$/;
79                 
80                 for(var i = 0; i < this.account.endpointList.length; i++){
81                         if(this.account.endpointList[i].name == undefined
82                         || this.account.endpointList[i].name == null
83                         || this.account.endpointList[i].name == ""){
84                                 this.account.endpointList.splice(i, 1);
85                                 i--;
86                         }else{
87                                 if(!this.account.endpointList[i].name.startsWith("/")){
88                                         this.account.endpointList[i].name = "/" + this.account.endpointList[i].name;
89                                 }
90                                 if(!r.test(this.account.endpointList[i].name)){
91                                         this.account.endpointList[i].valid = false;
92                                         isValid = false;
93                                 }
94                                 
95                         }
96                 }
97                 
98                 if(this.account.applicationName == ''
99                 || this.account.applicationName == undefined){
100                         this.emptyAccountName = true;
101                         isValid = false;
102                 }
103                 
104                 if(this.account.username == ''
105                 || this.account.username == undefined){
106                         this.emptyAccountUsername = true;
107                     isValid = false;
108                 }
109                 
110                 if(this.dupliateName == true){
111                    isValid = false;
112                 }
113                 
114                 if(this.account.password != this.account.repassword){
115                         this.passwordMatched =  false;
116                         isValid = false;
117                 }
118                 
119                 if(!isValid)
120                         return;
121                 
122                 
123                 
124                 var active = 'N';
125                 if(this.account.active == true)
126                         active = 'Y';
127                 
128                 var newAccount = {
129                                 applicationName: this.account.applicationName,
130                                 username: this.account.username,
131                                 password: this.account.password,
132                                 endpoints: this.account.endpointList,
133                                 isActive: active
134                 };
135                 
136                 
137                 if(this.isEditMode){
138                         var message = "Are you sure you want to change '" + this.account.applicationName + "'?"
139                         confirmBoxService.editItem(message).then(isConfirmed => {
140                                 if(isConfirmed){
141                                         basicAuthAccountService.updateAccount(this.account.id, newAccount).then(() => {
142                                 $scope.$dismiss('cancel');
143                                 $window.location.reload();
144
145                                         });
146                                         }
147                         });
148                 }else{
149                         basicAuthAccountService.createAccount(newAccount).then(() => {
150                         $scope.$dismiss('cancel');
151                         $window.location.reload();
152
153                         });
154                 }
155             }
156             
157             init();
158             $scope.$on('$stateChangeStart', e => {
159                 e.preventDefault();
160             });
161         }
162     }
163     AccountAddDetailsCtrl.$inject = ['$scope', '$log', '$interval', 'basicAuthAccountService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window', 'confirmBoxService', '$cookies','items'];
164     angular.module('ecompApp').controller('AccountAddDetailsCtrl', AccountAddDetailsCtrl);
165 })();