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