c2067cc1567bb736a46049ff29f97607339fa07a
[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                    }
82                    else {
83                            // check password length complexity.
84                            var warningMsg = adminsService.isComplexPassword(this.newUser.loginPwd);
85                            if (warningMsg != null) {
86                                    confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
87                                    return;
88                            }
89                    } // password
90
91                    adminsService.addNewUser(this.newUser,'Yes').then(res=> {
92                                 
93                                         if(res.message == 'Record already exist'){
94                                                 
95                                                 this.showAddUser = true;
96                                     this.showSearch = false;
97                                                 $scope.userExist = true;
98                                                 
99                                         }else{
100                                                 
101                                                 $scope.userExist = false;
102                                                 this.selectedUser = this.newUser;
103                                                 this.selectedUser.orgUserId = this.newUser.loginId;
104                                                 this.searchUsersResults = [];
105                                                 this.searchUsersResults.push(this.newUser);
106                                                 this.showAddUser = false;
107                                                 this.showSearch = true;
108                                                 this.newUser ={
109                                                 firstName:'',
110                                                 lastName:'',
111                                                 emailAdress:'',
112                                                 middleName:'',
113                                                 loginId:'',
114                                                 loginPwd:'',
115                                     loginPwdCheck:''
116                                 };
117                         this.searchUserString ='';
118                                                 $scope.UserSearchsIsNull = false;                                               
119                                         }
120                                         
121         
122                        }).catch(err=> {
123                            $log.error('adminsService: addNewUser error:: ', err);
124                           // $scope.errMsg=err;
125                            confirmBoxService.showInformation('Add New User failed: ' + err);
126                           
127                        }).finally(() => {
128                            //this.isLoadingTable = false;
129                            
130                        });
131            }
132            
133             this.searchUsers = () => {
134                 this.isLoading = true;
135                 if(this.searchUsersInProgress){
136                     return;
137                 }
138                 this.selectedUser = null;
139                 this.searchUsersInProgress = true;
140                 this.searchUsersResults = null;
141
142                 let searchUsersReq = usersService.searchUsers(this.searchUserString);
143                 activeRequests.push(searchUsersReq);
144                 searchUsersReq.promise().then(usersList => {
145                     $log.debug('searchUsers found the following users: ', JSON.stringify(usersList));
146                     this.searchUsersResults = usersList;
147                     $log.debug('searchUsersResults length: ', usersList.length);
148                     if (usersList.length != 1) {
149                         $scope.txtResults = 'results'
150                     } else {
151                         $scope.txtResults = 'result'
152                     }
153                     $scope.UserSearchsIsNull=false;
154                 }).catch(err => {
155                     $log.error('SearchUsersCtrl.searchUsers: ' + err);
156                     $scope.UserSearchsIsNull=true;
157                 }).finally(() => {
158                     this.scrollApi.scrollTop();
159                     this.searchUsersInProgress = false;
160                     clearReq(searchUsersReq);
161                     this.isLoading = false;
162                 });
163             };
164
165             let init = () => {
166                 this.isLoading = false;
167                 this.searchUsersInProgress = false;
168             };
169
170             this.setSelectedUser = user => {
171                 this.selectedUser = user;
172             };
173
174             init();
175
176             $scope.$on('$destroy', () => {
177                 activeRequests.forEach(req => {
178                     req.cancel();
179                 });
180             });
181         }
182     }
183     SearchUsersCtrl.$inject = ['$log', 'usersService','adminsService', '$scope','confirmBoxService'];
184     angular.module('ecompApp').controller('SearchUsersCtrl', SearchUsersCtrl);
185 })();