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