nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / 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             
31             this.showAddUser = false;
32             this.showSearch = true;
33             this.newUser ={
34                                 firstName:'',
35                                 lastName:'',
36                                 emailAddress:'',
37
38                                 middleName:'',
39                                 loginId:'',
40                                 loginPwd:'',
41                     loginPwdCheck:''
42                 };
43
44             let activeRequests = [];
45             let clearReq = (req) => {
46                 activeRequests.splice(activeRequests.indexOf(req), 1);
47             };
48            
49             this.showAddUserSection = () => {
50                  this.showAddUser = true;
51                          this.showSearch = false;
52             }
53             
54            this.addNewUserFun = () => {
55                    if (this.newUser.loginId =='' || this.newUser.loginPwd == '' || this.newUser.firstName == '' || this.newUser.lastName =='' || this.newUser.emailAddress ==''||this.newUser.loginPwd ==''){
56                            var warningMsg = "Please enter a value for all fields marked with *.";
57                            confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
58                            return;
59                    } else if (this.newUser.loginPwd != this.newUser.loginPwdCheck) {
60                            var warningMsg = "Passwords do not match, please try again.";
61                            confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
62                            return;
63                    }
64                    else {
65                            // check password length complexity.
66                            var warningMsg = adminsService.isComplexPassword(this.newUser.loginPwd);
67                            if (warningMsg != null) {
68                                    confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});
69                                    return;
70                            }
71                    } // password
72
73                    adminsService.addNewUser(this.newUser,'Yes').then(res=> {
74                                 
75                                         if(res.message == 'Record already exist'){
76                                                 
77                                                 this.showAddUser = true;
78                                     this.showSearch = false;
79                                                 $scope.userExist = true;
80                                                 
81                                         }else{
82                                                 
83                                                 $scope.userExist = false;
84                                                 this.selectedUser = this.newUser;
85                                                 this.selectedUser.orgUserId = this.newUser.loginId;
86                                                 this.searchUsersResults = [];
87                                                 this.searchUsersResults.push(this.newUser);
88                                                 this.showAddUser = false;
89                                                 this.showSearch = true;
90                                                 this.newUser ={
91                                                 firstName:'',
92                                                 lastName:'',
93                                                 emailAdress:'',
94                                                 middleName:'',
95                                                 loginId:'',
96                                                 loginPwd:'',
97                                     loginPwdCheck:''
98                                 };
99                         this.searchUserString ='';
100                                                 $scope.UserSearchsIsNull = false;                                               
101                                         }
102                                         
103         
104                        }).catch(err=> {
105                            $log.error('adminsService: addNewUser error:: ', err);
106                           // $scope.errMsg=err;
107                            confirmBoxService.showInformation('Add New User failed: ' + err);
108                           
109                        }).finally(() => {
110                            //this.isLoadingTable = false;
111                            
112                        });
113            }
114            
115             this.searchUsers = () => {
116                 this.isLoading = true;
117                 if(this.searchUsersInProgress){
118                     return;
119                 }
120                 this.selectedUser = null;
121                 this.searchUsersInProgress = true;
122                 this.searchUsersResults = null;
123
124                 let searchUsersReq = usersService.searchUsers(this.searchUserString);
125                 activeRequests.push(searchUsersReq);
126                 searchUsersReq.promise().then(usersList => {
127                     $log.debug('searchUsers found the following users: ', JSON.stringify(usersList));
128                     this.searchUsersResults = usersList;
129                     $scope.UserSearchsIsNull=false;
130                 }).catch(err => {
131                     $log.error('SearchUsersCtrl.searchUsers: ' + err);
132                     $scope.UserSearchsIsNull=true;
133                 }).finally(() => {
134                     this.scrollApi.scrollTop();
135                     this.searchUsersInProgress = false;
136                     clearReq(searchUsersReq);
137                     this.isLoading = false;
138                 });
139             };
140
141             let init = () => {
142                 this.isLoading = false;
143                 this.searchUsersInProgress = false;
144             };
145
146             this.setSelectedUser = user => {
147                 this.selectedUser = user;
148             };
149
150             init();
151
152             $scope.$on('$destroy', () => {
153                 activeRequests.forEach(req => {
154                     req.cancel();
155                 });
156             });
157         }
158     }
159     SearchUsersCtrl.$inject = ['$log', 'usersService','adminsService', '$scope','confirmBoxService'];
160     angular.module('ecompApp').controller('SearchUsersCtrl', SearchUsersCtrl);
161 })();