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