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