2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
24 class userNotificationsModalCtrl {
25 constructor($scope, $log, functionalMenuService, confirmBoxService, notificationService, $modal, ngDialog, $state, $filter, items) {
27 $scope.newNotifModel = {
28 'isOnlineUsersOnly': null,
29 'isForAllRolesOptions': null,
30 'selectedPriority': null,
35 'msgDescription': null,
37 'anyTreeItemSelected': false,
39 notificationRoleIds: null
43 $scope.notificationId = null;
44 $scope.selectedCat = null;
45 $scope.selectedEcompFunc = null;
46 this.YN_index_mapping = {
51 $scope.onlineAllUsersOptions = [{
54 "title": "Online Users Only"
59 "title": "Online & Offline Users"
64 $scope.isForAllRolesOptions = [{
76 $scope.priorityOptions = [{
88 $scope.isActiveOptions = [{
99 $scope.newNotifModel.isActive = $scope.isActiveOptions[0];
100 $scope.newNotifModel.selectPriority = $scope.priorityOptions[0];
101 $scope.newNotifModel.isOnlineUsersOnly = $scope.onlineAllUsersOptions[1];
102 $scope.newNotifModel.isForAllRoles = $scope.isForAllRolesOptions[0].value;
103 $scope.newNotifModel.isFunctionalMenu = "Y";
105 $scope.newNotifModel.selectedPriority = $scope.priorityOptions[0].value;
106 $scope.newNotifModel.msgHeader = '';
107 $scope.newNotifModel.msgDescription = '';
108 $scope.newNotifModel.treeTitle = "Functional Menu";
109 $scope.newNotifModel.notifObj = {
110 isCategoriesFunctionalMenu: true
115 $log.debug('userNotificationsModalCtrl::init');
116 this.isSaving = false;
117 var today = new Date();
118 $scope.minDate = today.toISOString().substring(0, 10);
119 var threeMonthsFromNow = new Date();
120 threeMonthsFromNow.setMonth(threeMonthsFromNow.getMonth() + 3);
121 $scope.maxDate = threeMonthsFromNow.toISOString().substring(0, 10);
122 if (items && items.notif) {
124 $log.debug('userNotificationsModalCtrl:init:: Edit notification mode for', items.notif);
125 $scope.isEditMode = true;
126 $scope.editModeObj = {
129 this.notif = _.clone(items.notif);
130 $scope.modalPgTitle = 'View Notification'
131 $scope.newNotifModel.isOnlineUsersOnly = $scope.onlineAllUsersOptions[this.YN_index_mapping[this.notif.isForOnlineUsers]];
132 $scope.newNotifModel.isForAllRoles = $scope.isForAllRolesOptions[this.YN_index_mapping[this.notif.isForAllRoles]].value;
133 $scope.newNotifModel.isActive = $scope.isActiveOptions[this.YN_index_mapping[this.notif.activeYn]];
134 $scope.newNotifModel.selectedPriority = $scope.priorityOptions[this.notif.priority - 1].value;
135 $scope.newNotifModel.startTime = new Date(this.notif.startTime);
136 $scope.newNotifModel.endTime = new Date(this.notif.endTime);
137 $scope.newNotifModel.msgHeader = this.notif.msgHeader;
138 $scope.newNotifModel.msgDescription = this.notif.msgDescription;
139 $scope.notificationId = this.notif.notificationId;
140 $scope.newNotifModel.notificationRoleIds = this.notif.roleIds;
142 notificationRoleIds: this.notif.roleIds
146 $log.debug('AppDetailsModalCtrl:init:: New app mode');
147 $scope.isEditMode = false;
148 $scope.editModeObj = {
151 $scope.modalPgTitle = 'Add a New Notification'
152 this.notif = _.clone($scope.newNotifModel);
154 notificationRoleIds: null
158 this.conflictMessages = {};
160 let handleConflictErrors = err => {
164 if (!err.data.length) { // support objects
165 err.data = [err.data]
167 _.forEach(err.data, item => {
168 _.forEach(item.fields, field => {
169 // set conflict message
170 this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
171 // set field as invalid
172 $scope.appForm[field.name].$setValidity('conflict', false);
173 // set watch once to clear error after user correction
174 watchOnce[field.name]();
177 this.scrollApi.scrollTop();
180 let resetConflict = fieldName => {
181 delete this.conflictMessages[fieldName];
182 if ($scope.appForm[fieldName]) {
183 $scope.appForm[fieldName].$setValidity('conflict', true);
186 $scope.addUserNotificationValidation = function() {
188 if (!($scope.isEditMode)) {
189 var validation = false;
190 if ($scope.isStartDateValidFromToday($scope.newNotifModel.startTime)&&$scope.isStartDateValidFromToday($scope.newNotifModel.endTime)&&$scope.isDateValid($scope.newNotifModel.startTime) && $scope.isDateValid($scope.newNotifModel.endTime) && $scope.newNotifModel.msgHeader != '' && $scope.newNotifModel.msgDescription != '' && ($scope.newNotifModel.startTime < $scope.newNotifModel.endTime)) {
192 if ($scope.newNotifModel.isForAllRoles == 'N') {
193 validation = $scope.checkTreeSelect();
202 /* format the value for viewing a notification */
203 $scope.formatStartDate = function() {
204 if ($scope.newNotifModel.startTime) {
205 $scope.newNotifModel.startTime = $filter('date')($scope.startTime, 'medium');
209 /* format the value for viewing a notification */
210 $scope.formatEndDate = function() {
211 if ($scope.newNotifModel.endTime) {
212 $scope.newNotifModel.endTime = $filter('date')($scope.endTime, 'medium');
216 /*To validate the manual entry of date in MM/DD/YYYY Format*/
218 $scope.isDateValid = function(time) {
219 if (time == undefined) {
222 if (typeof time == 'object') {
225 var startDateformat = time.split('/');
226 if (startDateformat.length != 3) return false;
227 var day = startDateformat[1];
228 var month = startDateformat[0];
229 month = parseInt(month) - 1;
230 var year = startDateformat[2];
231 if (year.length != 4) return false;
232 var composedDate = new Date(year, month, day);
233 return composedDate.getDate() == day &&
234 composedDate.getMonth() == month &&
235 composedDate.getFullYear() == year;
238 /*The manual and drop down calendar should be consistent.
239 Start date must be greater than or equal to current date.The end dates are not allowed after the 3 months from current dates*/
241 $scope.isStartDateValidFromToday = function (time) {
242 if(time == undefined){
245 if(typeof time == 'object'){
248 var startDateformat =time.split('/');
249 if (startDateformat.length != 3) return true;
250 var day = startDateformat[1];
251 var month = startDateformat[0];
252 month= parseInt(month)-1;
253 var year = startDateformat[2];
254 if(year.length!=4) return true;
255 var composedDate = new Date(year, month, day);
256 /* As end dates are not allowed after the 3 months from current dates*/
257 var x = 3; //or whatever offset
258 var CurrentDate = new Date();
259 /*If composed date is less than the current date,error message should display*/
260 if(composedDate<CurrentDate)
262 CurrentDate.setMonth(CurrentDate.getMonth() + x);
263 if(composedDate>CurrentDate)
269 $scope.addUserNotification = function() {
270 $scope.notificationRoleIds = [];
273 for (var fi = 0; fi < $scope.treedata.length; fi++) {
274 var fLevel = $scope.treedata[fi];
276 var fLevelChild = fLevel.child;
278 for (var si = 0; si < fLevelChild.length; si++) {
279 var sLevel = fLevelChild[si];
281 var sLevelChild = sLevel.child;
283 for (var ti = 0; ti < sLevelChild.length; ti++) {
284 var tLevel = sLevelChild[ti];
285 if (tLevel.isSelected && tLevel.roleId) {
286 $scope.newNotifModel.anyTreeItemSelected = true;
287 for (var i in tLevel.roleId)
288 $scope.notificationRoleIds.push(tLevel.roleId[i]);
293 if (sLevel.isSelected && sLevel.roleId) {
294 for (var i in sLevel.roleId)
295 $scope.notificationRoleIds.push(sLevel.roleId[i]);
302 $scope.notificationRoleIds.sort();
303 if (($scope.newNotifModel.isOnlineUsersOnly) && ($scope.newNotifModel.isForAllRoles) && ($scope.newNotifModel.selectedPriority) && ($scope.newNotifModel.isActive) &&
304 ($scope.newNotifModel.startTime) && ($scope.newNotifModel.endTime) && ($scope.newNotifModel.msgHeader != '') && ($scope.newNotifModel.msgDescription != '')) {
305 this.newUserNotification = {
306 'notificationId': $scope.notificationId,
307 'isForOnlineUsers': $scope.newNotifModel.isOnlineUsersOnly.value,
308 'isForAllRoles': $scope.newNotifModel.isForAllRoles,
309 'priority': $scope.newNotifModel.selectedPriority,
310 'activeYn': $scope.newNotifModel.isActive.value,
311 'startTime': $scope.newNotifModel.startTime,
312 'endTime': $scope.newNotifModel.endTime,
313 'msgHeader': $scope.newNotifModel.msgHeader,
314 'msgDescription': $scope.newNotifModel.msgDescription,
315 'roleIds': $scope.notificationRoleIds,
316 'createdDate': new Date()
319 // POST ajax call here;
320 if ($scope.isEditMode) {
321 notificationService.updateAdminNotification(this.newUserNotification)
324 $log.debug('NotificationService:updateAdminNotification:: Admin notification update succeeded!');
325 $scope.closeThisDialog(true);
327 $log.error('notificationService.updateAdminNotfication failed: ' + JSON.stringify(err));
328 switch (err.status) {
329 case '409': // Conflict
330 // handleConflictErrors(err);
332 case '500': // Internal Server Error
333 confirmBoxService.showInformation('There was a problem updating the notification. ' +
334 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
336 case '403': // Forbidden... possible
337 // webjunction error to
339 confirmBoxService.showInformation('There was a problem updating the notification. ' +
340 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
343 confirmBoxService.showInformation('There was a problem updating the notification. ' +
344 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
348 var objOffsetVersion = objAgent.indexOf("MSIE");
349 if (objOffsetVersion != -1) {
351 $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
352 $window.location.reload();
358 notificationService.addAdminNotification(this.newUserNotification)
361 $log.debug('notificationService:addAdminNotification:: Admin notification creation succeeded!,', res);
362 if (res.status == 'ERROR') {
363 confirmBoxService.showInformation('There was a problem adding the notification. ' +
364 ' Error: ' + res.response).then(isConfirmed => {});
368 // $scope.closeThisDialog(true);
369 $scope.$dismiss('cancel');
374 switch (err.status) {
375 case '409': // Conflict
376 // handleConflictErrors(err);
378 case '500': // Internal Server Error
379 confirmBoxService.showInformation('There was a problem adding the notification. ' +
380 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
383 confirmBoxService.showInformation('There was a problem adding the notification. ' +
384 'Please try again. If the problem persists, then try again later. Error: ' +
385 err.status).then(isConfirmed => {});
387 $log.error('notificationService:addAdminNotification error:: ' + JSON.stringify(err));
393 $log.warn('please fill in all required fields');
394 confirmBoxService.showInformation('Please fill in all required fields').then(isConfirmed => {});
397 $scope.functionalMenuRes = {};
398 $scope.checkTreeSelect = function() {
399 if ($scope.treedata) {
400 for (var fi = 0; fi < $scope.treedata.length; fi++) {
401 var fLevel = $scope.treedata[fi];
402 if (fLevel.isSelected) {
405 var sLevel = fLevel.child;
407 for (var si = 0; si < sLevel.length; si++) {
408 if (sLevel[si].isSelected) {
411 var tLevel = sLevel[si].child;
413 for (var ti = 0; ti < tLevel.length; ti++) {
414 if (tLevel[ti].isSelected) {
425 // Populate the category list for category dropdown list
426 let getFunctionalMenu = () => {
427 this.isLoadingTable = true;
429 $log.debug('getFunctionalMenu:init');
431 functionalMenuService.getFunctionalMenuRole().then(role_res => {
432 var menu_role_dict = {};
434 $log.debug('functionalMenuService:getFunctionalMenuRole:: getting result', role_res);
436 for (var i in role_res) {
437 // if first time appear in menu_role_dict
438 if (!(role_res[i].menuId in menu_role_dict)) {
439 menu_role_dict[role_res[i].menuId] = [role_res[i].roleId];
441 menu_role_dict[role_res[i].menuId].push(role_res[i].roleId);
445 functionalMenuService.getManagedFunctionalMenuForNotificationTree().then(res => {
447 $log.debug('functionalMenuService:getManagedFunctionalMenuForNotificationTree:: getting result', res);
448 var exclude_list = ['Favorites'];
450 $scope.functionalMenuRes = res;
452 //Adding children and label attribute to all objects in res
453 for (let i = 0; i < res.length; i++) {
455 res[i].name = res[i].text;
456 res[i].id = res[i].text;
457 res[i].displayCheckbox = true;
458 $scope.checkBoxObj = {
459 isAnyRoleSelected: false
461 res[i].roleId = menu_role_dict[res[i].menuId];
462 res[i].onSelect = function() {
463 $scope.$apply(function() {
464 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
468 if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
469 res[i].isSelected = true;
470 res[i].selected = true;
471 res[i].indeterminate = false;
474 res[i].isSelected = false;
475 res[i].selected = false;
476 res[i].indeterminate = false;
480 // Adding actual child items to children array in res
482 $scope.parentChildDict = {};
483 $scope.parentChildRoleIdDict = {};
484 for (let i = 0; i < res.length; i++) {
485 let parentId = res[i].menuId;
486 $scope.parentChildDict[parentId] = [];
487 $scope.parentChildRoleIdDict[parentId] = [];
488 for (let j = 0; j < res.length; j++) {
489 let childId = res[j].parentMenuId;
490 if (parentId === childId) {
491 res[i].child.push(res[j]);
492 $scope.parentChildDict[parentId].push(res[j].menuId);
493 //if res[j].roleId is defined
495 for (let k in res[j].roleId) {
496 $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
503 //check if grand children exist
504 for (var key in $scope.parentChildDict) {
505 var child = $scope.parentChildDict[key];
506 var isGrandParent = false;
507 if (child.length > 0) {
508 for (var i in child) {
509 if ($scope.parentChildDict[child[i]].length > 0) {
510 isGrandParent = true;
516 for (var i in child) {
517 // if the child has children
518 if ($scope.parentChildDict[child[i]].length > 0) {
519 for (var j in $scope.parentChildRoleIdDict[child[i]]) {
520 if ($scope.parentChildRoleIdDict[key].indexOf($scope.parentChildRoleIdDict[child[i]][j]) === -1) {
521 $scope.parentChildRoleIdDict[key].push($scope.parentChildRoleIdDict[child[i]][j]);
530 // Sort the top-level menu items in order based on the column
531 res.sort(function(a, b) {
532 return a.column - b.column;
535 // Sort all the child in order based on the column
536 for (let i = 0; i < res.length; i++) {
537 res[i].child.sort(function(a, b) {
538 return a.column - b.column;
542 //Forming actual parent items
543 for (let i = 0; i < res.length; i++) {
544 let parentId = res[i].parentMenuId;
545 if (parentId === null) {
546 actualData.push(res[i]);
549 var treedata = actualData[0].child;
550 $scope.treedata = [];
552 /*Remove favorite from the list */
553 for (var i in treedata) {
554 if (!(treedata[i].name.indexOf(exclude_list) > -1)) {
555 $scope.treedata.push(treedata[i])
558 //setting b2b tree parameter
559 $scope.settingTreeParam();
562 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
564 this.isLoadingTable = false;
568 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
573 let getAppRoleIds = () => {
575 isCategoriesFunctionalMenu: false
577 notificationService.getAppRoleIds().then(res => {
579 $log.debug('notificationService:getAppRoleIds:: getting result', res);
582 var app_id_name_list = {};
583 $scope.checkBoxObj = {
584 isAnyRoleSelected: false
586 for (let i = 0; i < res.length; i++) {
587 if (!(res[i].appId in app_id_name_list)) {
588 app_id_name_list[res[i].appId] = res[i].appName;
591 res[i].name = res[i].roleName;
592 res[i].displayCheckbox = true;
593 res[i].id = res[i].roleId;
594 res[i].menuId = res[i].roleId;
595 res[i].parentMenuId = res[i].appId;
596 res[i].can_check = true;
597 res[i].roleId = [res[i].roleId];
598 res[i].onSelect = function() {
599 $scope.$apply(function() {
600 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
603 /*assigning selected value*/
604 if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
605 res[i].isSelected = true;
606 res[i].selected = true;
607 res[i].indeterminate = false;
610 res[i].isSelected = false;
611 res[i].selected = false;
612 res[i].indeterminate = false;
616 for (var app_id in app_id_name_list) {
619 new_res.name = app_id_name_list[app_id];
621 new_res.displayCheckbox = true;
622 new_res.menuId = app_id;
623 new_res.parentMenuId = null;
624 new_res.appId = null;
625 new_res.can_check = true;
626 new_res.roleId = null;
627 new_res.onSelect = function() {
628 $scope.$apply(function() {
629 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
634 $scope.parentChildRoleIdDict = {};
635 //Adding actual child items to child array in res objects
636 for (let i = 0; i < res.length; i++) {
637 let parentId = res[i].menuId;
638 $scope.parentChildRoleIdDict[parentId] = [];
639 for (let j = 0; j < res.length; j++) {
640 let childId = res[j].parentMenuId;
641 if (parentId == childId) {
642 res[i].child.push(res[j]);
644 for (let k in res[j].roleId) {
645 $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
652 //Forming actual parent items
653 for (let i = 0; i < res.length; i++) {
654 let parentId = res[i].parentMenuId;
655 if (parentId === null) {
656 actualData.push(res[i]);
660 $scope.treedata = actualData;
661 //setting correct parameters for b2b tree
662 $scope.settingTreeParam();
664 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
666 this.isLoadingTable = false;
669 $scope.getFunctionalMenu = function() {
670 $scope.treeTitle = "Functional Menu";
673 $scope.getAppRoleIds = function() {
674 $scope.treeTitle = "Applications/Roles";
680 $scope.settingTreeParam = function() {
681 /**************first level****************/
682 for (var fi = 0; fi < $scope.treedata.length; fi++) {
683 var fLevel = $scope.treedata[fi];
684 var sLevel = $scope.treedata[fi].child;
685 var sLevelSelectedCount = 0;
686 var sLevelChildNumber = 0
687 if (fLevel.child.length == 0 && fLevel.roleId == null) {
690 /**************Second level****************/
691 var sLevelDelArray = [];
692 for (var si = 0; si < sLevel.length; si++) {
693 var deletThisSLev = false;
694 if (sLevel[si].child.length == 0 && sLevel[si].roleId == null) {
695 sLevel[si].displayCheckbox = false;
696 sLevelDelArray.push(sLevel[si].name);
697 sLevel[si].name = '';
698 sLevel[si].active = false;
699 delete sLevel[si].child;
700 } else if (sLevel[si].child.length == 0) {
701 delete sLevel[si].child;
703 /**************Third level****************/
704 var tLevel = sLevel[si].child;
705 var tLevelSelectedCount = 0;
706 var tLevelChildNumber = 0;
708 var tLevelDelArray = [];
709 var tLevelLen = tLevel.length;
710 var tLevelRoleIdUndefined = 0;
711 for (var ti = 0; ti < tLevel.length; ti++) {
712 delete tLevel[ti].child;
713 if (tLevel[ti].roleId == null) {
714 tLevel[ti].displayCheckbox = false;
715 tLevelDelArray.push(tLevel[ti].name);
716 tLevel[ti].name = '';
717 tLevel[ti].active = false;
718 tLevelRoleIdUndefined++
720 if (tLevel[ti].isSelected)
721 tLevelSelectedCount++;
723 if (tLevel[ti].displayCheckbox)
727 if (tLevelRoleIdUndefined == tLevelLen)
728 deletThisSLev = true;
729 if (tLevelSelectedCount == tLevelChildNumber) {
730 sLevel[si].isSelected = true;
731 sLevel[si].indeterminate = false;
732 sLevel[si].active = true;
733 } else if (tLevelSelectedCount > 0) {
734 sLevel[si].indeterminate = true;
735 sLevel[si].active = true;
738 /*Cleanup unused third level items*/
739 for (var i = 0; i < tLevelDelArray.length; i++) {
740 var name = tLevelDelArray[i];
741 for (var ti = 0; ti < tLevel.length; ti++) {
742 if (name == tLevel[ti].text) {
743 tLevel.splice(ti, 1);
750 if (deletThisSLev) { //remove the whole second level item if all it's child has no roleId
751 sLevel[si].displayCheckbox = false;
752 sLevelDelArray.push(sLevel[si].name);
753 sLevel[si].name = '';
754 sLevel[si].active = false;
756 if (sLevel[si].isSelected)
757 sLevelSelectedCount++;
758 if (sLevel[si].displayCheckbox)
762 if (sLevelSelectedCount == sLevelChildNumber && sLevelChildNumber != 0) {
763 fLevel.isSelected = true;
764 fLevel.indeterminate = false;
765 fLevel.active = true;
766 } else if (sLevelSelectedCount > 0) {
767 fLevel.indeterminate = true;
768 fLevel.active = true;
770 //fLevel.active=false;
771 fLevel.indeterminate = false;
773 /*Cleanup unused second level items*/
774 for (var i = 0; i < sLevelDelArray.length; i++) {
775 var name = sLevelDelArray[i];
776 for (var si = 0; si < sLevel.length; si++) {
777 if (name == sLevel[si].text) {
778 sLevel.splice(si, 1);
793 userNotificationsModalCtrl.$inject = ['$scope', '$log', 'functionalMenuService', 'confirmBoxService', 'notificationService', '$modal', 'ngDialog', '$state', '$filter', 'items'];
794 angular.module('ecompApp').controller('userNotificationsModalCtrl', userNotificationsModalCtrl);