4207bc6e6aa97b98a71fe5961d08a6e084d7ede8
[portal.git] / ecomp-portal-FE-common / client / app / views / user-notifications-admin / user.notifications.controller.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 'use strict';
21
22 (function () {
23
24     class userNotificationsCtrl {
25         constructor($scope, $log, notificationService, confirmBoxService, $modal, ngDialog, $state) {
26
27                 var priorityItems={"1":"Normal","2":"Important"};
28                 $scope.priorityItems=priorityItems;
29                 $scope.searchString='';
30             $scope.externalNotification="External System";
31                 $scope.itemExpired={"background-color":"silver "};
32                 $scope.showInput = true;
33                 $scope.totalPages1 = 0;
34                 $scope.viewPerPage1 = 15;
35                 $scope.currentPage1 = 1;
36                 $scope.showLoader = false;
37                 $scope.firstPlay = true;
38                 // Start with empty list to silence error in console
39                 $scope.tableData = [];
40                 $scope.tableAdminNotifItems = [];
41                  let getAdminNotifications = () => {
42                $scope.isLoadingTable = true;
43                 notificationService.getAdminNotification().then(res => {
44                 $scope.adminNotifications = res.data;
45                 $scope.isLoadingTable = false;
46                 $scope.tableData = res.data;
47                         var totalItems = $scope.tableData.length;
48                         $scope.totalPages1  = Math.ceil(totalItems / $scope.viewPerPage1);
49                         $scope.showLoader = false;
50                         $scope.currentPage1=1;
51                         var endIndex = 1 * $scope.viewPerPage1;
52                         var startIndex = endIndex - $scope.viewPerPage1;
53                         $scope.tableAdminNotifItems = $scope.tableData.slice(startIndex, endIndex); 
54                 }).catch(err => {
55                 $log.error('userNotificationsCtrl:getAdminNotifications:: error ', err);
56                 $scope.isLoadingTable = false;
57             });
58              }
59                  
60            getAdminNotifications();           
61
62                 $scope.customPageHandler = function(num) {
63                         $scope.currentPage1=num;
64                         var endIndex = num * $scope.viewPerPage1;
65                         var startIndex = endIndex - $scope.viewPerPage1;
66                         $scope.tableAdminNotifItems = $scope.tableData.slice(startIndex, endIndex);
67                 };
68                   
69
70           
71
72                 
73            $scope.removeUserNotification = function (selectedAdminNotification) {
74                 selectedAdminNotification.activeYn = 'N';
75                 confirmBoxService.deleteItem(selectedAdminNotification.msgHeader)
76                     .then(isConfirmed => {
77                         if (isConfirmed) {
78                             notificationService.updateAdminNotification(selectedAdminNotification)
79                                 .then(() => {
80                                     getAdminNotifications();
81                                 }).catch(err => {
82                                     switch (err.status) {
83                                         case '409':         // Conflict
84                                             // handleConflictErrors(err);
85                                             break;
86                                         case '500':         // Internal Server
87                                                                                                                         // Error
88                                             confirmBoxService.showInformation('There was a problem updating the notification. ' +
89                                                 'Please try again later. Error: ' + err.status).then(isConfirmed => { });
90                                             break;
91                                         case '403':         // Forbidden...
92                                                                                                                         // possible
93                                                                                                                         // webjunction error
94                                                                                                                         // to try again
95                                             confirmBoxService.showInformation('There was a problem updating the notification. ' +
96                                                 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => { });
97                                             break;
98                                         default:
99                                             confirmBoxService.showInformation('There was a problem updating the notification. ' +
100                                                 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => { });
101                                     }
102                                     $log.error('UserNotifCtlr::updateAdminNOtif failed: ' + JSON.stringify(err));
103                                 }).finally(() => {
104                                     var objOffsetVersion = objAgent.indexOf("MSIE");
105                                     if (objOffsetVersion != -1) {
106                                         $window.location.reload();       
107                                     }
108                                 });
109                         }
110                     }).catch(err => {
111                         $log.error('UserNotifCtlr::deleteItem error: ' + err);
112                     });
113             }
114            
115
116           
117                  $scope.showDetailedJsonMessage=function (selectedAdminNotification) {
118                          notificationService.getMessageRecipients(selectedAdminNotification.notificationId).then(res =>{
119                      $scope.messageRecipients = res;
120                                  var messageObject=JSON.parse(selectedAdminNotification.msgDescription);
121                                  var html="";
122                                  html+='<p>'+'Message Source'+' : '+selectedAdminNotification.msgSource+'</p>';
123                                  html+='<p>'+'Message Title'+' : '+selectedAdminNotification.msgHeader+'</p>';
124                                  html+='<p>'+'Message Recipient'+' : '+$scope.messageRecipients+'</p>';
125
126                                  for(var field in  messageObject){
127                                          if(field=='eventDate'||field=='lastModifiedDate'){
128                                                  html+='<p>'+field+' : '+new Date(+messageObject[field])+'</p>';
129                                                   
130                                          }else{
131                                          html+='<p>'+field+' : '+messageObject[field]+'</p>';
132                                          
133                                          }
134                                  }
135
136                      var modalInstance = ngDialog.open({
137                                     templateUrl: 'app/views/user-notifications-admin/user.notifications.Json.details.modal.page.html',
138                                     controller: 'userNotificationCtrl',
139                                     resolve: {
140                                         message: function () {
141                                                 var message = {
142                                                            title:    '',
143                                                 text:    html
144                                                 
145                                                 };
146                                           return message;
147                                         },
148                                      
149                                       }
150                                   }); 
151                      
152                  }).catch(err => {
153                  $log.error('userNotificationsCtrl:getMessageRecipients:: error ', err);
154                  $scope.isLoadingTable = false;
155              });
156
157                          };
158     
159                          
160             $scope.editUserNotificationModal = function (selectedAdminNotification) {
161
162                 // retrieve roleIds here
163                 selectedAdminNotification.roleIds = null;
164                 notificationService.getNotificationRoles(selectedAdminNotification.notificationId)
165                     .then(res => {
166                         selectedAdminNotification.roleIds = res.data;
167
168                         $scope.openUserNotificationModal(selectedAdminNotification);
169                     }).catch(err => {
170                         $log.error('UserNotifCtlr:getNotificationRoles:: error ', err);
171
172                     });
173             }
174
175             $scope.openUserNotificationModal = function (selectedAdminNotification) {
176                 let data = null;
177                 if (selectedAdminNotification) {
178                     data = {
179                         notif: selectedAdminNotification
180                     }
181                 }
182                 ngDialog.open({
183                     templateUrl: 'app/views/user-notifications-admin/user.notifications.modal.page.html',
184                     controller: 'userNotificationsModalCtrl',
185                     controllerAs: 'userNotifModal',
186                     data: data
187                 }).closePromise.then(function (needUpdate) {
188                     getAdminNotifications();
189                 });
190             }
191
192         }
193     }
194     userNotificationsCtrl.$inject = ['$scope', '$log', 'notificationService', 'confirmBoxService', '$modal', 'ngDialog', '$state'];
195     angular.module('ecompApp').controller('userNotificationsCtrl', userNotificationsCtrl);
196 })();