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