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