fdd4521495a22e5dc700c9dffab85c614cafd5f6
[portal.git] / ecomp-portal-FE-common / client / app / services / notification / notification.service.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38
39 'use strict';
40 (function () {
41     class NotificationService {
42         constructor($q, $log, $http, conf, uuid,utilsService) {
43             this.$q = $q;
44             this.$log = $log;
45             this.$http = $http;
46             this.conf = conf;
47             this.uuid = uuid;
48             this.notificationCount = {count:0};
49             this.refreshCount = 0;
50             this.maxCount = 0;
51             this.utilsService = utilsService;            
52         }       
53         getNotificationCount() {
54                  return this.notificationCount;
55         }
56         setNotificationCount(count) {
57                 this.notificationCount.count = count;
58         }
59         getRefreshCount() {
60             return this.refreshCount;
61         }
62         setRefreshCount(count){
63             this.refreshCount = count;
64         }
65         setMaxRefreshCount(count){
66             this.maxCount = count;
67         }
68         decrementRefreshCount(){
69             this.refreshCount = this.refreshCount - 1;
70         }
71         
72         getNotificationRate() {
73             let deferred = this.$q.defer();
74             let url = this.conf.api.notificationUpdateRate;
75             this.$http({
76                     method: "GET",
77                     url: url,
78                     cache: false,
79                     headers: {
80                         'X-ECOMP-RequestID':this.uuid.generate()
81                     }
82                 }).then( res => {
83                     // If response comes back as a redirected HTML page which IS NOT a success
84                         if (Object.keys(res.data).length == 0) {
85                         deferred.reject("ecomp::NotificationService::getNotificationRate Failed");
86                     } else {
87                         deferred.resolve(res.data);
88                     }
89                 }).catch( status => {
90                     deferred.reject(status);
91                 });
92             return deferred.promise;
93         }
94         
95         getNotification(){
96                 let deferred = this.$q.defer();
97             this.$http({
98                     method: "GET",
99                     cache: false,
100                     url: this.conf.api.getNotifications,
101                     headers: {
102                         'X-ECOMP-RequestID':this.uuid.generate()
103                     }
104                 })
105                 .then( res => {
106                     // If response comes back as a redirected HTML page which IS NOT a success                  
107                     if (this.utilsService.isValidJSON(res)=== false) {
108                         this.$log.error('NotificationService::getNotification Failed');
109                         deferred.reject("NotificationService::getNotification Failed");
110                     } else {
111                         deferred.resolve(res);
112                     }
113                 })
114                 .catch( status => {
115                         this.$log.error('NotificationService::getNotification Failed', status);
116                     deferred.reject(status);
117                 });
118            
119             return deferred.promise;
120         }
121         getNotificationHistory(){
122                 let deferred = this.$q.defer();
123             this.$http({
124                     method: "GET",
125                     cache: false,
126                     url: this.conf.api.getNotificationHistory,
127                     headers: {
128                         'X-ECOMP-RequestID':this.uuid.generate()
129                     }
130                 })
131                 .then( res => {
132                     // If response comes back as a redirected HTML page which IS NOT a success                  
133                     if (this.utilsService.isValidJSON(res)=== false) {
134                         this.$log.error('NotificationService::getNotification Failed');
135                         deferred.reject("NotificationService::getNotification Failed");
136                     } else {
137                         deferred.resolve(res);
138                     }
139                 })
140                 .catch( status => {
141                         this.$log.error('NotificationService::getNotification Failed', status);
142                     deferred.reject(status);
143                 });
144            
145             return deferred.promise;
146         }
147         
148         getAdminNotification(){
149                 let deferred = this.$q.defer();
150             this.$http({
151                     method: "GET",
152                     cache: false,
153                     url: this.conf.api.getAdminNotifications,
154                     headers: {
155                         'X-ECOMP-RequestID':this.uuid.generate()
156                     }
157                 })
158                 .then( res => {
159                     // If response comes back as a redirected HTML page which IS NOT a success                  
160                     if (this.utilsService.isValidJSON(res)=== false) {
161                         this.$log.error('NotificationService::getAdminNotification Failed');
162                         deferred.reject("NotificationService::getAdminNotification Failed");
163                     } else {
164                         deferred.resolve(res);
165                     }
166                 })
167                 .catch( status => {
168                         this.$log.error('NotificationService::getAdminNotification Failed', status);
169                     deferred.reject(status);
170                 });
171            
172             return deferred.promise;
173         }
174
175         
176         getMessageRecipients(notificationId){
177                 let deferred = this.$q.defer();
178             this.$http({
179                     method: "GET",
180                     cache: false,
181                    url: this.conf.api.getMessageRecipients+"?notificationId="+notificationId,
182                     headers: {
183                         'X-ECOMP-RequestID':this.uuid.generate()
184                     }
185                 })
186                 .then( res => {
187                     // If response comes back as a redirected HTML page which IS NOT a success                  
188                     if (this.utilsService.isValidJSON(res.data)=== false) {
189                         this.$log.error('NotificationService::getMessageRecipients Failed');
190                         deferred.reject("NotificationService::getMessageRecipients Failed");
191                     } else {
192                         deferred.resolve(res.data);
193                     }
194                 })
195                 .catch( status => {
196                         this.$log.error('NotificationService::getMappedRecipients Failed', status);
197                     deferred.reject(status);
198                 });
199            
200             return deferred.promise;
201         }
202         
203         getAppRoleIds(){
204                 let deferred = this.$q.defer();
205             this.$http({
206                     method: "GET",
207                     cache: false,
208                     url: this.conf.api.getAllAppRoleIds,
209                     headers: {
210                         'X-ECOMP-RequestID':this.uuid.generate()
211                     }
212                 })
213                 .then( res => {
214                     // If response comes back as a redirected HTML page which IS NOT a success                  
215                     if (this.utilsService.isValidJSON(res)=== false) {
216                         this.$log.error('NotificationService::getAppRoleIds Failed');
217                         deferred.reject("NotificationService::getAppRoleIds Failed");
218                     } else {
219                         deferred.resolve(res);
220                     }
221                 })
222                 .catch( status => {
223                         this.$log.error('NotificationService::getAppRoleIds Failed', status);
224                     deferred.reject(status);
225                 });           
226             return deferred.promise;
227         }
228
229         getNotificationRoles(notificationId){
230                 let deferred = this.$q.defer();
231             this.$http({
232                     method: "GET",
233                     cache: false,
234                     url: this.conf.api.getNotificationRoles + '/'+notificationId+'/roles',
235                     headers: {
236                         'X-ECOMP-RequestID':this.uuid.generate()
237                     }
238                 })
239                 .then( res => {
240                     // If response comes back as a redirected HTML page which IS NOT a success                  
241                     if (this.utilsService.isValidJSON(res)=== false) {
242                         this.$log.error('NotificationService::getAdminNotification Failed');
243                         deferred.reject("NotificationService::getAdminNotification Failed");
244                     } else {
245                         deferred.resolve(res);
246                     }
247                 })
248                 .catch( status => {
249                         this.$log.error('NotificationService::getAdminNotification Failed', status);
250                     deferred.reject(status);
251                 });           
252             return deferred.promise;
253         }
254
255         addAdminNotification(newAdminNotif){
256             let deferred = this.$q.defer();
257             // this.$log.debug('applications-service::addOnboardingApp with:', newApp);
258
259             this.$http({
260                 method: "POST",
261                 url: this.conf.api.saveNotification,
262                 data: newAdminNotif,
263                 cache: false,
264                 headers: {
265                     'X-ECOMP-RequestID':this.uuid.generate()
266                 }
267             }).then( res => {
268                     // If response comes back as a redirected HTML page which IS NOT a success
269                         // But don't declare an empty list to be an error.
270                     if (res == null || res.data == null) {
271                         deferred.reject("NotificationService::addAdminNotification Failed");
272                     } else {
273                         // this.$log.info('NotificationService::addAdminNotification Succeeded');
274                         deferred.resolve(res.data);
275                     }
276                 })
277                 .catch( status => {
278                     deferred.reject(status);
279                 });
280             return deferred.promise;
281         }
282
283         updateAdminNotification(adminNotif){
284             let deferred = this.$q.defer();
285             this.$http({
286                 method: "POST",
287                 url: this.conf.api.saveNotification,
288                 data: adminNotif,
289                 cache: false,
290                 headers: {
291                     'X-ECOMP-RequestID':this.uuid.generate()
292                 }
293             }).then( res => {
294                     // If response comes back as a redirected HTML page which IS NOT a success
295                         // But don't declare an empty list to be an error.
296                     if (res == null || res.data == null) {
297                         deferred.reject("NotificationService::updateAdminNotification Failed");
298                     } else {
299                         // this.$log.info('NotificationService::updateAdminNotification Succeeded');
300                         deferred.resolve(res.data);
301                     }
302                 })
303                 .catch( status => {
304                     deferred.reject(status);
305                 });
306             return deferred.promise;
307         }
308
309         setNotificationRead(notificationId){
310                 let deferred = this.$q.defer();
311             this.$http({
312                     method: "GET",
313                     cache: false,
314                     url: this.conf.api.notificationRead+"?notificationId="+notificationId,
315                     headers: {
316                         'X-ECOMP-RequestID':this.uuid.generate()
317                     }
318                 })
319                 .then( res => {
320                     // If response comes back as a redirected HTML page which IS NOT a success
321                     if (this.utilsService.isValidJSON(res)=== false) {
322                         this.$log.error('NotificationService::setNotificationRead Failed');
323                         deferred.reject("NotificationService::setNotificationRead Failed");
324                     } else {
325                         deferred.resolve(res);
326                     }
327                 })
328                 .catch( status => {
329                         this.$log.error('NotificationService::setNotificationRead Failed', status);
330                     deferred.reject(status);
331                 });
332            
333             return deferred.promise;    
334         }
335     }
336     NotificationService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4','utilsService'];
337     angular.module('ecompApp').service('notificationService', NotificationService)
338 })();