Fix typo in addNewUser()
[portal.git] / ecomp-portal-FE-os / client / src / directives / search-users / search-users.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 'use strict';
39 (function () {
40     class SearchUsersCtrl {
41
42         constructor($log, usersService,adminsService, $scope,confirmBoxService) {
43
44             $scope.UserSearchsIsNull=false;
45             $scope.userExist = false;
46             this.scrollApi = {};//scrollTop directive
47             $scope.txtResults = 'result';
48              
49             this.showAddUser = false;
50             this.showSearch = true;
51             this.newUser ={
52                                 firstName:'',
53                                 lastName:'',
54                                 emailAddress:'',
55
56                                 middleName:'',
57                                 loginId:'',
58                                 loginPwd:'',
59                     loginPwdCheck:''
60                 };
61
62             let activeRequests = [];
63             let clearReq = (req) => {
64                 activeRequests.splice(activeRequests.indexOf(req), 1);
65             };
66            
67             this.showAddUserSection = () => {
68                  this.showAddUser = true;
69                          this.showSearch = false;
70             }
71             
72            this.addNewUserFun = () => {
73                    if (this.newUser.loginId =='' || this.newUser.loginPwd == '' || this.newUser.firstName == '' || this.newUser.lastName =='' || this.newUser.emailAddress ==''||this.newUser.loginPwd ==''){
74                            var warningMsg = "Please enter a value for all fields marked with *.";
75                            confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
76                            return;
77                    } else if (this.newUser.loginPwd != this.newUser.loginPwdCheck) {
78                            var warningMsg = "Passwords do not match, please try again.";
79                            confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
80                            return;
81                    } else if(/[^a-zA-Z0-9\-\@\_]/.test(this.newUser.loginId)){
82                            var warningMsg = "Login ID should not contain special characters except '@', '-', and '_'.";
83                            confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
84                            return;
85                    }
86                    else {
87                            // check password length complexity.
88                            var warningMsg = adminsService.isComplexPassword(this.newUser.loginPwd);
89                            if (warningMsg != null) {
90                                    confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
91                                    return;
92                            }
93                    } // password
94
95                    adminsService.addNewUser(this.newUser,'Yes').then(res=> {
96                                 
97                                         if(res.message == 'Record already exist'){
98                                                 
99                                                 this.showAddUser = true;
100                                     this.showSearch = false;
101                                                 $scope.userExist = true;
102                                                 
103                                         }else{
104                                                 
105                                                 $scope.userExist = false;
106                                                 this.selectedUser = this.newUser;
107                                                 this.selectedUser.orgUserId = this.newUser.loginId;
108                                                 this.searchUsersResults = [];
109                                                 this.searchUsersResults.push(this.newUser);
110                                                 this.showAddUser = false;
111                                                 this.showSearch = true;
112                                                 this.newUser ={
113                                                 firstName:'',
114                                                 lastName:'',
115                                                 emailAddress:'',
116                                                 middleName:'',
117                                                 loginId:'',
118                                                 loginPwd:'',
119                                     loginPwdCheck:''
120                                 };
121                         this.searchUserString ='';
122                                                 $scope.UserSearchsIsNull = false;                                               
123                                         }
124                                         
125         
126                        }).catch(err=> {
127                            $log.error('adminsService: addNewUser error:: ', err);
128                           // $scope.errMsg=err;
129                            confirmBoxService.showInformation('Add New User failed: ' + err);
130                           
131                        }).finally(() => {
132                            //this.isLoadingTable = false;
133                            
134                        });
135            }
136            
137             this.searchUsers = () => {
138                 this.isLoading = true;
139                 if(this.searchUsersInProgress){
140                     return;
141                 }
142                 this.selectedUser = null;
143                 this.searchUsersInProgress = true;
144                 this.searchUsersResults = null;
145
146                 let searchUsersReq = usersService.searchUsers(this.searchUserString);
147                 activeRequests.push(searchUsersReq);
148                 searchUsersReq.promise().then(usersList => {
149                     $log.debug('searchUsers found the following users: ', JSON.stringify(usersList));
150                     this.searchUsersResults = usersList;
151                     $log.debug('searchUsersResults length: ', usersList.length);
152                     if (usersList.length != 1) {
153                         $scope.txtResults = 'results'
154                     } else {
155                         $scope.txtResults = 'result'
156                     }
157                     $scope.UserSearchsIsNull=false;
158                 }).catch(err => {
159                     $log.error('SearchUsersCtrl.searchUsers: ' + err);
160                     $scope.UserSearchsIsNull=true;
161                 }).finally(() => {
162                     this.scrollApi.scrollTop();
163                     this.searchUsersInProgress = false;
164                     clearReq(searchUsersReq);
165                     this.isLoading = false;
166                 });
167             };
168
169             let init = () => {
170                 this.isLoading = false;
171                 this.searchUsersInProgress = false;
172             };
173
174             this.setSelectedUser = user => {
175                 this.selectedUser = user;
176             };
177
178             init();
179
180             $scope.$on('$destroy', () => {
181                 activeRequests.forEach(req => {
182                     req.cancel();
183                 });
184             });
185         }
186     }
187     SearchUsersCtrl.$inject = ['$log', 'usersService','adminsService', '$scope','confirmBoxService'];
188     angular.module('ecompApp').controller('SearchUsersCtrl', SearchUsersCtrl);
189 })();