[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / 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 'use strict';
21
22 (function () {
23     class ApplicationsService {
24         constructor($q, $log, $http, conf, uuid, utilsService) {
25             this.$q = $q;
26             this.$log = $log;
27             this.$http = $http;
28             this.conf = conf;
29             this.uuid = uuid;
30             this.utilsService = utilsService
31         }
32         
33         getPersUserApps() {
34             let deferred = this.$q.defer();
35             var _this0 = this;
36             // this.$log.info('ApplicationsService::getPersUserApps');
37             this.$http.get(this.conf.api.persUserApps,
38                 {
39                     cache: false,
40                     headers: {
41                         'X-ECOMP-RequestID':this.uuid.generate()
42                     }
43                 })
44                 .then( res => {
45                     // If response comes back as a redirected HTML page which IS NOT a success
46                         // But don't declare an empty list to be an error.
47                     if (res == null || res.data == null || _this0.utilsService.isValidJSON(res.data) == false) {
48                         deferred.reject("ApplicationsService::getPersUserApps Failed");
49                     } else {
50                         deferred.resolve(res.data);
51                     }
52                 })
53                 .catch( status => {
54                     deferred.reject(status);
55                 });
56             return deferred.promise;
57         }
58         
59         getAppsOrderBySortPref(userAppSortTypePref) {
60             let deferred = this.$q.defer();
61             var _this1 = this;
62             // this.$log.info('ApplicationsService::getAppsOrderBySortPref');
63             this.$http.get(this.conf.api.userAppsOrderBySortPref,
64                 {
65                     cache: false,
66                     params:{'mparams':userAppSortTypePref},
67                     headers: {
68                         'X-ECOMP-RequestID':this.uuid.generate()
69                     }
70                 })
71                 .then( res => {
72                     // If response comes back as a redirected HTML page which IS NOT a success
73                         // But don't declare an empty list to be an error.
74                     if (res == null || res.data == null || _this1.utilsService.isValidJSON(res.data)== false) {
75                         deferred.reject("ApplicationsService::getAppsOrderBySortPref Failed");
76                     } else {
77                         // this.$log.info('ApplicationsService::getAppsOrderBySortPref Succeeded');
78                         deferred.resolve(res.data);
79                     }
80                 })
81                 .catch( status => {
82                     deferred.reject(status);
83                 });
84
85             return deferred.promise;
86         }
87
88         saveAppsSortTypeManual(appsSortManual){
89             let deferred = this.$q.defer();
90             if (appsSortManual== undefined
91                                         || appsSortManual == null
92                                         || appsSortManual.length == 0) {
93                 this.$log.error('ApplicationsService::saveAppsSortTypeManual: Apps Sort Manual received empty string!'); 
94                 return deferred.reject('Apps Sort Manual received empty string ');
95             }
96             
97             this.$http({
98                 method: 'PUT',
99                 url: this.conf.api.saveUserAppsSortingManual,
100                 data: appsSortManual,
101                 cache: false,
102                 headers: {
103                     'X-ECOMP-RequestID':this.uuid.generate()
104                 }
105             }).then( res => {
106                     // If response comes back as a redirected HTML page which IS
107                                         // NOT a success
108                         // But don't declare an empty list to be an error.
109                     if (res == null || res.data == null) {
110                         deferred.reject("ApplicationsService::saveAppsSortTypeManual Failed");
111                     } else {
112                         // this.$log.info('ApplicationsService::saveAppsSortTypeManual
113                                                 // Succeeded');
114                         deferred.resolve(res.data);
115                     }
116                 })
117                 .catch( status => {
118                     deferred.reject(status);
119                 });
120             return deferred.promise;
121         }
122             
123         saveAppsSortTypePreference(appsSortPreference){
124             let deferred = this.$q.defer();
125             var _this2 = this;
126             if (appsSortPreference== undefined
127                                         || appsSortPreference == null
128                                         || appsSortPreference.length == 0){
129                 this.$log.error('ApplicationsService::saveAppsSortTypePreference: Apps Sort type Preference received empty string!'); 
130                          return deferred.reject('Apps Sort type Preference received empty string ');
131             }
132             this.$http({
133                 method: 'PUT',
134                 url: this.conf.api.saveUserAppsSortingPreference,
135                 data: appsSortPreference,
136                 cache: false,
137                 headers: {
138                     'X-ECOMP-RequestID':this.uuid.generate()
139                 }
140             }).then( res => {
141                     // If response comes back as a redirected HTML page which IS NOT a success
142                         // But don't declare an empty list to be an error.
143                     if (res == null || res.data == null || _this2.utilsService.isValidJSON(res) == false) {
144                         deferred.reject("ApplicationsService::saveAppsSortTypePreference Failed");
145                     } else {
146                         // this.$log.info('ApplicationsService::saveAppsSortTypePreference Succeeded');
147                         deferred.resolve(res.data);
148                     }
149                 })
150                 .catch( status => {
151                     deferred.reject(status);
152                 });
153             return deferred.promise;
154         }
155         
156         getUserAppsSortTypePreference() {
157                 let deferred = this.$q.defer();
158                 var _this3 = this;
159                 this.$http({
160                         method: "GET",
161                         url: this.conf.api.userAppsSortTypePreference,
162                         cache: false,
163                         headers: {
164                             'X-ECOMP-RequestID':this.uuid.generate()
165                         }
166                     })
167                     .then( res => {
168                         if (res == null || res.data == null || _this3.utilsService.isValidJSON(res) == false) {  
169                                 deferred.reject("ApplicationsService::getSortTypePreference Failed");
170                         } else {
171                             // this.$log.info('ApplicationsService::getUserAppsSortTypePreference Succeeded');
172                             deferred.resolve(res.data);
173                         }
174                     })
175                     .catch( status => {
176                         deferred.reject(status);
177                     });
178                         return deferred.promise;
179         }
180         
181         saveWidgetsSortManual(widgetsSortManual){
182             let deferred = this.$q.defer();
183             var _this4 = this;
184             if (widgetsSortManual== undefined
185                                         || widgetsSortManual == null
186                                         || widgetsSortManual.length == 0){
187                 this.$log.error('ApplicationsService::saveWidgetsSortManual: Widegts Sort type Preference received empty string!'); 
188                          return deferred.reject('Widgets Sort Manual received empty string ');
189             }
190             this.$http({
191                 method: 'PUT',
192                 url: this.conf.api.saveUserWidgetsSortManual,
193                 data: widgetsSortManual,
194                 cache: false,
195                 headers: {
196                     'X-ECOMP-RequestID':this.uuid.generate()
197                 }
198             }).then( res => {
199                     if (res == null || res.data == null || _this4.utilsService.isValidJSON(res) == false) {
200                         deferred.reject("ApplicationsService::saveWidgetsSortManual Failed");
201                     } else {
202                         // this.$log.info('ApplicationsService::saveWidgetsSortManual
203                                                 // Succeeded');
204                         deferred.resolve(res.data);
205                     }
206                 })
207                 .catch( status => {
208                     deferred.reject(status);
209                 });
210             return deferred.promise;
211         }
212         
213         delWidgetsSortPref(widgetsData){
214             let deferred = this.$q.defer();
215             var _this5 = this;
216              
217             if (widgetsData== undefined
218                                         || widgetsData == null
219                                         || widgetsData.length == 0){
220                 this.$log.error('ApplicationsService::delWidgetsSortPref: While deleting, widgets  received empty string!'); 
221                 return deferred.reject('Widgets received empty string ');
222             }
223             
224             this.$http({
225                 method: 'PUT',
226                 url: this.conf.api.updateWidgetsSortPref,
227                 data: widgetsData,
228                 cache: false,
229                 headers: {
230                     'X-ECOMP-RequestID':this.uuid.generate()
231                 }
232             }).then( res => {
233                     if (res == null || res.data == null || _this5.utilsService.isValidJSON(res) == false) {
234                         deferred.reject("ApplicationsService::delWidgetsSortPref Failed");
235                     } else {
236                         // this.$log.info('ApplicationsService::delWidgetsSortPref
237                                                 // Succeeded');
238                         deferred.resolve(res.data);
239                     }
240                 })
241                 .catch( status => {
242                     deferred.reject(status);
243                 });
244             return deferred.promise;
245         }
246         
247         getAvailableApps() {
248             let deferred = this.$q.defer();
249             var _this6 = this;
250             // this.$log.info('ApplicationsService::getAvailableApps');
251             
252             this.$http(
253                 {
254                     method: "GET",
255                     url: this.conf.api.availableApps,
256                     cache: false,
257                     headers: {
258                         'X-ECOMP-RequestID':this.uuid.generate()
259                     }
260                 })
261                 .then( res => {
262                     if (res == null || res.data == null || _this6.utilsService.isValidJSON(res) == false) {
263                         deferred.reject("ApplicationsService::getAvailableApps Failed");
264                     } else {
265                         // this.$log.info('ApplicationsService::getAvailableApps Succeeded');
266                         deferred.resolve(res.data);
267                     }
268                 })
269                 .catch( status => {
270                     deferred.reject(status);
271                 });
272
273             return deferred.promise;
274         }
275
276         getAdminApps(){
277             var _this7 = this;
278             let canceller = this.$q.defer();
279             let isActive = false;
280
281             let cancel = () => {
282                 if(isActive){
283                     this.$log.debug('ApplicationsService::getAdminApps: canceling the request');
284                     canceller.resolve();
285                 }
286             };
287
288             let promise = () => {
289                 isActive = true;
290                 let deferred = this.$q.defer();
291                 // this.$log.info('ApplicationsService::getAdminApps: starting');
292                 this.$http({method: "GET",
293                         url: this.conf.api.adminApps,
294                         cache: false,
295                         timeout: canceller.promise,
296                         headers: {
297                             'X-ECOMP-RequestID':this.uuid.generate()
298                         }
299                     }).then(res => {
300                         isActive = false;
301                         if (res == null || res.data == null || _this7.utilsService.isValidJSON(res) == false) {
302                             deferred.reject("ApplicationsService::adminApps Failed");
303                         } else {
304                             // this.$log.info('ApplicationsService::adminApps Succeeded');
305                             deferred.resolve(res.data);
306                         }
307                     })
308                     .catch(status => {
309                         isActive = false;
310                         deferred.reject(status);
311                     });
312                 return deferred.promise;
313             };
314
315             return {
316                 cancel: cancel,
317                 promise: promise
318             };
319         }
320
321         getLeftMenuItems(){
322             let deferred = this.$q.defer();
323             var _this8 = this;
324             // this.$log.info('ApplicationsService::getAppsForSuperAdminAndAccountAdmin');
325             this.$http({method: "GET",
326                 url: this.conf.api.leftmenuItems,
327                 cache: false,
328                 headers: {
329                     'X-ECOMP-RequestID':this.uuid.generate()
330                 }
331             }).then(res => {
332                     // If response comes back as a redirected HTML page which IS NOT a success
333                         // But don't declare an empty list to be an error.
334                 this.$log.debug('getLeftMenuItems::getAdminApps: canceling the request',res);
335
336                     if (res == null || res.data == null || _this8.utilsService.isValidJSON(res) == false) {
337                         deferred.reject("ApplicationsService::getLeftMenuItems Failed");
338                     } else {
339                         // this.$log.info('ApplicationsService::getAppsForSuperAdminAndAccountAdmin Succeeded');
340                         deferred.resolve(res.data);
341                     }
342                 })
343                 .catch(status => {
344                     deferred.reject(status);
345                 });
346             return deferred.promise;
347         }
348         
349         getAppsForSuperAdminAndAccountAdmin(){
350             let deferred = this.$q.defer();
351             var _this9 = this;
352             // this.$log.info('ApplicationsService::getAppsForSuperAdminAndAccountAdmin');
353             this.$http({method: "GET",
354                 url: this.conf.api.appsForSuperAdminAndAccountAdmin,
355                 cache: false,
356                 headers: {
357                     'X-ECOMP-RequestID':this.uuid.generate()
358                 }
359             }).then(res => {
360                     // If response comes back as a redirected HTML page which IS NOT a success
361                         // But don't declare an empty list to be an error.
362                     if (res == null || res.data == null || _this9.utilsService.isValidJSON(res) == false) {
363                         deferred.reject("ApplicationsService::getAppsForSuperAdminAndAccountAdmin Failed");
364                     } else {
365                         // this.$log.info('ApplicationsService::getAppsForSuperAdminAndAccountAdmin Succeeded');
366                         deferred.resolve(res.data);
367                     }
368                 })
369                 .catch(status => {
370                     deferred.reject(status);
371                 });
372             return deferred.promise;
373         }
374
375         getAdminAppsSimpler(){
376                 let deferred = this.$q.defer();
377                 var _this10 = this;
378             // this.$log.info('ApplicationsService::getAdminAppsSimpler');
379                 this.$http({method: "GET",
380                         url: this.conf.api.adminApps,
381                         cache: false,
382                         headers: {
383                                 'X-ECOMP-RequestID':this.uuid.generate()
384                         }
385                 }).then(res => {
386                         // If response comes back as a redirected HTML page which IS NOT a success
387                         // But don't declare an empty list to be an error.
388                         if (res == null || res.data == null || _this10.utilsService.isValidJSON(res.data) == false) {
389                                 deferred.reject("ApplicationsService::getAdminAppsSimpler Failed");
390                         } else {
391                                 // this.$log.info('ApplicationsService::getAdminAppsSimpler Succeeded');
392                                 deferred.resolve(res.data);
393                         }
394                 })
395                 .catch(status => {
396                         deferred.reject(status);
397                 });
398                 return deferred.promise;
399         }
400
401         getOnboardingApps(){
402             let deferred = this.$q.defer();
403             var _this11 = this;
404             // this.$log.debug('applications-service::getOnboardingApps');
405             this.$http.get(this.conf.api.onboardingApps,
406                 {
407                     cache: false,
408                     headers: {
409                         'X-ECOMP-RequestID':this.uuid.generate()
410                     }
411                 })
412                 .then( res => {
413                     // If response comes back as a redirected HTML page which IS NOT a success
414                         // But don't declare an empty list to be an error.
415                     if (res == null || res.data == null || _this11.utilsService.isValidJSON(res.data) == false) {
416                         deferred.reject("ApplicationsService::getOnboardingApps Failed");
417                     } else {
418                         // this.$log.info('ApplicationsService::getOnboardingApps Succeeded');
419                         deferred.resolve(res.data);
420                     }
421                 })
422                 .catch( status => {
423                     deferred.reject(status);
424                 });
425
426             return deferred.promise;
427         }
428
429         addOnboardingApp(newApp){
430             let deferred = this.$q.defer();
431             // this.$log.debug('applications-service::addOnboardingApp with:', newApp);
432             this.$http({
433                 method: "POST",
434                 url: this.conf.api.onboardingApps,
435                 data: newApp,
436                 cache: false,
437                 headers: {
438                     'X-ECOMP-RequestID':this.uuid.generate()
439                 }
440             }).then( res => {
441                     // If response comes back as a redirected HTML page which IS NOT a success
442                         // But don't declare an empty list to be an error.
443                     if (res == null || res.data == null) {
444                         deferred.reject("ApplicationsService::addOnboardingApp Failed");
445                     } else {
446                         // this.$log.info('ApplicationsService::addOnboardingApp Succeeded');
447                         deferred.resolve(res.data);
448                     }
449                 })
450                 .catch( status => {
451                     deferred.reject(status);
452                 });
453             return deferred.promise;
454         }
455
456         updateOnboardingApp(appData){
457             let deferred = this.$q.defer();
458             // this.$log.info('ApplicationsService::addOnboardingApp');
459             if(!appData.id){
460                 this.$log.error('ApplicationsService::addOnboardingApp: App id not found!');
461                 return deferred.reject('App id not found');
462             }
463
464             this.$http({
465                 method: "PUT",
466                 url: this.conf.api.onboardingApps,
467                 data: appData,
468                 cache: false,
469                 headers: {
470                     'X-ECOMP-RequestID':this.uuid.generate()
471                 }
472             }).then( res => {
473                     // If response comes back as a redirected HTML page which IS NOT a success
474                         // But don't declare an empty list to be an error.
475                     if (res == null || res.data == null) {
476                         deferred.reject("ApplicationsService::updateOnboardingApp Failed");
477                     } else {
478                         // this.$log.info('ApplicationsService::updateOnboardingApp Succeeded');
479                         deferred.resolve(res.data);
480                     }
481                 })
482                 .catch( status => {
483                     deferred.reject(status);
484                 });
485             return deferred.promise;
486         }
487         
488         saveUserAppsRoles(UserAppRolesRequest) {
489                  let deferred = this.$q.defer();
490                  
491                 if (UserAppRolesRequest== undefined
492                                 || UserAppRolesRequest == null
493                                 || UserAppRolesRequest.length == 0){
494                 this.$log.error('ApplicationsService::saveAppsSortTypeManual: Apps Sort Manual received empty string!'); 
495                          return deferred.reject('Apps Sort Manual received empty string ');
496                         }
497                         // var manualAppsJson = angular.toJson(appsSortManual);
498       
499                     // console.log(manual_jsonData);
500                     this.$http({
501                         method: 'PUT',
502                         url: this.conf.api.saveUserAppRoles,
503                         data: UserAppRolesRequest,
504                         cache: false,
505                         headers: {
506                             'X-ECOMP-RequestID':this.uuid.generate()
507                         }
508                     }).then( res => {
509                             // If response comes back as a redirected HTML page which IS
510                                         // NOT a success
511                                 // But don't declare an empty list to be an error.
512                             if (res == null || res.data == null) {
513                                 deferred.reject("ApplicationsService::saveAppsSortTypeManual Failed");
514                             } else {
515                                 // this.$log.info('ApplicationsService::saveAppsSortTypeManual
516                                                 // Succeeded');
517                                 deferred.resolve(res.data);
518                             }
519                         })
520                         .catch( status => {
521                             deferred.reject(status);
522                         });
523                     return deferred.promise;
524
525         }
526          
527
528         deleteOnboardingApp(appId) {
529             let deferred = this.$q.defer();
530             let url = this.conf.api.onboardingApps + '/' + appId;
531             // this.$log.info('applications.service::deleteOnboardingApp' +appId);
532
533             this.$http({
534                 method: "DELETE",
535                 url: url,
536                 cache: false,
537                 data:'',
538                 headers: {
539                     'X-ECOMP-RequestID':this.uuid.generate()
540                 }
541             }).then(res => {
542                     // If response comes back as a redirected HTML page which IS NOT a success
543                         // But don't declare an empty list to be an error.
544                     if (res == null || res.data == null) {
545                         deferred.reject("applications.service::deleteOnboardingApp Failed");
546                     } else {
547                         // this.$log.info('applications.service::deleteOnboardingApp succeeded: ');
548                         deferred.resolve(res.data);
549                     }
550                 })
551                 .catch(errRes => {
552                     deferred.reject(errRes);
553                 });
554             return deferred.promise;
555         }
556
557         getTopMenuData(selectedApp) {
558             let deferred = this.$q.defer();
559             // this.$log.info('ApplicationsService:getTopMenuData');
560             this.$log.debug('ApplicationsService:getTopMenuData with:', selectedApp);
561         }
562
563         ping(){
564             let deferred = this.$q.defer();
565             // this.$log.info('ApplicationsService::ping: ');
566             this.$http.get(this.conf.api.ping,
567                 {
568                     cache: false,
569                     headers: {
570                         'X-ECOMP-RequestID':this.uuid.generate()
571                     }
572                 })
573                 .success( res => {
574                     // If response comes back as a redirected HTML page which IS NOT a success
575                     if (Object.keys(res).length == 0) {
576                         deferred.reject("ApplicationsService::ping: Failed");
577                     } else {
578                         // this.$log.info('ApplicationsService::ping: Succeeded');
579                         deferred.resolve(res);
580                     }
581                 })
582                 .error( status => {
583                     deferred.reject(status);
584                 });
585
586             return deferred.promise;
587         }
588     }
589     ApplicationsService.$inject = ['$q', '$log', '$http', 'conf','uuid4','utilsService'];
590     angular.module('ecompApp').service('applicationsService', ApplicationsService)
591 })();