[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / services / notification / notification.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 /**\r
21  * Created by wl849v on 12/14/2016.\r
22  */\r
23 'use strict';\r
24 (function () {\r
25     class NotificationService {\r
26         constructor($q, $log, $http, conf, uuid,utilsService) {\r
27             this.$q = $q;\r
28             this.$log = $log;\r
29             this.$http = $http;\r
30             this.conf = conf;\r
31             this.uuid = uuid;\r
32             this.notificationCount = {count:0};\r
33             this.refreshCount = 0;\r
34             this.maxCount = 0;\r
35             this.utilsService = utilsService;            \r
36         }       \r
37         getNotificationCount() {\r
38                  return this.notificationCount;\r
39         }\r
40         setNotificationCount(count) {\r
41                 this.notificationCount.count = count;\r
42         }\r
43         getRefreshCount() {\r
44             return this.refreshCount;\r
45         }\r
46         setRefreshCount(count){\r
47             this.refreshCount = count;\r
48         }\r
49         setMaxRefreshCount(count){\r
50             this.maxCount = count;\r
51         }\r
52         decrementRefreshCount(){\r
53             this.refreshCount = this.refreshCount - 1;\r
54         }\r
55         \r
56         getNotificationRate() {\r
57             let deferred = this.$q.defer();\r
58             let url = this.conf.api.notificationUpdateRate;\r
59             this.$http({\r
60                     method: "GET",\r
61                     url: url,\r
62                     cache: false,\r
63                     headers: {\r
64                         'X-ECOMP-RequestID':this.uuid.generate()\r
65                     }\r
66                 }).then( res => {\r
67                     // If response comes back as a redirected HTML page which IS NOT a success\r
68                         if (Object.keys(res.data).length == 0) {\r
69                         deferred.reject("ecomp::NotificationService::getNotificationRate Failed");\r
70                     } else {\r
71                         deferred.resolve(res.data);\r
72                     }\r
73                 }).catch( status => {\r
74                     deferred.reject(status);\r
75                 });\r
76             return deferred.promise;\r
77         }\r
78         \r
79         getNotification(){\r
80                 let deferred = this.$q.defer();\r
81             this.$http({\r
82                     method: "GET",\r
83                     cache: false,\r
84                     url: this.conf.api.getNotifications,\r
85                     headers: {\r
86                         'X-ECOMP-RequestID':this.uuid.generate()\r
87                     }\r
88                 })\r
89                 .then( res => {\r
90                     // If response comes back as a redirected HTML page which IS NOT a success                  \r
91                     if (this.utilsService.isValidJSON(res)=== false) {\r
92                         this.$log.error('NotificationService::getNotification Failed');\r
93                         deferred.reject("NotificationService::getNotification Failed");\r
94                     } else {\r
95                         deferred.resolve(res);\r
96                     }\r
97                 })\r
98                 .catch( status => {\r
99                         this.$log.error('NotificationService::getNotification Failed', status);\r
100                     deferred.reject(status);\r
101                 });\r
102            \r
103             return deferred.promise;\r
104         }\r
105         getNotificationHistory(){\r
106                 let deferred = this.$q.defer();\r
107             this.$http({\r
108                     method: "GET",\r
109                     cache: false,\r
110                     url: this.conf.api.getNotificationHistory,\r
111                     headers: {\r
112                         'X-ECOMP-RequestID':this.uuid.generate()\r
113                     }\r
114                 })\r
115                 .then( res => {\r
116                     // If response comes back as a redirected HTML page which IS NOT a success                  \r
117                     if (this.utilsService.isValidJSON(res)=== false) {\r
118                         this.$log.error('NotificationService::getNotification Failed');\r
119                         deferred.reject("NotificationService::getNotification Failed");\r
120                     } else {\r
121                         deferred.resolve(res);\r
122                     }\r
123                 })\r
124                 .catch( status => {\r
125                         this.$log.error('NotificationService::getNotification Failed', status);\r
126                     deferred.reject(status);\r
127                 });\r
128            \r
129             return deferred.promise;\r
130         }\r
131         \r
132         getAdminNotification(){\r
133                 let deferred = this.$q.defer();\r
134             this.$http({\r
135                     method: "GET",\r
136                     cache: false,\r
137                     url: this.conf.api.getAdminNotifications,\r
138                     headers: {\r
139                         'X-ECOMP-RequestID':this.uuid.generate()\r
140                     }\r
141                 })\r
142                 .then( res => {\r
143                     // If response comes back as a redirected HTML page which IS NOT a success                  \r
144                     if (this.utilsService.isValidJSON(res)=== false) {\r
145                         this.$log.error('NotificationService::getAdminNotification Failed');\r
146                         deferred.reject("NotificationService::getAdminNotification Failed");\r
147                     } else {\r
148                         deferred.resolve(res);\r
149                     }\r
150                 })\r
151                 .catch( status => {\r
152                         this.$log.error('NotificationService::getAdminNotification Failed', status);\r
153                     deferred.reject(status);\r
154                 });\r
155            \r
156             return deferred.promise;\r
157         }\r
158 \r
159         \r
160         getMessageRecipients(notificationId){\r
161                 let deferred = this.$q.defer();\r
162             this.$http({\r
163                     method: "GET",\r
164                     cache: false,\r
165                    url: this.conf.api.getMessageRecipients+"?notificationId="+notificationId,\r
166                     headers: {\r
167                         'X-ECOMP-RequestID':this.uuid.generate()\r
168                     }\r
169                 })\r
170                 .then( res => {\r
171                     // If response comes back as a redirected HTML page which IS NOT a success                  \r
172                     if (this.utilsService.isValidJSON(res.data)=== false) {\r
173                         this.$log.error('NotificationService::getMessageRecipients Failed');\r
174                         deferred.reject("NotificationService::getMessageRecipients Failed");\r
175                     } else {\r
176                         deferred.resolve(res.data);\r
177                     }\r
178                 })\r
179                 .catch( status => {\r
180                         this.$log.error('NotificationService::getMappedRecipients Failed', status);\r
181                     deferred.reject(status);\r
182                 });\r
183            \r
184             return deferred.promise;\r
185         }\r
186         \r
187         getAppRoleIds(){\r
188                 let deferred = this.$q.defer();\r
189             this.$http({\r
190                     method: "GET",\r
191                     cache: false,\r
192                     url: this.conf.api.getAllAppRoleIds,\r
193                     headers: {\r
194                         'X-ECOMP-RequestID':this.uuid.generate()\r
195                     }\r
196                 })\r
197                 .then( res => {\r
198                     // If response comes back as a redirected HTML page which IS NOT a success                  \r
199                     if (this.utilsService.isValidJSON(res)=== false) {\r
200                         this.$log.error('NotificationService::getAppRoleIds Failed');\r
201                         deferred.reject("NotificationService::getAppRoleIds Failed");\r
202                     } else {\r
203                         deferred.resolve(res);\r
204                     }\r
205                 })\r
206                 .catch( status => {\r
207                         this.$log.error('NotificationService::getAppRoleIds Failed', status);\r
208                     deferred.reject(status);\r
209                 });           \r
210             return deferred.promise;\r
211         }\r
212 \r
213         getNotificationRoles(notificationId){\r
214                 let deferred = this.$q.defer();\r
215             this.$http({\r
216                     method: "GET",\r
217                     cache: false,\r
218                     url: this.conf.api.getNotificationRoles + '/'+notificationId+'/roles',\r
219                     headers: {\r
220                         'X-ECOMP-RequestID':this.uuid.generate()\r
221                     }\r
222                 })\r
223                 .then( res => {\r
224                     // If response comes back as a redirected HTML page which IS NOT a success                  \r
225                     if (this.utilsService.isValidJSON(res)=== false) {\r
226                         this.$log.error('NotificationService::getAdminNotification Failed');\r
227                         deferred.reject("NotificationService::getAdminNotification Failed");\r
228                     } else {\r
229                         deferred.resolve(res);\r
230                     }\r
231                 })\r
232                 .catch( status => {\r
233                         this.$log.error('NotificationService::getAdminNotification Failed', status);\r
234                     deferred.reject(status);\r
235                 });           \r
236             return deferred.promise;\r
237         }\r
238 \r
239         addAdminNotification(newAdminNotif){\r
240             let deferred = this.$q.defer();\r
241             // this.$log.debug('applications-service::addOnboardingApp with:', newApp);\r
242 \r
243             this.$http({\r
244                 method: "POST",\r
245                 url: this.conf.api.saveNotification,\r
246                 data: newAdminNotif,\r
247                 cache: false,\r
248                 headers: {\r
249                     'X-ECOMP-RequestID':this.uuid.generate()\r
250                 }\r
251             }).then( res => {\r
252                     // If response comes back as a redirected HTML page which IS NOT a success\r
253                         // But don't declare an empty list to be an error.\r
254                     if (res == null || res.data == null) {\r
255                         deferred.reject("NotificationService::addAdminNotification Failed");\r
256                     } else {\r
257                         // this.$log.info('NotificationService::addAdminNotification Succeeded');\r
258                         deferred.resolve(res.data);\r
259                     }\r
260                 })\r
261                 .catch( status => {\r
262                     deferred.reject(status);\r
263                 });\r
264             return deferred.promise;\r
265         }\r
266 \r
267         updateAdminNotification(adminNotif){\r
268             let deferred = this.$q.defer();\r
269             this.$http({\r
270                 method: "POST",\r
271                 url: this.conf.api.saveNotification,\r
272                 data: adminNotif,\r
273                 cache: false,\r
274                 headers: {\r
275                     'X-ECOMP-RequestID':this.uuid.generate()\r
276                 }\r
277             }).then( res => {\r
278                     // If response comes back as a redirected HTML page which IS NOT a success\r
279                         // But don't declare an empty list to be an error.\r
280                     if (res == null || res.data == null) {\r
281                         deferred.reject("NotificationService::updateAdminNotification Failed");\r
282                     } else {\r
283                         // this.$log.info('NotificationService::updateAdminNotification Succeeded');\r
284                         deferred.resolve(res.data);\r
285                     }\r
286                 })\r
287                 .catch( status => {\r
288                     deferred.reject(status);\r
289                 });\r
290             return deferred.promise;\r
291         }\r
292 \r
293         setNotificationRead(notificationId){\r
294                 let deferred = this.$q.defer();\r
295             this.$http({\r
296                     method: "GET",\r
297                     cache: false,\r
298                     url: this.conf.api.notificationRead+"?notificationId="+notificationId,\r
299                     headers: {\r
300                         'X-ECOMP-RequestID':this.uuid.generate()\r
301                     }\r
302                 })\r
303                 .then( res => {\r
304                     // If response comes back as a redirected HTML page which IS NOT a success\r
305                     if (this.utilsService.isValidJSON(res)=== false) {\r
306                         this.$log.error('NotificationService::setNotificationRead Failed');\r
307                         deferred.reject("NotificationService::setNotificationRead Failed");\r
308                     } else {\r
309                         deferred.resolve(res);\r
310                     }\r
311                 })\r
312                 .catch( status => {\r
313                         this.$log.error('NotificationService::setNotificationRead Failed', status);\r
314                     deferred.reject(status);\r
315                 });\r
316            \r
317             return deferred.promise;    \r
318         }\r
319     }\r
320     NotificationService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4','utilsService'];\r
321     angular.module('ecompApp').service('notificationService', NotificationService)\r
322 })();\r