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