808a66323b9aa7fcfa78a0c9102792038ed3c37b
[portal.git] / ecomp-portal-FE-common / client / app / services / users / users.service.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  * Created by doritrieur on 12/8/15.
22  */
23 'use strict';
24
25 (function () {
26     class UsersService {
27         constructor($q, $log, $http, conf, uuid, utilsService) {
28             this.$q = $q;
29             this.$log = $log;
30             this.$http = $http;
31             this.conf = conf;
32             this.uuid = uuid;
33             this.utilsService = utilsService;
34         }
35
36
37         searchUsers(queryString) {
38             let canceller = this.$q.defer();
39             let isActive = false;
40
41             let cancel = () => {
42                 if(isActive){
43                     this.$log.debug('UsersService::searchUsers: canceling the request');
44                     canceller.resolve();
45                 }
46             };
47
48             let promise = () => {
49                 let deferred = this.$q.defer();
50                 if(!queryString){
51                     return deferred.reject(new Error('query string is mandatory'));
52                 }
53                 isActive = true;
54                 this.$http({
55                     method: 'GET',
56                     url: this.conf.api.queryUsers,
57                     params: {search: queryString},
58                     cache: false,
59                     timeout: canceller.promise,
60                     headers: {
61                         'X-ECOMP-RequestID':this.uuid.generate()
62                     }
63                 }).then( res => {
64                     // If response comes back as a redirected HTML page which IS NOT a success
65                     if (this.utilsService.isValidJSON(res)== false) {
66                         deferred.reject('UsersService::queryUsers Failed');
67                     } else {
68                         //this.$log.info('UsersService::queryUsers Succeeded');
69                         isActive = false;
70                         deferred.resolve(res.data);
71                     }
72                 }).catch( status => {
73                     isActive = false;
74                     deferred.reject('UsersService::searchUsers:: API Failed with status: ' + status);
75                 });
76                 return deferred.promise;
77             };
78
79             return {
80                 cancel: cancel,
81                 promise: promise
82             };
83
84         }
85
86         getAccountUsers(appId) {
87             let deferred = this.$q.defer();
88             let log = this.$log;
89             // this.$log.debug('UsersService::getAccountUsers for appId: ' + appId);
90
91             let url = this.conf.api.accountUsers.replace(':appId', appId);
92             this.$http({
93                 method: 'GET',
94                 url: url,
95                 cache: false,
96                 headers: {
97                     'X-ECOMP-RequestID':this.uuid.generate()
98                 }
99             }).then( res => {
100                     // If response comes back as a redirected HTML page which IS NOT a success
101                 if (this.utilsService.isValidJSON(res)== false) {
102                         deferred.reject('UsersService::getAccountUsers for appId Failed');
103                     } else {
104                         // log.info('UsersService::getAccountUsers(appId) Succeeded');
105                         deferred.resolve(res.data);
106                     }
107                 })
108                 .catch( status => {
109                     log.error('getAccountUsers(appId) $http error = ', status);
110                     deferred.reject(status);
111                 });
112
113             return deferred.promise;
114         }
115
116         getUserAppRoles(appid, orgUserId, extRequestValue){
117                 let canceller = this.$q.defer();
118             let isActive = false;
119
120             let cancel = () => {
121                 if(isActive){
122                     this.$log.debug('UsersService::getUserAppRoles: canceling the request');
123                     canceller.resolve();
124                 }
125             };
126
127             let promise = () => {
128                 let deferred = this.$q.defer();
129                 isActive = false;
130             this.$http({
131                 method: 'GET',
132                 url: this.conf.api.userAppRoles,
133                 params: {user: orgUserId, app: appid, externalRequest: extRequestValue},
134                 cache: false,
135                 headers: {
136                     'X-ECOMP-RequestID':this.uuid.generate()
137                 }
138             }).then( res => {
139                     if (!this.utilsService.isValidJSON(res.data)) {
140                         deferred.reject('UsersService::getUserAppRoles: Failed');
141                     } else {
142                         isActive = false;
143                         deferred.resolve(res.data);
144                     }
145                 })
146                 .catch( status => {
147                         isActive = false;
148                     deferred.reject(status);
149                 });
150
151             return deferred.promise;
152             };
153             
154             return {
155                 cancel: cancel,
156                 promise: promise
157             };
158         }
159
160         updateUserAppRoles(newUserAppRoles) {
161 //            let deferred = this.$q.defer();
162             let canceller = this.$q.defer();
163             let isActive = false;
164
165             let cancel = () => {
166                 if(isActive){
167                     this.$log.debug('UsersService::updateUserAppRoles: canceling the request');
168                     canceller.resolve();
169                 }
170             };
171
172             // this.$log.info('UsersService::updateUserAppRoles');
173             let promise = () => {
174                 let deferred = this.$q.defer();
175             this.$http({
176                 method: 'PUT',
177                 url: this.conf.api.userAppRoles,
178                 data: newUserAppRoles,
179                 headers: {
180                     'X-ECOMP-RequestID':this.uuid.generate()
181                 }
182             }).then( res => {
183                 // this.$log.debug('getUserAppRoles response: ', JSON.stringify(res))
184                 // If response comes back as a redirected HTML page which IS NOT a success
185                 if (this.utilsService.isValidJSON(res)== false) {
186                     deferred.reject('UsersService::updateUserAppRoles: Failed');
187                 } else {
188                     // this.$log.info('UsersService::updateUserAppRoles: Succeeded');
189                     deferred.resolve(res.data);
190                 }
191             })
192             .catch( status => {
193                 deferred.reject(status);
194             });
195
196             return deferred.promise;
197             };
198             
199             return {
200                 cancel: cancel,
201                 promise: promise
202             };
203
204         }
205         
206         getLoggedInUser () {
207                 let deferred = this.$q.defer();
208                 this.$http({
209                         method: 'GET',
210                         url: this.conf.api.loggedinUser,
211                         cache: false,
212                         headers: {
213                                 'X-ECOMP-RequestID':this.uuid.generate(),
214                                 'Content-Type': 'application/json'
215                 },
216                 data: ''
217                 }).then( res => {
218                         if (res.data==null || !this.utilsService.isValidJSON(res.data)) {                       
219                                 deferred.reject('MenusService::getLoggedInUser rest call failed');
220                         } else {
221                                 if(res.data.status!='OK' && res.data.message!=null)
222                                 deferred.reject('MenusService::getLoggedInUser rest call failed ' + res.data.message);
223                                 else
224                                         deferred.resolve(res.data);
225                         }
226                 })
227                 .catch( status => {
228                         this.$log.error('MenusService::getLoggedInUser rejection:' + status);
229                         deferred.reject(status);
230                 });
231
232                 return deferred.promise;
233         }
234         
235         modifyLoggedInUser (profileDetail) {
236                 let deferred = this.$q.defer();
237                 this.$http({
238                         method: 'PUT',
239                         url: this.conf.api.modifyLoggedinUser,
240                         cache: false,
241                         headers: {
242                                 'X-ECOMP-RequestID':this.uuid.generate(),
243                                 'Content-Type': 'application/json'
244                 },
245                 data: profileDetail
246                 }).then( res => {
247                         if (res.data==null || !this.utilsService.isValidJSON(res.data)) {                       
248                                 deferred.reject('MenusService::getLoggedInUser rest call failed');
249                         } else {
250                                 if(res.data.status!='OK' && res.data.message!=null)
251                                 deferred.reject('MenusService::getLoggedInUser rest call failed ' + res.data.message);
252                                 else
253                                         deferred.resolve(res.data);
254                         }
255                 })
256                 .catch( status => {
257                         console.log(status);
258                         this.$log.error('MenusService::getLoggedInUser rejection:' + status);
259                         deferred.reject(status);
260                 });
261
262                 return deferred.promise;
263         }
264
265     }
266     UsersService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
267     angular.module('ecompApp').service('usersService', UsersService)
268 })();