nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / services / applications / applications.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 ApplicationsService {
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         getPersUserApps() {
34             let deferred = this.$q.defer();
35             // this.$log.info('ApplicationsService::getPersUserApps');
36             this.$http.get(this.conf.api.persUserApps,
37                 {
38                     cache: false,
39                     headers: {
40                         'X-ECOMP-RequestID':this.uuid.generate()
41                     }
42                 })
43                 .then( res => {
44                     // If response comes back as a redirected HTML page which IS NOT a success
45                         // But don't declare an empty list to be an error.
46                     if (res == null || res.data == null) {
47                         deferred.reject("ApplicationsService::getPersUserApps Failed");
48                     } else {
49                         deferred.resolve(res.data);
50                     }
51                 })
52                 .catch( status => {
53                     deferred.reject(status);
54                 });
55             return deferred.promise;
56         }
57
58         getUserApps(){
59             let deferred = this.$q.defer();
60             this.$log.info('ApplicationsService::getUserApps');
61             this.$http.get(this.conf.api.userApps,
62                 {
63                     cache: false,
64                     headers: {
65                         'X-ECOMP-RequestID':this.uuid.generate()
66                     }
67                 })
68                 .then( res => {
69                     if (Object.keys(res.data).length == 0) {
70                         deferred.reject("ApplicationsService::getUserApps Failed");
71                     } else {
72                         this.$log.info('ApplicationsService::getUserApps Succeeded');
73                         deferred.resolve(res.data);
74                     }
75                 })
76                 .catch( status => {
77                     deferred.reject(status);
78                 });
79
80             return deferred.promise;
81         }
82
83         getAvailableApps() {
84             let deferred = this.$q.defer();
85             this.$log.info('ApplicationsService::getAvailableApps');
86             this.$http(
87                 {
88                     method: "GET",
89                     url: this.conf.api.availableApps,
90                     cache: false,
91                     headers: {
92                         'X-ECOMP-RequestID':this.uuid.generate()
93                     }
94                 })
95                 .then( res => {
96                     if (Object.keys(res.data).length == 0) {
97                         deferred.reject("ApplicationsService::getAvailableApps Failed");
98                     } else {
99                         this.$log.info('ApplicationsService::getAvailableApps Succeeded');
100                         deferred.resolve(res.data);
101                     }
102                 })
103                 .catch( status => {
104                     deferred.reject(status);
105                 });
106
107             return deferred.promise;
108         }
109
110         getAdminApps(){
111             let canceller = this.$q.defer();
112             let isActive = false;
113
114             let cancel = () => {
115                 if(isActive){
116                     this.$log.debug('ApplicationsService::getAdminApps: canceling the request');
117                     canceller.resolve();
118                 }
119             };
120
121             let promise = () => {
122                 isActive = true;
123                 let deferred = this.$q.defer();
124                 this.$log.info('ApplicationsService::getAdminApps: starting');
125                 this.$http({method: "GET",
126                         url: this.conf.api.adminApps,
127                         cache: false,
128                         timeout: canceller.promise,
129                         headers: {
130                             'X-ECOMP-RequestID':this.uuid.generate()
131                         }
132                     }).then(res => {
133                         isActive = false;
134                         if (Object.keys(res.data).length == 0) {
135                             deferred.reject("ApplicationsService::adminApps Failed");
136                         } else {
137                             this.$log.info('ApplicationsService::adminApps Succeeded');
138                             deferred.resolve(res.data);
139                         }
140                     })
141                     .catch(status => {
142                         isActive = false;
143                         deferred.reject(status);
144                     });
145                 return deferred.promise;
146             };
147
148             return {
149                 cancel: cancel,
150                 promise: promise
151             };
152         }
153
154         getAppsForSuperAdminAndAccountAdmin(){
155             let deferred = this.$q.defer();
156             this.$log.info('ApplicationsService::getAppsForSuperAdminAndAccountAdmin');
157             this.$http({method: "GET",
158                 url: this.conf.api.appsForSuperAdminAndAccountAdmin,
159                 cache: false,
160                 headers: {
161                     'X-ECOMP-RequestID':this.uuid.generate()
162                 }
163             }).then(res => {
164                 if (Object.keys(res.data).length == 0) {
165                     deferred.reject("ApplicationsService::getAppsForSuperAdminAndAccountAdmin Failed");
166                 } else {
167                     this.$log.info('ApplicationsService::getAppsForSuperAdminAndAccountAdmin Succeeded');
168                     deferred.resolve(res.data);
169                 }
170             })
171             .catch(status => {
172                 deferred.reject(status);
173             });
174             return deferred.promise;
175         }
176
177         getAdminAppsSimpler(){
178                 let deferred = this.$q.defer();
179                 this.$log.info('ApplicationsService::getAdminApps');
180                 this.$http({method: "GET",
181                     url: this.conf.api.adminApps,
182                     cache: false,
183                     headers: {
184                         'X-ECOMP-RequestID':this.uuid.generate()
185                     }
186                 }).then(res => {
187                     if (Object.keys(res.data).length == 0) {
188                         deferred.reject("ApplicationsService::getAdminApps Failed");
189                     } else {
190                         this.$log.info('ApplicationsService::getAdminApps Succeeded');
191                         deferred.resolve(res.data);
192                     }
193                 })
194                 .catch(status => {
195                     deferred.reject(status);
196                 });
197                 return deferred.promise;
198         }
199
200         getOnboardingApps(){
201             let deferred = this.$q.defer();
202             this.$log.info('ApplicationsService::getOnboardingApps');
203
204             this.$http.get(this.conf.api.onboardingApps,
205                 {
206                     cache: false,
207                     headers: {
208                         'X-ECOMP-RequestID':this.uuid.generate()
209                     }
210                 })
211                 .then( res => {
212                     if (Object.keys(res.data).length == 0) {
213                         deferred.reject("ApplicationsService::getOnboardingApps Failed");
214                     } else {
215                         this.$log.info('ApplicationsService::getOnboardingApps Succeeded');
216                         deferred.resolve(res.data);
217                     }
218                 })
219                 .catch( status => {
220                     deferred.reject(status);
221                 });
222
223             return deferred.promise;
224         }
225
226         addOnboardingApp(newApp){
227             let deferred = this.$q.defer();
228             this.$log.info('applications-service::addOnboardingApp');
229             this.$log.debug('applications-service::addOnboardingApp with:', newApp);
230
231             this.$http({
232                 method: "POST",
233                 url: this.conf.api.onboardingApps,
234                 data: newApp,
235                 cache: false,
236                 headers: {
237                     'X-ECOMP-RequestID':this.uuid.generate()
238                 }
239             }).then( res => {
240                 if (Object.keys(res.data).length == 0) {
241                     deferred.reject("ApplicationsService::addOnboardingApp Failed");
242                 } else {
243                     this.$log.info('ApplicationsService::addOnboardingApp Succeeded');
244                     deferred.resolve(res.data);
245                 }
246             })
247             .catch( status => {
248                 deferred.reject(status);
249             });
250             return deferred.promise;
251         }
252
253         updateOnboardingApp(appData){
254             let deferred = this.$q.defer();
255             this.$log.info('ApplicationsService::addOnboardingApp');
256             if(!appData.id){
257                 this.$log.error('ApplicationsService::addOnboardingApp: App id not found!');
258                 return deferred.reject('App id not found');
259             }
260
261             this.$http({
262                 method: "PUT",
263                 url: this.conf.api.onboardingApps,
264                 data: appData,
265                 cache: false,
266                 headers: {
267                     'X-ECOMP-RequestID':this.uuid.generate()
268                 }
269             }).then( res => {
270                 if (Object.keys(res.data).length == 0) {
271                     deferred.reject("ApplicationsService::updateOnboardingApp Failed");
272                 } else {
273                     this.$log.info('ApplicationsService::updateOnboardingApp Succeeded');
274                     deferred.resolve(res.data);
275                 }
276             })
277             .catch( status => {
278                 deferred.reject(status);
279             });
280             return deferred.promise;
281         }
282
283         deleteOnboardingApp(appId) {
284             let deferred = this.$q.defer();
285             let url = this.conf.api.onboardingApps + '/' + appId;
286
287             this.$log.info('applications.service::deleteOnboardingApp' +appId);
288
289             this.$http({
290                 method: "DELETE",
291                 url: url,
292                 cache: false,
293                 data:'',
294                 headers: {
295                     'X-ECOMP-RequestID':this.uuid.generate()
296                 }
297             }).then(res => {
298                 if (Object.keys(res.data).length == 0) {
299                     deferred.reject("applications.service::deleteOnboardingApp Failed");
300                 } else {
301                     this.$log.info('applications.service::deleteOnboardingApp succeeded: ');
302                     deferred.resolve(res.data);
303                 }
304             })
305             .catch(errRes => {
306                 deferred.reject(errRes);
307             });
308             return deferred.promise;
309         }
310
311         getTopMenuData(selectedApp) {
312             let deferred = this.$q.defer();
313             this.$log.info('ApplicationsService:getTopMenuData');
314             this.$log.debug('ApplicationsService:getTopMenuData with:', selectedApp);
315
316         }
317
318         ping(){
319             let deferred = this.$q.defer();
320             this.$log.info('ApplicationsService::ping: ');
321
322             this.$http.get(this.conf.api.ping,
323                 {
324                     cache: false,
325                     headers: {
326                         'X-ECOMP-RequestID':this.uuid.generate()
327                     }
328                 })
329                 .success( res => {
330                     if (Object.keys(res).length == 0) {
331                         deferred.reject("ApplicationsService::ping: Failed");
332                     } else {
333                         this.$log.info('ApplicationsService::ping: Succeeded');
334                         deferred.resolve(res);
335                     }
336                 })
337                 .error( status => {
338                     deferred.reject(status);
339                 });
340
341             return deferred.promise;
342         }
343     }
344     ApplicationsService.$inject = ['$q', '$log', '$http', 'conf','uuid4'];
345     angular.module('ecompApp').service('applicationsService', ApplicationsService)
346 })();