[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-FE-common / client / app / services / catalog / catalog.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 CatalogService {
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.debug = false;
32             this.utilsService = utilsService;
33         }
34
35         getAppCatalog() {
36             let deferred = this.$q.defer();
37             this.$http({
38                         method: "GET",
39                         url: this.conf.api.appCatalog,
40                     cache: false,
41                     headers: {
42                         'X-ECOMP-RequestID':this.uuid.generate()
43                     }
44                 })
45                 .then( res => {
46                         if (this.debug)
47                                 this.$log.debug('CatalogService::getAppCatalog: result is ' + JSON.stringify(res));
48                     // Res is always JSON, but the data object might be an HTML error page.
49                     if (! this.utilsService.isValidJSON(res.data)) {
50                         var msg = 'CatalogService::getAppCatalog: result data is not JSON';
51                         if (this.debug)
52                                 this.$log.debug(msg);
53                         deferred.reject(msg);
54                     } else {
55                         if (this.debug)
56                                 this.$log.debug('CatalogService::getAppCatalog: success');
57                         deferred.resolve(res.data);
58                     }
59                 })
60                 .catch( status => {
61                         this.$log.error('CatalogService:getAppCatalog failed: ' + status);
62                     deferred.reject(status);
63                 });
64             return deferred.promise;
65         }
66         
67         // Expects an object with fields matching model class AppCatalogSelection:
68         // appId (number), select (boolean), pending (boolean).
69         updateAppCatalog(appData) {
70             let deferred = this.$q.defer();
71             // Validate the request, maybe this is overkill
72             if (appData == null || appData.appId == null || appData.select == null) {
73                 var msg = 'CatalogService::updateAppCatalog: field appId and/or select not found';
74                 this.$log.error(msg);
75                 return deferred.reject(msg);
76             }
77             this.$http({
78                 method: "PUT",
79                 url: this.conf.api.appCatalog,
80                 data: appData,
81                 headers: {
82                     'X-ECOMP-RequestID':this.uuid.generate()
83                 }
84             }).then( res => {
85                     // Detect missing result
86                     if (res == null || res.data == null) {
87                         deferred.reject("CatalogService::updateAppCatalog Failed");
88                     } else {
89                         deferred.resolve(res.data);
90                     }
91                 })
92                 .catch( status => {
93                         this.$log.error('CatalogService:updateAppCatalog failed: ' + status);
94                     deferred.reject(status);
95                 });
96             return deferred.promise;
97         }
98         
99         // Expects an object with fields and used to update records for ep_pers_user_app_man_sort table:
100         // appId (number), select (boolean).
101         updateManualAppSort(appData) {
102             let deferred = this.$q.defer();
103             
104             // Validate the request, maybe this is overkill
105             if (appData == null || appData.appId == null || appData.select == null) {
106                 var msg = 'CatalogService::updateManualAppSort: field appId and/or select not found';
107                 this.$log.error(msg);
108                 return deferred.reject(msg);
109             }
110             this.$http({
111                 method: "PUT",
112                 url: this.conf.api.UpdateUserAppsSortManual,
113                 data: appData,
114                 headers: {
115                     'X-ECOMP-RequestID':this.uuid.generate()
116                 }
117             }).then( res => {
118                     // Detect missing result
119                     if (res == null || res.data == null) {
120                         deferred.reject("CatalogService::updateManualAppSort Failed");
121                     } else {
122                         deferred.resolve(res.data);
123                     }
124                 })
125                 .catch( status => {
126                         this.$log.error('CatalogService:updateManualAppSort failed: ' + status);
127                     deferred.reject(status);
128                 });
129             
130             return deferred.promise;
131         }
132            
133         getuserAppRolesCatalog(item) {
134             let deferred = this.$q.defer();
135             this.$http({
136                         method: "GET",
137                         url: this.conf.api.appCatalogRoles,
138                         params:{appName:item},
139                     cache: false,
140                     headers: {
141                         'X-ECOMP-RequestID':this.uuid.generate()
142                     }
143                 })
144                 .then( res => {
145                         if (this.debug)
146                                 this.$log.debug('CatalogService::getAppCatalog: result is ' + JSON.stringify(res));
147                     // Res is always JSON, but the data object might be an HTML error page.
148                     if (! this.utilsService.isValidJSON(res.data)) {
149                         var msg = 'CatalogService::getAppCatalog: result data is not JSON';
150                         if (this.debug)
151                                 this.$log.debug(msg);
152                         deferred.reject(msg);
153                     } else {
154                         if (this.debug)
155                                 this.$log.debug('CatalogService::getAppCatalog: success');
156                         deferred.resolve(res.data);
157                     }
158                 })
159                 .catch( status => {
160                         this.$log.error('CatalogService:getAppCatalog failed: ' + status);
161                     deferred.reject(status);
162                 });
163             return deferred.promise;
164         }
165         
166         getAppsFullList() {
167             let deferred = this.$q.defer();
168             this.$http({
169                         method: "GET",
170                         url: this.conf.api.appsFullList,
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('CatalogService::getAppsFullList: 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 = 'CatalogService::getAppsFullList: result data is not JSON';
182                         if (this.debug)
183                                 this.$log.error(msg);
184                         deferred.reject(msg);
185                     } else {
186                         if (this.debug)
187                                 this.$log.debug('CatalogService::getAppsFullList: success');
188                         deferred.resolve(res.data);
189                     }
190                 })
191                 .catch( status => {
192                         this.$log.error('CatalogService:getAppsFullList failed: ' + status);
193                     deferred.reject(status);
194                 });
195             return deferred.promise;
196         }
197
198     }
199     
200     CatalogService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
201     angular.module('ecompApp').service('catalogService', CatalogService)
202 })();