[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-os / client / src / directives / search-users / search-users.controller.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20
21 'use strict';
22 (function () {
23     class SearchUsersCtrl {
24
25         constructor($log, usersService,adminsService, $scope,confirmBoxService) {
26
27             $scope.UserSearchsIsNull=false;
28             $scope.userExist = false;
29             this.scrollApi = {};//scrollTop directive
30             $scope.txtResults = 'result';
31              
32             this.showAddUser = false;
33             this.showSearch = true;
34             this.newUser ={
35                                 firstName:'',
36                                 lastName:'',
37                                 emailAddress:'',
38
39                                 middleName:'',
40                                 loginId:'',
41                                 loginPwd:'',
42                     loginPwdCheck:''
43                 };
44
45             let activeRequests = [];
46             let clearReq = (req) => {
47                 activeRequests.splice(activeRequests.indexOf(req), 1);
48             };
49            
50             this.showAddUserSection = () => {
51                  this.showAddUser = true;
52                          this.showSearch = false;
53             }
54             
55            this.addNewUserFun = () => {
56                    if (this.newUser.loginId =='' || this.newUser.loginPwd == '' || this.newUser.firstName == '' || this.newUser.lastName =='' || this.newUser.emailAddress ==''||this.newUser.loginPwd ==''){
57                            var warningMsg = "Please enter a value for all fields marked with *.";
58                            confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
59                            return;
60                    } else if (this.newUser.loginPwd != this.newUser.loginPwdCheck) {
61                            var warningMsg = "Passwords do not match, please try again.";
62                            confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
63                            return;
64                    }
65                    else {
66                            // check password length complexity.
67                            var warningMsg = adminsService.isComplexPassword(this.newUser.loginPwd);
68                            if (warningMsg != null) {
69                                    confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
70                                    return;
71                            }
72                    } // password
73
74                    adminsService.addNewUser(this.newUser,'Yes').then(res=> {
75                                 
76                                         if(res.message == 'Record already exist'){
77                                                 
78                                                 this.showAddUser = true;
79                                     this.showSearch = false;
80                                                 $scope.userExist = true;
81                                                 
82                                         }else{
83                                                 
84                                                 $scope.userExist = false;
85                                                 this.selectedUser = this.newUser;
86                                                 this.selectedUser.orgUserId = this.newUser.loginId;
87                                                 this.searchUsersResults = [];
88                                                 this.searchUsersResults.push(this.newUser);
89                                                 this.showAddUser = false;
90                                                 this.showSearch = true;
91                                                 this.newUser ={
92                                                 firstName:'',
93                                                 lastName:'',
94                                                 emailAdress:'',
95                                                 middleName:'',
96                                                 loginId:'',
97                                                 loginPwd:'',
98                                     loginPwdCheck:''
99                                 };
100                         this.searchUserString ='';
101                                                 $scope.UserSearchsIsNull = false;                                               
102                                         }
103                                         
104         
105                        }).catch(err=> {
106                            $log.error('adminsService: addNewUser error:: ', err);
107                           // $scope.errMsg=err;
108                            confirmBoxService.showInformation('Add New User failed: ' + err);
109                           
110                        }).finally(() => {
111                            //this.isLoadingTable = false;
112                            
113                        });
114            }
115            
116             this.searchUsers = () => {
117                 this.isLoading = true;
118                 if(this.searchUsersInProgress){
119                     return;
120                 }
121                 this.selectedUser = null;
122                 this.searchUsersInProgress = true;
123                 this.searchUsersResults = null;
124
125                 let searchUsersReq = usersService.searchUsers(this.searchUserString);
126                 activeRequests.push(searchUsersReq);
127                 searchUsersReq.promise().then(usersList => {
128                     $log.debug('searchUsers found the following users: ', JSON.stringify(usersList));
129                     this.searchUsersResults = usersList;
130                     $log.debug('searchUsersResults length: ', usersList.length);
131                     if (usersList.length != 1) {
132                         $scope.txtResults = 'results'
133                     } else {
134                         $scope.txtResults = 'result'
135                     }
136                     $scope.UserSearchsIsNull=false;
137                 }).catch(err => {
138                     $log.error('SearchUsersCtrl.searchUsers: ' + err);
139                     $scope.UserSearchsIsNull=true;
140                 }).finally(() => {
141                     this.scrollApi.scrollTop();
142                     this.searchUsersInProgress = false;
143                     clearReq(searchUsersReq);
144                     this.isLoading = false;
145                 });
146             };
147
148             let init = () => {
149                 this.isLoading = false;
150                 this.searchUsersInProgress = false;
151             };
152
153             this.setSelectedUser = user => {
154                 this.selectedUser = user;
155             };
156
157             init();
158
159             $scope.$on('$destroy', () => {
160                 activeRequests.forEach(req => {
161                     req.cancel();
162                 });
163             });
164         }
165     }
166     SearchUsersCtrl.$inject = ['$log', 'usersService','adminsService', '$scope','confirmBoxService'];
167     angular.module('ecompApp').controller('SearchUsersCtrl', SearchUsersCtrl);
168 })();