[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / services / catalog / catalog.service.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 'use strict';\r
21 \r
22 (function () {\r
23     class CatalogService {\r
24         \r
25         constructor($q, $log, $http, conf, uuid, utilsService) {\r
26             this.$q = $q;\r
27             this.$log = $log;\r
28             this.$http = $http;\r
29             this.conf = conf;\r
30             this.uuid = uuid;\r
31             this.debug = false;\r
32             this.utilsService = utilsService;\r
33         }\r
34 \r
35         getAppCatalog() {\r
36             let deferred = this.$q.defer();\r
37             this.$http({\r
38                         method: "GET",\r
39                         url: this.conf.api.appCatalog,\r
40                     cache: false,\r
41                     headers: {\r
42                         'X-ECOMP-RequestID':this.uuid.generate()\r
43                     }\r
44                 })\r
45                 .then( res => {\r
46                         if (this.debug)\r
47                                 this.$log.debug('CatalogService::getAppCatalog: result is ' + JSON.stringify(res));\r
48                     // Res is always JSON, but the data object might be an HTML error page.\r
49                     if (! this.utilsService.isValidJSON(res.data)) {\r
50                         var msg = 'CatalogService::getAppCatalog: result data is not JSON';\r
51                         if (this.debug)\r
52                                 this.$log.debug(msg);\r
53                         deferred.reject(msg);\r
54                     } else {\r
55                         if (this.debug)\r
56                                 this.$log.debug('CatalogService::getAppCatalog: success');\r
57                         deferred.resolve(res.data);\r
58                     }\r
59                 })\r
60                 .catch( status => {\r
61                         this.$log.error('CatalogService:getAppCatalog failed: ' + status);\r
62                     deferred.reject(status);\r
63                 });\r
64             return deferred.promise;\r
65         }\r
66         \r
67         // Expects an object with fields matching model class AppCatalogSelection:\r
68         // appId (number), select (boolean), pending (boolean).\r
69         updateAppCatalog(appData) {\r
70             let deferred = this.$q.defer();\r
71             // Validate the request, maybe this is overkill\r
72             if (appData == null || appData.appId == null || appData.select == null) {\r
73                 var msg = 'CatalogService::updateAppCatalog: field appId and/or select not found';\r
74                 this.$log.error(msg);\r
75                 return deferred.reject(msg);\r
76             }\r
77             this.$http({\r
78                 method: "PUT",\r
79                 url: this.conf.api.appCatalog,\r
80                 data: appData,\r
81                 headers: {\r
82                     'X-ECOMP-RequestID':this.uuid.generate()\r
83                 }\r
84             }).then( res => {\r
85                     // Detect missing result\r
86                     if (res == null || res.data == null) {\r
87                         deferred.reject("CatalogService::updateAppCatalog Failed");\r
88                     } else {\r
89                         deferred.resolve(res.data);\r
90                     }\r
91                 })\r
92                 .catch( status => {\r
93                         this.$log.error('CatalogService:updateAppCatalog failed: ' + status);\r
94                     deferred.reject(status);\r
95                 });\r
96             return deferred.promise;\r
97         }\r
98         \r
99         // Expects an object with fields and used to update records for ep_pers_user_app_man_sort table:\r
100         // appId (number), select (boolean).\r
101         updateManualAppSort(appData) {\r
102             let deferred = this.$q.defer();\r
103             \r
104             // Validate the request, maybe this is overkill\r
105             if (appData == null || appData.appId == null || appData.select == null) {\r
106                 var msg = 'CatalogService::updateManualAppSort: field appId and/or select not found';\r
107                 this.$log.error(msg);\r
108                 return deferred.reject(msg);\r
109             }\r
110             this.$http({\r
111                 method: "PUT",\r
112                 url: this.conf.api.UpdateUserAppsSortManual,\r
113                 data: appData,\r
114                 headers: {\r
115                     'X-ECOMP-RequestID':this.uuid.generate()\r
116                 }\r
117             }).then( res => {\r
118                     // Detect missing result\r
119                     if (res == null || res.data == null) {\r
120                         deferred.reject("CatalogService::updateManualAppSort Failed");\r
121                     } else {\r
122                         deferred.resolve(res.data);\r
123                     }\r
124                 })\r
125                 .catch( status => {\r
126                         this.$log.error('CatalogService:updateManualAppSort failed: ' + status);\r
127                     deferred.reject(status);\r
128                 });\r
129             \r
130             return deferred.promise;\r
131         }\r
132            \r
133         getuserAppRolesCatalog(item) {\r
134             let deferred = this.$q.defer();\r
135             this.$http({\r
136                         method: "GET",\r
137                         url: this.conf.api.appCatalogRoles,\r
138                         params:{appName:item},\r
139                     cache: false,\r
140                     headers: {\r
141                         'X-ECOMP-RequestID':this.uuid.generate()\r
142                     }\r
143                 })\r
144                 .then( res => {\r
145                         if (this.debug)\r
146                                 this.$log.debug('CatalogService::getAppCatalog: result is ' + JSON.stringify(res));\r
147                     // Res is always JSON, but the data object might be an HTML error page.\r
148                     if (! this.utilsService.isValidJSON(res.data)) {\r
149                         var msg = 'CatalogService::getAppCatalog: result data is not JSON';\r
150                         if (this.debug)\r
151                                 this.$log.debug(msg);\r
152                         deferred.reject(msg);\r
153                     } else {\r
154                         if (this.debug)\r
155                                 this.$log.debug('CatalogService::getAppCatalog: success');\r
156                         deferred.resolve(res.data);\r
157                     }\r
158                 })\r
159                 .catch( status => {\r
160                         this.$log.error('CatalogService:getAppCatalog failed: ' + status);\r
161                     deferred.reject(status);\r
162                 });\r
163             return deferred.promise;\r
164         }\r
165         \r
166         \r
167 \r
168     }\r
169     \r
170     CatalogService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];\r
171     angular.module('ecompApp').service('catalogService', CatalogService)\r
172 })();\r