nexus site path corrected
[portal.git] / ecomp-portal-FE / 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 'use strict';
22
23 (function () {
24     class UsersService {
25         constructor($q, $log, $http, conf,uuid) {
26             this.$q = $q;
27             this.$log = $log;
28             this.$http = $http;
29             this.conf = conf;
30             this.uuid = uuid;
31         }
32
33
34         searchUsers(queryString) {
35             let canceller = this.$q.defer();
36             let isActive = false;
37
38             let cancel = () => {
39                 if(isActive){
40                     this.$log.debug('UsersService::searchUsers: canceling the request');
41                     canceller.resolve();
42                 }
43             };
44
45             let promise = () => {
46                 let deferred = this.$q.defer();
47                 if(!queryString){
48                     return deferred.reject(new Error('query string is mandatory'));
49                 }
50                 isActive = true;
51                 this.$http({
52                     method: "GET",
53                     url: this.conf.api.queryUsers,
54                     params: {search: queryString},
55                     cache: false,
56                     timeout: canceller.promise,
57                     headers: {
58                         'X-ECOMP-RequestID':this.uuid.generate()
59                     }
60                 }).then( res => {
61                     if (Object.keys(res.data).length == 0) {
62                         deferred.reject("UsersService::queryUsers Failed");
63                     } else {
64                         this.$log.info('UsersService::queryUsers Succeeded');
65                         isActive = false;
66                         deferred.resolve(res.data);
67                     }
68                 }).catch( status => {
69                     isActive = false;
70                     deferred.reject('UsersService::searchUsers:: API Failed with status: ' + status);
71                 });
72                 return deferred.promise;
73             };
74
75             return {
76                 cancel: cancel,
77                 promise: promise
78             };
79
80         }
81
82         getAccountUsers(appId) {
83             let deferred = this.$q.defer();
84             let log = this.$log;
85             this.$log.debug('UsersService::getAccountUsers for appId: ' + appId);
86
87             let url = this.conf.api.accountUsers.replace(':appId', appId);
88             this.$http({
89                 method: "GET",
90                 url: url,
91                 cache: false,
92                 headers: {
93                     'X-ECOMP-RequestID':this.uuid.generate()
94                 }
95             }).then( res => {
96              if (Object.keys(res.data).length == 0) {
97                     deferred.reject("UsersService::getAccountUsers for appId Failed");
98                 } else {
99                     log.info('UsersService::getAccountUsers(appId) Succeeded');
100                     deferred.resolve(res.data);
101                 }
102             })
103             .catch( status => {
104                 log.error('getAccountUsers(appId) $http error = ', status);
105                 deferred.reject(status);
106             });
107             return deferred.promise;
108         }
109
110         getUserAppRoles(appid, orgUserId){
111             let deferred = this.$q.defer();
112             this.$log.info('UsersService::getUserAppRoles');
113
114             this.$http({
115                 method: "GET",
116                 url: this.conf.api.userAppRoles,
117                 params: {orgUserId: orgUserId, app: appid},
118                 cache: false,
119                 headers: {
120                     'X-ECOMP-RequestID':this.uuid.generate()
121                 }
122             }).then( res => {
123                 if (Object.keys(res.data).length == 0) {
124                     deferred.reject("UsersService::getUserAppRoles: Failed");
125                 } else {
126                     this.$log.info('UsersService::getUserAppRoles: Succeeded');
127                     deferred.resolve(res.data);
128                 }
129             })
130             .catch( status => {
131                 this.$log.debug("UsersService::getUserAppRoles: status.headers(FEErrorString)");
132                 this.$log.debug('UsersService::getUserAppRoles status.headers(FEErrorString)', status.headers('FEErrorString'));
133                 deferred.reject(status);
134             });
135             return deferred.promise;
136         }
137
138         updateUserAppRoles(newUserAppRoles) {
139             let deferred = this.$q.defer();
140             this.$log.info('UsersService::updateUserAppRoles');
141             this.$http({
142                 method: "PUT",
143                 url: this.conf.api.userAppRoles,
144                 data: newUserAppRoles,
145                 headers: {
146                     'X-ECOMP-RequestID':this.uuid.generate()
147                 }
148             }).then( res => {
149                     deferred.resolve(res.data);
150                 })
151                 .catch( status => {
152                     deferred.reject(status);
153                 });
154
155             return deferred.promise;
156         }
157
158     }
159     UsersService.$inject = ['$q', '$log', '$http', 'conf','uuid4'];
160     angular.module('ecompApp').service('usersService', UsersService)
161 })();