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