36e6d6e3d8b6a6991b3494a92b5ceea9fcb6b61d
[portal.git] / ecomp-portal-FE-os / client / src / services / userProfile / userProfile.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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 'use strict';
39
40 (function () {
41     class UserProfileService {
42         
43         constructor($q, $log, $http, conf, uuid, utilsService) {
44             this.$q = $q;
45             this.$log = $log;
46             this.$http = $http;
47             this.conf = conf;
48             this.uuid = uuid;
49             this.utilsService = utilsService;
50             this.userProfile = null;
51             this.debug = false;
52         }
53
54         getUserProfile() {
55             if (this.debug)
56                 this.$log.debug('UserProfileService::getUserProfile: get logged user profile');
57             let deferred = this.$q.defer();
58             let url = this.conf.api.userProfile;
59             this.$http({
60                     method: "GET",
61                     url: url,
62                     cache: false,
63                     headers: {
64                         'X-ECOMP-RequestID':this.uuid.generate()
65                     }
66                 })
67                 .then( res => {
68                         if (this.debug)
69                                 this.$log.debug('UserProfileService::getUserProfile: result is ' + JSON.stringify(res));
70                     // Res is always JSON, but the data object might be an HTML error page.
71                     if (! this.utilsService.isValidJSON(res.data)) {
72                         var msg = 'UserProfileService::getUserProfile: result data is not JSON';
73                         if (this.debug)
74                                 this.$log.debug(msg);
75                         deferred.reject(msg);
76                     } else {
77                         if (this.debug)
78                                 this.$log.debug('UserProfileService::getUserProfile: success');
79                         this.userProfile = res.data;
80                         deferred.resolve(res.data);
81                     }
82                 })
83                 .catch( status => {
84                     this.$log.error('UserProfileService::getUserProfile caught exception: ' + JSON.stringify(status));
85                     deferred.reject(status);
86                 });
87             return deferred.promise;
88         }
89         
90         getSortedUserIdCombination(user1, user2) {
91                 
92                 var combination = "";
93                 if(user1<user2) {
94                         combination =  user1+"-"+user2;
95                 } else if (user1>user2){
96                         combination =  user2+"-"+user1;
97                 } else {
98                         
99                         return "self";
100                 }
101                 
102                 var collaborateUrl =  'opencollaboration?chat_id=' + combination ;
103                 
104                 return collaborateUrl;
105                 
106                 
107                 
108         }
109         
110         getFunctionalMenuStaticInfo() {
111                 if (this.debug)
112                         this.$log.debug('UserProfileService::getFunctionalMenuStaticInfo: start');
113             let deferred = this.$q.defer();
114             let url = this.conf.api.functionalMenuStaticInfo;
115             this.$http({
116                     method: "GET",
117                     url : url,
118                     cache: false,
119                     headers: {
120                         'X-ECOMP-RequestID':this.uuid.generate()
121                     }
122                 }).then( res => {
123                         if (this.debug)
124                                 this.$log.debug('UserProfileService::getFunctionalMenuStaticInfo: result is ' + JSON.stringify(res));
125                     // Res is always JSON, but the data object might be an HTML error page.
126                     if (! this.utilsService.isValidJSON(res.data)) {
127                         var msg = 'UserProfileService::getFunctionalMenuStaticInfo: result data is not JSON';
128                         if (this.debug)
129                                 this.$log.debug(msg);
130                         deferred.reject(msg);
131                     } else {
132                         if (this.debug)
133                                 this.$log.debug('UserProfileService::getFunctionalMenuStaticInfo Succeeded');
134                         // saving the userId at session level
135                         sessionStorage.userId = res.data.userId;
136                         deferred.resolve(res.data);                        
137                     }
138                 }).catch( status => {
139                     this.$log.error('UserProfileService::getFunctionalMenuStaticInfo caught exception: ' + JSON.stringify(status));
140                     deferred.reject(status);
141                 });
142             return deferred.promise;
143         }
144         
145         getActiveUser() {       
146             if (this.debug)
147                 this.$log.debug('UserProfileService::getActiveUser: start');
148             let deferred = this.$q.defer();
149             let url = this.conf.api.getActiveUser;           
150             this.$http({
151                     method: 'GET',
152                     url : url,
153                     cache: false,
154                     headers: {
155                         'X-ECOMP-RequestID':  this.uuid.generate()
156                     }
157                 }).then( res => {
158                         if (this.debug) {
159                                 this.$log.debug('UserProfileService::getActiveUser: result is ' + JSON.stringify(res));
160                                 this.$log.debug('UserProfileService::getActiveUser: isValidJSON is ' + this.utilsService.isValidJSON(res.data));
161                         }
162                     // Res is always JSON, but the data object might be an HTML error page.
163                         // res.data should be a list of Org IDs; an empty list is NOT an error.
164                     if (! this.utilsService.isValidJSON(res.data)) {
165                         var msg = 'UserProfileService::getActiveUser: result data is not JSON';
166                         if (this.debug)
167                                 this.$log.debug(msg);
168                         deferred.reject(msg);
169                     }
170                     else {
171                         if (this.debug)
172                                 this.$log.debug('UserProfileService::getActiveUser: success');
173                         deferred.resolve(res.data);
174                     }
175                 }).catch( status => {
176                     this.$log.error('UserProfileService::getActiveUser caught exception: ' + JSON.stringify(status));
177                     deferred.reject(status);
178                 });
179             return deferred.promise;
180         }
181         
182         
183         getUserAppRoles(userId) {
184             if (this.debug)
185                 this.$log.debug('UserProfileService::getUserAppRoles: get logged user profile');
186             let deferred = this.$q.defer();
187             this.$http({
188                     method: "GET",
189                     url: this.conf.api.userApplicationRoles,
190                     params: {userId:userId},
191                     cache: false,
192                     headers: {
193                         'X-ECOMP-RequestID':this.uuid.generate()
194                     }
195                 })
196                 .then( res => {
197                         if (this.debug)
198                                 this.$log.debug('UserProfileService::getUserAppRoles: result is ' + JSON.stringify(res));
199                     // Res is always JSON, but the data object might be an HTML error page.
200                     if (! this.utilsService.isValidJSON(res.data)) {
201                         var msg = 'UserProfileService::getUserAppRoles: result data is not JSON';
202                         if (this.debug)
203                                 this.$log.debug(msg);
204                         deferred.reject(msg);
205                     } else {
206                         if (this.debug)
207                                 this.$log.debug('UserProfileService::getUserAppRoles: success');
208                         deferred.resolve(res.data);
209                     }
210                 })
211                 .catch( status => {
212                     this.$log.error('UserProfileService::getUserAppRoles caught exception: ' + JSON.stringify(status));
213                     deferred.reject(status);
214                 });
215
216             return deferred.promise;
217         }
218         
219   
220         
221     }
222     UserProfileService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4', 'utilsService'];
223     angular.module('ecompApp').service('userProfileService', UserProfileService)
224 })();