3d728b64e23d8c717957d07cf30c8ada5f421f88
[portal.git] / ecomp-portal-FE-common / client / app / views / account-onboarding / account-add-details / account-add-details.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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 'use strict';
39 (function () {
40     class AccountAddDetailsCtrl {
41         constructor($scope, $log, $interval, basicAuthAccountService, $modalInstance, errorMessageByCode, ECOMP_URL_REGEX, $window, confirmBoxService, $cookies,items) {
42          
43            this.addEndpoint = () => {
44                   this.account.endpointList.push({
45                           valid: true
46                   }); 
47            }
48             let init = () => {
49                 this.account = [];
50                 this.account.endpointList = [];
51                 this.passwordMatched = true;
52                 this.dupliateName = false;
53                 this.emptyAccountName = false;
54                 this.emptyAccountUsername = false;
55                 this.accountList = items.list;
56                 
57                 if (items&& items.account) {
58                     this.isEditMode = true;
59                     this.account = _.clone(items.account);
60                     this.account.repassword = this.account.password;
61                     this.account.endpointList = this.account.endpoints;
62                     if(this.account.isActive == 'Y')
63                         this.account.active = true;
64                     else
65                         this.account.active = false;
66                 } else {
67                     this.isEditMode = false;
68                     this.account.active = true;
69                 } 
70                 console.log(this.account);
71             };
72             
73             let resetConflict = fieldName => {
74                 delete this.conflictMessages[fieldName];
75                 if($scope.widgetForm[fieldName]){
76                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
77                 }
78             };
79
80             
81           /*  this.closeThisDialog = () => {
82                 $scope.closeThisDialog(true);
83             }*/
84             
85             this.removeEndpointItem = (endpoint) => {
86                         for(var i = 0; i < this.account.endpointList.length; i++){
87                                 if(this.account.endpointList[i].name == endpoint.name){
88                                         this.account.endpointList.splice(i, 1);
89                                         return; 
90                                 }
91                         }
92             }
93             
94             this.updateUsername = () => {
95                 this.emptyAccountUsername = false;
96             }
97             
98             this.updateAccountName = () => {
99                 this.dupliateName = false;
100                 for(var i = 0; i < this.accountList.length; i++){
101                         if(this.accountList[i].applicationName == this.account.applicationName){
102                                 this.dupliateName = true;
103                                 return;
104                         }
105                 }
106             }
107             
108             this.updateAccountEndpoint = (endpoint) => {
109                 endpoint.valid = true;
110             }
111            
112             this.saveChanges = () => {
113
114                 var isValid = true;
115                 var r = /\/[^ "]+$/;
116                 
117                 for(var i = 0; i < this.account.endpointList.length; i++){
118                         if(this.account.endpointList[i].name == undefined
119                         || this.account.endpointList[i].name == null
120                         || this.account.endpointList[i].name == ""){
121                                 this.account.endpointList.splice(i, 1);
122                                 i--;
123                         }else{
124                                 if(!this.account.endpointList[i].name.startsWith("/")){
125                                         this.account.endpointList[i].name = "/" + this.account.endpointList[i].name;
126                                 }
127                                 if(!r.test(this.account.endpointList[i].name)){
128                                         this.account.endpointList[i].valid = false;
129                                         isValid = false;
130                                 }
131                                 
132                         }
133                 }
134                 
135                 if(this.account.applicationName == ''
136                 || this.account.applicationName == undefined){
137                         this.emptyAccountName = true;
138                         isValid = false;
139                 }
140                 
141                 if(this.account.username == ''
142                 || this.account.username == undefined){
143                         this.emptyAccountUsername = true;
144                     isValid = false;
145                 }
146                 
147                 if(this.dupliateName == true){
148                    isValid = false;
149                 }
150                 
151                 if(this.account.password != this.account.repassword){
152                         this.passwordMatched =  false;
153                         isValid = false;
154                 }
155                 
156                 if(!isValid)
157                         return;
158                 
159                 
160                 
161                 var active = 'N';
162                 if(this.account.active == true)
163                         active = 'Y';
164                 
165                 var newAccount = {
166                                 applicationName: this.account.applicationName,
167                                 username: this.account.username,
168                                 password: this.account.password,
169                                 endpoints: this.account.endpointList,
170                                 isActive: active
171                 };
172                 
173                 
174                 if(this.isEditMode){
175                         var message = "Are you sure you want to change '" + this.account.applicationName + "'?"
176                         confirmBoxService.editItem(message).then(isConfirmed => {
177                                 if(isConfirmed){
178                                         basicAuthAccountService.updateAccount(this.account.id, newAccount).then(() => {
179                                                 $modalInstance.close("confirmed");
180                                         });
181                                         }
182                         });
183                 }else{
184                         basicAuthAccountService.createAccount(newAccount).then(() => {
185                                  $modalInstance.close("confirmed");
186                         });
187                 }
188             }
189             
190             init();
191             $scope.$on('$stateChangeStart', e => {
192                 e.preventDefault();
193             });
194         }
195     }
196     AccountAddDetailsCtrl.$inject = ['$scope', '$log', '$interval', 'basicAuthAccountService', '$modalInstance', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window', 'confirmBoxService', '$cookies','items'];
197     angular.module('ecompApp').controller('AccountAddDetailsCtrl', AccountAddDetailsCtrl);
198 })();