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,
38 'roleObj': {notificationRoleIds:null}
41 $scope.notificationId = null;
42 $scope.selectedCat = null;
43 $scope.selectedEcompFunc = null;
44 this.YN_index_mapping = {
49 $scope.onlineAllUsersOptions = [
50 { "index": 0, "value": "Y", "title": "Online Users Only" },
51 { "index": 1, "value": "N", "title": "Online & Offline Users" }
55 $scope.isForAllRolesOptions = [
56 { "index": 0, "value": "Y", "title": "Yes" },
57 { "index": 1, "value": "N", "title": "No" }
60 $scope.priorityOptions = [
61 { "index": 0, "value": 1, "title": "Normal" },
62 { "index": 1, "value": 2, "title": "Important" }
65 $scope.isActiveOptions = [
66 { "index": 0, "value": "Y", "title": "Yes" },
67 { "index": 1, "value": "N", "title": "No" }
69 $scope.newNotifModel.isActive = $scope.isActiveOptions[0];
70 $scope.newNotifModel.selectPriority = $scope.priorityOptions[0];
71 $scope.newNotifModel.isOnlineUsersOnly = $scope.onlineAllUsersOptions[1];
72 $scope.newNotifModel.isForAllRoles=$scope.isForAllRolesOptions[0].value;
73 $scope.newNotifModel.isFunctionalMenu ="Y";
75 $scope.newNotifModel.selectedPriority=$scope.priorityOptions[0].value;
76 $scope.newNotifModel.msgHeader = '';
77 $scope.newNotifModel.msgDescription = '';
78 $scope.newNotifModel.treeTitle="Functional Menu";
79 $scope.newNotifModel.notifObj= {isCategoriesFunctionalMenu:true};
83 $log.debug('userNotificationsModalCtrl::init');
84 this.isSaving = false;
85 var today = new Date();
86 $scope.minDate = today.toISOString().substring(0, 10);
87 var threeMonthsFromNow = new Date();
88 threeMonthsFromNow.setMonth(threeMonthsFromNow.getMonth() + 3);
89 $scope.maxDate = threeMonthsFromNow.toISOString().substring(0, 10);
90 if (items && items.notif) {
92 $log.debug('userNotificationsModalCtrl:init:: Edit notification mode for', items.notif);
93 $scope.isEditMode = true;
94 $scope.editModeObj = {isEditMode: true};
95 this.notif = _.clone(items.notif);
96 $scope.modalPgTitle = 'View Notification'
97 $scope.newNotifModel.isOnlineUsersOnly = $scope.onlineAllUsersOptions[this.YN_index_mapping[this.notif.isForOnlineUsers]];
98 $scope.newNotifModel.isForAllRoles = $scope.isForAllRolesOptions[this.YN_index_mapping[this.notif.isForAllRoles]].value;
99 $scope.newNotifModel.isActive = $scope.isActiveOptions[this.YN_index_mapping[this.notif.activeYn]];
100 $scope.newNotifModel.selectedPriority = $scope.priorityOptions[this.notif.priority - 1].value;
101 $scope.newNotifModel.startTime = new Date(this.notif.startTime);
102 $scope.newNotifModel.endTime = new Date(this.notif.endTime);
103 $scope.newNotifModel.msgHeader = this.notif.msgHeader;
104 $scope.newNotifModel.msgDescription = this.notif.msgDescription;
105 $scope.notificationId = this.notif.notificationId;
106 $scope.newNotifModel.notificationRoleIds = this.notif.roleIds;
107 $scope.roleObj = {notificationRoleIds:this.notif.roleIds};
110 $log.debug('AppDetailsModalCtrl:init:: New app mode');
111 $scope.isEditMode = false;
112 $scope.editModeObj = {isEditMode: false};
113 $scope.modalPgTitle = 'Add a New Notification'
114 this.notif = _.clone($scope.newNotifModel);
115 $scope.roleObj = {notificationRoleIds:null};
118 this.conflictMessages = {};
120 let handleConflictErrors = err => {
124 if(!err.data.length){ // support objects
125 err.data = [err.data]
127 _.forEach(err.data, item => {
128 _.forEach(item.fields, field => {
129 // set conflict message
130 this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
131 // set field as invalid
132 $scope.appForm[field.name].$setValidity('conflict', false);
133 // set watch once to clear error after user correction
134 watchOnce[field.name]();
137 this.scrollApi.scrollTop();
140 let resetConflict = fieldName => {
141 delete this.conflictMessages[fieldName];
142 if($scope.appForm[fieldName]){
143 $scope.appForm[fieldName].$setValidity('conflict', true);
146 $scope.addUserNotificationValidation = function () {
148 if (!($scope.isEditMode)) {
149 var validation=false;
150 if($scope.isDateValid($scope.newNotifModel.startTime) && $scope.isDateValid($scope.newNotifModel.endTime) && $scope.newNotifModel.msgHeader != '' && $scope.newNotifModel.msgDescription != '' && ($scope.newNotifModel.startTime<$scope.newNotifModel.endTime)){
152 if( $scope.newNotifModel.isForAllRoles=='N'){
153 validation = $scope.checkTreeSelect();
163 /* format the value for viewing a notification */
164 $scope.formatStartDate = function () {
165 if ($scope.newNotifModel.startTime) {
166 $scope.newNotifModel.startTime = $filter('date')($scope.startTime, 'medium');
170 /* format the value for viewing a notification */
171 $scope.formatEndDate = function () {
172 if($scope.newNotifModel.endTime){
173 $scope.newNotifModel.endTime = $filter('date')($scope.endTime, 'medium');
177 $scope.isDateValid = function (time) {
178 if(time == undefined){
181 if(typeof time == 'object'){
184 var startDateformat =time.split('/');
185 if (startDateformat.length != 3) return false;
186 var day = startDateformat[1];
187 var month = startDateformat[0];
188 month= parseInt(month)-1;
189 var year = startDateformat[2];
190 if(year.length!=4) return false;
191 var composedDate = new Date(year, month, day);
192 return composedDate.getDate() == day &&
193 composedDate.getMonth() == month &&
194 composedDate.getFullYear() == year;
199 $scope.addUserNotification = function () {
200 $scope.notificationRoleIds = [];
203 for(var fi=0;fi<$scope.treedata.length;fi++){
204 var fLevel = $scope.treedata[fi];
206 var fLevelChild = fLevel.child;
207 for(var si=0;si<fLevelChild.length;si++){
208 var sLevel = fLevelChild[si];
210 var sLevelChild = sLevel.child;
212 for(var ti=0;ti< sLevelChild.length;ti++){
213 var tLevel = sLevelChild[ti];
214 if(tLevel.isSelected && tLevel.roleId){
215 $scope.newNotifModel.anyTreeItemSelected=true;
216 for(var i in tLevel.roleId)
217 $scope.notificationRoleIds.push(tLevel.roleId[i]);
222 if(sLevel.isSelected && sLevel.roleId){
223 for(var i in sLevel.roleId)
224 $scope.notificationRoleIds.push(sLevel.roleId[i]);
230 $scope.notificationRoleIds.sort();
231 if (($scope.newNotifModel.isOnlineUsersOnly) && ($scope.newNotifModel.isForAllRoles) && ($scope.newNotifModel.selectedPriority) && ($scope.newNotifModel.isActive)
232 && ($scope.newNotifModel.startTime) && ($scope.newNotifModel.endTime) && ($scope.newNotifModel.msgHeader != '') && ($scope.newNotifModel.msgDescription != '')) {
233 this.newUserNotification =
235 'notificationId':$scope.notificationId,
236 'isForOnlineUsers': $scope.newNotifModel.isOnlineUsersOnly.value,
237 'isForAllRoles': $scope.newNotifModel.isForAllRoles,
238 'priority': $scope.newNotifModel.selectedPriority,
239 'activeYn': $scope.newNotifModel.isActive.value,
240 'startTime': $scope.newNotifModel.startTime,
241 'endTime': $scope.newNotifModel.endTime,
242 'msgHeader': $scope.newNotifModel.msgHeader,
243 'msgDescription': $scope.newNotifModel.msgDescription,
244 'roleIds': $scope.notificationRoleIds,
245 'createdDate': new Date()
248 // POST ajax call here;
249 if ($scope.isEditMode) {
250 notificationService.updateAdminNotification(this.newUserNotification)
253 $log.debug('NotificationService:updateAdminNotification:: Admin notification update succeeded!');
254 $scope.closeThisDialog(true);
256 $log.error('notificationService.updateAdminNotfication failed: ' + JSON.stringify(err));
257 switch (err.status) {
258 case '409': // Conflict
259 // handleConflictErrors(err);
261 case '500': // Internal Server Error
262 confirmBoxService.showInformation('There was a problem updating the notification. ' +
263 'Please try again later. Error: ' + err.status).then(isConfirmed => { });
265 case '403': // Forbidden... possible
266 // webjunction error to
268 confirmBoxService.showInformation('There was a problem updating the notification. ' +
269 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => { });
272 confirmBoxService.showInformation('There was a problem updating the notification. ' +
273 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => { });
277 var objOffsetVersion = objAgent.indexOf("MSIE");
278 if (objOffsetVersion != -1) {
280 $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
281 $window.location.reload();
287 notificationService.addAdminNotification(this.newUserNotification)
290 $log.debug('notificationService:addAdminNotification:: Admin notification creation succeeded!,',res);
291 if(res.status=='ERROR'){
292 confirmBoxService.showInformation('There was a problem adding the notification. ' +
293 ' Error: ' + res.response).then(isConfirmed => { });
298 //$scope.closeThisDialog(true);
299 $scope.$dismiss('cancel');
304 switch (err.status) {
305 case '409': // Conflict
306 // handleConflictErrors(err);
308 case '500': // Internal Server Error
309 confirmBoxService.showInformation('There was a problem adding the notification. ' +
310 'Please try again later. Error: ' + err.status).then(isConfirmed => { });
313 confirmBoxService.showInformation('There was a problem adding the notification. ' +
314 'Please try again. If the problem persists, then try again later. Error: ' +
315 err.status).then(isConfirmed => { });
317 $log.error('notificationService:addAdminNotification error:: ' + JSON.stringify(err));
323 $log.warn('please fill in all required fields');
324 confirmBoxService.showInformation('Please fill in all required fields').then(isConfirmed => { });
327 $scope.functionalMenuRes={};
328 $scope.checkTreeSelect = function(){
330 for(var fi=0; fi<$scope.treedata.length;fi++){
331 var fLevel = $scope.treedata[fi];
332 if(fLevel.isSelected){
335 var sLevel = fLevel.child;
337 for(var si=0;si<sLevel.length;si++){
338 if(sLevel[si].isSelected){
341 var tLevel = sLevel[si].child;
343 for(var ti=0;ti<tLevel.length;ti++){
344 if(tLevel[ti].isSelected){
355 // Populate the category list for category dropdown list
356 let getFunctionalMenu = () => {
357 this.isLoadingTable = true;
359 $log.debug('getFunctionalMenu:init');
361 functionalMenuService.getFunctionalMenuRole().then(role_res => {
362 var menu_role_dict = {};
364 $log.debug('functionalMenuService:getFunctionalMenuRole:: getting result', role_res);
366 for (var i in role_res) {
367 // if first time appear in menu_role_dict
368 if (!(role_res[i].menuId in menu_role_dict)) {
369 menu_role_dict[role_res[i].menuId] = [role_res[i].roleId];
371 menu_role_dict[role_res[i].menuId].push(role_res[i].roleId);
375 functionalMenuService.getManagedFunctionalMenuForNotificationTree().then(res => {
377 $log.debug('functionalMenuService:getManagedFunctionalMenuForNotificationTree:: getting result', res);
378 var exclude_list = ['Favorites'];
380 $scope.functionalMenuRes=res;
382 //Adding children and label attribute to all objects in res
383 for(let i = 0; i < res.length; i++){
385 res[i].name=res[i].text;
386 res[i].id=res[i].text;
387 res[i].displayCheckbox= true;
388 $scope.checkBoxObj = {isAnyRoleSelected:false};
389 res[i].roleId = menu_role_dict[res[i].menuId];
390 res[i].onSelect = function () {
391 $scope.$apply(function () {
392 $scope.newNotifModel.anyTreeItemSelected=$scope.checkTreeSelect();
396 if (res[i].roleId && res[i].roleId.length==_.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length){
397 res[i].isSelected= true;
398 res[i].selected= true;
399 res[i].indeterminate= false;
402 res[i].isSelected= false;
403 res[i].selected= false;
404 res[i].indeterminate= false;
408 // Adding actual child items to children array in res
410 $scope.parentChildDict ={};
411 $scope.parentChildRoleIdDict ={};
412 for (let i = 0; i < res.length; i++) {
413 let parentId = res[i].menuId;
414 $scope.parentChildDict[parentId] = [];
415 $scope.parentChildRoleIdDict[parentId]=[];
416 for (let j = 0; j < res.length; j++) {
417 let childId = res[j].parentMenuId;
418 if (parentId === childId) {
419 res[i].child.push(res[j]);
420 $scope.parentChildDict[parentId].push(res[j].menuId);
421 //if res[j].roleId is defined
423 for (let k in res[j].roleId) {
424 $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
431 //check if grand children exist
432 for (var key in $scope.parentChildDict){
433 var child = $scope.parentChildDict[key];
434 var isGrandParent = false;
435 if (child.length>0) {
436 for (var i in child) {
437 if ($scope.parentChildDict[child[i]].length>0){
438 isGrandParent = true;
444 for (var i in child) {
445 // if the child has children
446 if ($scope.parentChildDict[child[i]].length>0) {
447 for (var j in $scope.parentChildRoleIdDict[child[i]]) {
448 if ($scope.parentChildRoleIdDict[key].indexOf($scope.parentChildRoleIdDict[child[i]][j]) === -1) {
449 $scope.parentChildRoleIdDict[key].push($scope.parentChildRoleIdDict[child[i]][j]);
458 // Sort the top-level menu items in order based on the column
459 res.sort(function(a, b) {
460 return a.column-b.column;
463 // Sort all the child in order based on the column
464 for(let i = 0; i < res.length; i++){
465 res[i].child.sort(function(a, b){
466 return a.column-b.column;
470 //Forming actual parent items
471 for(let i = 0; i < res.length; i++){
472 let parentId=res[i].parentMenuId;
474 actualData.push(res[i]);
477 var treedata = actualData[0].child;
478 $scope.treedata = [];
480 /*Remove favorite from the list */
481 for (var i in treedata) {
482 if (!(treedata[i].name.indexOf(exclude_list) > -1)) {
483 $scope.treedata.push(treedata[i])
486 //setting b2b tree parameter
487 $scope.settingTreeParam();
490 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
492 this.isLoadingTable = false;
496 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
502 let getAppRoleIds = () => {
503 $scope.notifObj= {isCategoriesFunctionalMenu:false};
504 notificationService.getAppRoleIds().then(res => {
506 $log.debug('notificationService:getAppRoleIds:: getting result', res);
509 var app_id_name_list = {};
510 $scope.checkBoxObj = {isAnyRoleSelected:false};
511 for (let i = 0; i < res.length; i++) {
512 if (!(res[i].appId in app_id_name_list)) {
513 app_id_name_list[res[i].appId] = res[i].appName;
516 res[i].name=res[i].roleName;
517 res[i].displayCheckbox= true;
518 res[i].id = res[i].roleId;
519 res[i].menuId = res[i].roleId;
520 res[i].parentMenuId = res[i].appId;
521 res[i].can_check = true;
522 res[i].roleId = [res[i].roleId];
523 res[i].onSelect = function () {
524 $scope.$apply(function () {
525 $scope.newNotifModel.anyTreeItemSelected=$scope.checkTreeSelect();
528 /*assigning selected value*/
529 if (res[i].roleId && res[i].roleId.length==_.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length){
530 res[i].isSelected= true;
531 res[i].selected= true;
532 res[i].indeterminate= false;
535 res[i].isSelected= false;
536 res[i].selected= false;
537 res[i].indeterminate= false;
541 for (var app_id in app_id_name_list) {
544 new_res.name = app_id_name_list[app_id];
546 new_res.displayCheckbox= true;
547 new_res.menuId = app_id;
548 new_res.parentMenuId = null;
549 new_res.appId = null;
550 new_res.can_check = true;
551 new_res.roleId = null;
552 new_res.onSelect = function () {
553 $scope.$apply(function () {
554 $scope.newNotifModel.anyTreeItemSelected=$scope.checkTreeSelect();
559 $scope.parentChildRoleIdDict ={};
560 //Adding actual child items to child array in res objects
561 for (let i = 0; i < res.length; i++) {
562 let parentId = res[i].menuId;
563 $scope.parentChildRoleIdDict[parentId]=[];
564 for (let j = 0; j < res.length; j++) {
565 let childId = res[j].parentMenuId;
566 if (parentId == childId) {
567 res[i].child.push(res[j]);
569 for (let k in res[j].roleId) {
570 $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
577 //Forming actual parent items
578 for (let i = 0; i < res.length; i++) {
579 let parentId = res[i].parentMenuId;
580 if (parentId === null) {
581 actualData.push(res[i]);
585 $scope.treedata = actualData;
586 //setting correct parameters for b2b tree
587 $scope.settingTreeParam();
589 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
591 this.isLoadingTable = false;
594 $scope.getFunctionalMenu= function() {
595 $scope.treeTitle="Functional Menu";
598 $scope.getAppRoleIds = function() {
599 $scope.treeTitle="Applications/Roles";
605 $scope.settingTreeParam = function(){
606 /**************first level****************/
607 for(var fi=0; fi<$scope.treedata.length;fi++){
608 var fLevel = $scope.treedata[fi];
609 var sLevel = $scope.treedata[fi].child;
610 var sLevelSelectedCount =0;
611 var sLevelChildNumber =0
612 if(fLevel.child.length==0 && fLevel.roleId==null){
615 /**************Second level****************/
616 var sLevelDelArray=[];
617 for(var si=0;si<sLevel.length;si++){
618 var deletThisSLev= false;
619 if(sLevel[si].child.length==0 && sLevel[si].roleId==null){
620 sLevel[si].displayCheckbox=false;
621 sLevelDelArray.push(sLevel[si].name);
622 sLevel[si].name = '';
623 sLevel[si].active=false;
624 delete sLevel[si].child;
625 } else if(sLevel[si].child.length==0){
626 delete sLevel[si].child;
628 /**************Third level****************/
629 var tLevel = sLevel[si].child;
630 var tLevelSelectedCount =0;
631 var tLevelChildNumber =0;
633 var tLevelDelArray=[];
634 var tLevelLen = tLevel.length;
635 var tLevelRoleIdUndefined =0;
636 for(var ti=0;ti<tLevel.length;ti++){
637 delete tLevel[ti].child;
638 if(tLevel[ti].roleId==null){
639 tLevel[ti].displayCheckbox=false;
640 tLevelDelArray.push(tLevel[ti].name);
641 tLevel[ti].name = '';
642 tLevel[ti].active=false;
643 tLevelRoleIdUndefined++
645 if(tLevel[ti].isSelected)
646 tLevelSelectedCount++;
648 if(tLevel[ti].displayCheckbox)
652 if(tLevelRoleIdUndefined==tLevelLen)
654 if(tLevelSelectedCount==tLevelChildNumber){
655 sLevel[si].isSelected=true;
656 sLevel[si].indeterminate=false;
657 sLevel[si].active=true;
658 }else if(tLevelSelectedCount>0){
659 sLevel[si].indeterminate=true;
660 sLevel[si].active=true;
663 /*Cleanup unused third level items*/
664 for(var i=0;i<tLevelDelArray.length;i++){
665 var name = tLevelDelArray[i];
666 for(var ti=0;ti<tLevel.length;ti++){
667 if(name==tLevel[ti].text){
675 if(deletThisSLev){ //remove the whole second level item if all it's child has no roleId
676 sLevel[si].displayCheckbox=false;
677 sLevelDelArray.push(sLevel[si].name);
678 sLevel[si].name = '';
679 sLevel[si].active=false;
681 if(sLevel[si].isSelected)
682 sLevelSelectedCount++;
683 if(sLevel[si].displayCheckbox)
687 if(sLevelSelectedCount==sLevelChildNumber && sLevelChildNumber!=0){
688 fLevel.isSelected=true;
689 fLevel.indeterminate=false;
691 }else if(sLevelSelectedCount>0){
692 fLevel.indeterminate=true;
695 //fLevel.active=false;
696 fLevel.indeterminate=false;
698 /*Cleanup unused second level items*/
699 for(var i=0;i<sLevelDelArray.length;i++){
700 var name = sLevelDelArray[i];
701 for(var si=0;si<sLevel.length;si++){
702 if(name==sLevel[si].text){
718 userNotificationsModalCtrl.$inject = ['$scope', '$log', 'functionalMenuService', 'confirmBoxService', 'notificationService', '$modal', 'ngDialog', '$state', '$filter','items'];
719 angular.module('ecompApp').controller('userNotificationsModalCtrl', userNotificationsModalCtrl);
721 angular.module('ecompApp').directive('attDatepickerCustom', ['$log', function($log) {
727 controller: ['$scope', '$element', '$attrs', '$compile', 'datepickerConfig', 'datepickerService', function($scope, $element, $attrs, $compile, datepickerConfig, datepickerService) {
728 var dateFormatString = angular.isDefined($attrs.dateFormat) ? $scope.$parent.$eval($attrs.dateFormat) : datepickerConfig.dateFormat;
729 var selectedDateMessage = '<div class="sr-focus hidden-spoken" tabindex="-1">the date you selected is {{$parent.current | date : \'' + dateFormatString + '\'}}</div>';
730 $element.removeAttr('att-datepicker-custom');
731 $element.removeAttr('ng-model');
732 $element.attr('ng-value', '$parent.current | date:"EEEE, MMMM d, y"');
733 $element.attr('aria-describedby', 'datepicker');
735 $element.attr('maxlength', 10);
737 var wrapperElement = angular.element('<div></div>');
738 wrapperElement.attr('datepicker-popup', '');
739 wrapperElement.attr('current', 'current');
741 datepickerService.setAttributes($attrs, wrapperElement);
742 datepickerService.bindScope($attrs, $scope);
744 wrapperElement.html('');
745 wrapperElement.append($element.prop('outerHTML'));
746 if (navigator.userAgent.match(/MSIE 8/) === null) {
747 wrapperElement.append(selectedDateMessage);
749 var elm = wrapperElement.prop('outerHTML');
750 elm = $compile(elm)($scope);
751 $element.replaceWith(elm);
753 link: function(scope, elem, attr, ctrl) {
755 // do nothing if no ng-model
756 $log.error("ng-model is required.");
760 scope.$watch('current', function(value) {
761 ctrl.$setViewValue(value);
763 ctrl.$render = function() {
764 scope.current = ctrl.$viewValue;