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.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 $scope.isDateValid = function(time) {
217 if (time == undefined) {
220 if (typeof time == 'object') {
223 var startDateformat = time.split('/');
224 if (startDateformat.length != 3) return false;
225 var day = startDateformat[1];
226 var month = startDateformat[0];
227 month = parseInt(month) - 1;
228 var year = startDateformat[2];
229 if (year.length != 4) return false;
230 var composedDate = new Date(year, month, day);
231 return composedDate.getDate() == day &&
232 composedDate.getMonth() == month &&
233 composedDate.getFullYear() == year;
238 $scope.addUserNotification = function() {
239 $scope.notificationRoleIds = [];
242 for (var fi = 0; fi < $scope.treedata.length; fi++) {
243 var fLevel = $scope.treedata[fi];
245 var fLevelChild = fLevel.child;
247 for (var si = 0; si < fLevelChild.length; si++) {
248 var sLevel = fLevelChild[si];
250 var sLevelChild = sLevel.child;
252 for (var ti = 0; ti < sLevelChild.length; ti++) {
253 var tLevel = sLevelChild[ti];
254 if (tLevel.isSelected && tLevel.roleId) {
255 $scope.newNotifModel.anyTreeItemSelected = true;
256 for (var i in tLevel.roleId)
257 $scope.notificationRoleIds.push(tLevel.roleId[i]);
262 if (sLevel.isSelected && sLevel.roleId) {
263 for (var i in sLevel.roleId)
264 $scope.notificationRoleIds.push(sLevel.roleId[i]);
271 $scope.notificationRoleIds.sort();
272 if (($scope.newNotifModel.isOnlineUsersOnly) && ($scope.newNotifModel.isForAllRoles) && ($scope.newNotifModel.selectedPriority) && ($scope.newNotifModel.isActive) &&
273 ($scope.newNotifModel.startTime) && ($scope.newNotifModel.endTime) && ($scope.newNotifModel.msgHeader != '') && ($scope.newNotifModel.msgDescription != '')) {
274 this.newUserNotification = {
275 'notificationId': $scope.notificationId,
276 'isForOnlineUsers': $scope.newNotifModel.isOnlineUsersOnly.value,
277 'isForAllRoles': $scope.newNotifModel.isForAllRoles,
278 'priority': $scope.newNotifModel.selectedPriority,
279 'activeYn': $scope.newNotifModel.isActive.value,
280 'startTime': $scope.newNotifModel.startTime,
281 'endTime': $scope.newNotifModel.endTime,
282 'msgHeader': $scope.newNotifModel.msgHeader,
283 'msgDescription': $scope.newNotifModel.msgDescription,
284 'roleIds': $scope.notificationRoleIds,
285 'createdDate': new Date()
288 // POST ajax call here;
289 if ($scope.isEditMode) {
290 notificationService.updateAdminNotification(this.newUserNotification)
293 $log.debug('NotificationService:updateAdminNotification:: Admin notification update succeeded!');
294 $scope.closeThisDialog(true);
296 $log.error('notificationService.updateAdminNotfication failed: ' + JSON.stringify(err));
297 switch (err.status) {
298 case '409': // Conflict
299 // handleConflictErrors(err);
301 case '500': // Internal Server Error
302 confirmBoxService.showInformation('There was a problem updating the notification. ' +
303 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
305 case '403': // Forbidden... possible
306 // webjunction error to
308 confirmBoxService.showInformation('There was a problem updating the notification. ' +
309 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
312 confirmBoxService.showInformation('There was a problem updating the notification. ' +
313 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
317 var objOffsetVersion = objAgent.indexOf("MSIE");
318 if (objOffsetVersion != -1) {
320 $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
321 $window.location.reload();
327 notificationService.addAdminNotification(this.newUserNotification)
330 $log.debug('notificationService:addAdminNotification:: Admin notification creation succeeded!,', res);
331 if (res.status == 'ERROR') {
332 confirmBoxService.showInformation('There was a problem adding the notification. ' +
333 ' Error: ' + res.response).then(isConfirmed => {});
337 // $scope.closeThisDialog(true);
338 $scope.$dismiss('cancel');
343 switch (err.status) {
344 case '409': // Conflict
345 // handleConflictErrors(err);
347 case '500': // Internal Server Error
348 confirmBoxService.showInformation('There was a problem adding the notification. ' +
349 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
352 confirmBoxService.showInformation('There was a problem adding the notification. ' +
353 'Please try again. If the problem persists, then try again later. Error: ' +
354 err.status).then(isConfirmed => {});
356 $log.error('notificationService:addAdminNotification error:: ' + JSON.stringify(err));
362 $log.warn('please fill in all required fields');
363 confirmBoxService.showInformation('Please fill in all required fields').then(isConfirmed => {});
366 $scope.functionalMenuRes = {};
367 $scope.checkTreeSelect = function() {
368 if ($scope.treedata) {
369 for (var fi = 0; fi < $scope.treedata.length; fi++) {
370 var fLevel = $scope.treedata[fi];
371 if (fLevel.isSelected) {
374 var sLevel = fLevel.child;
376 for (var si = 0; si < sLevel.length; si++) {
377 if (sLevel[si].isSelected) {
380 var tLevel = sLevel[si].child;
382 for (var ti = 0; ti < tLevel.length; ti++) {
383 if (tLevel[ti].isSelected) {
394 // Populate the category list for category dropdown list
395 let getFunctionalMenu = () => {
396 this.isLoadingTable = true;
398 $log.debug('getFunctionalMenu:init');
400 functionalMenuService.getFunctionalMenuRole().then(role_res => {
401 var menu_role_dict = {};
403 $log.debug('functionalMenuService:getFunctionalMenuRole:: getting result', role_res);
405 for (var i in role_res) {
406 // if first time appear in menu_role_dict
407 if (!(role_res[i].menuId in menu_role_dict)) {
408 menu_role_dict[role_res[i].menuId] = [role_res[i].roleId];
410 menu_role_dict[role_res[i].menuId].push(role_res[i].roleId);
414 functionalMenuService.getManagedFunctionalMenuForNotificationTree().then(res => {
416 $log.debug('functionalMenuService:getManagedFunctionalMenuForNotificationTree:: getting result', res);
417 var exclude_list = ['Favorites'];
419 $scope.functionalMenuRes = res;
421 //Adding children and label attribute to all objects in res
422 for (let i = 0; i < res.length; i++) {
424 res[i].name = res[i].text;
425 res[i].id = res[i].text;
426 res[i].displayCheckbox = true;
427 $scope.checkBoxObj = {
428 isAnyRoleSelected: false
430 res[i].roleId = menu_role_dict[res[i].menuId];
431 res[i].onSelect = function() {
432 $scope.$apply(function() {
433 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
437 if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
438 res[i].isSelected = true;
439 res[i].selected = true;
440 res[i].indeterminate = false;
443 res[i].isSelected = false;
444 res[i].selected = false;
445 res[i].indeterminate = false;
449 // Adding actual child items to children array in res
451 $scope.parentChildDict = {};
452 $scope.parentChildRoleIdDict = {};
453 for (let i = 0; i < res.length; i++) {
454 let parentId = res[i].menuId;
455 $scope.parentChildDict[parentId] = [];
456 $scope.parentChildRoleIdDict[parentId] = [];
457 for (let j = 0; j < res.length; j++) {
458 let childId = res[j].parentMenuId;
459 if (parentId === childId) {
460 res[i].child.push(res[j]);
461 $scope.parentChildDict[parentId].push(res[j].menuId);
462 //if res[j].roleId is defined
464 for (let k in res[j].roleId) {
465 $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
472 //check if grand children exist
473 for (var key in $scope.parentChildDict) {
474 var child = $scope.parentChildDict[key];
475 var isGrandParent = false;
476 if (child.length > 0) {
477 for (var i in child) {
478 if ($scope.parentChildDict[child[i]].length > 0) {
479 isGrandParent = true;
485 for (var i in child) {
486 // if the child has children
487 if ($scope.parentChildDict[child[i]].length > 0) {
488 for (var j in $scope.parentChildRoleIdDict[child[i]]) {
489 if ($scope.parentChildRoleIdDict[key].indexOf($scope.parentChildRoleIdDict[child[i]][j]) === -1) {
490 $scope.parentChildRoleIdDict[key].push($scope.parentChildRoleIdDict[child[i]][j]);
499 // Sort the top-level menu items in order based on the column
500 res.sort(function(a, b) {
501 return a.column - b.column;
504 // Sort all the child in order based on the column
505 for (let i = 0; i < res.length; i++) {
506 res[i].child.sort(function(a, b) {
507 return a.column - b.column;
511 //Forming actual parent items
512 for (let i = 0; i < res.length; i++) {
513 let parentId = res[i].parentMenuId;
514 if (parentId === null) {
515 actualData.push(res[i]);
518 var treedata = actualData[0].child;
519 $scope.treedata = [];
521 /*Remove favorite from the list */
522 for (var i in treedata) {
523 if (!(treedata[i].name.indexOf(exclude_list) > -1)) {
524 $scope.treedata.push(treedata[i])
527 //setting b2b tree parameter
528 $scope.settingTreeParam();
531 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
533 this.isLoadingTable = false;
537 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
542 let getAppRoleIds = () => {
544 isCategoriesFunctionalMenu: false
546 notificationService.getAppRoleIds().then(res => {
548 $log.debug('notificationService:getAppRoleIds:: getting result', res);
551 var app_id_name_list = {};
552 $scope.checkBoxObj = {
553 isAnyRoleSelected: false
555 for (let i = 0; i < res.length; i++) {
556 if (!(res[i].appId in app_id_name_list)) {
557 app_id_name_list[res[i].appId] = res[i].appName;
560 res[i].name = res[i].roleName;
561 res[i].displayCheckbox = true;
562 res[i].id = res[i].roleId;
563 res[i].menuId = res[i].roleId;
564 res[i].parentMenuId = res[i].appId;
565 res[i].can_check = true;
566 res[i].roleId = [res[i].roleId];
567 res[i].onSelect = function() {
568 $scope.$apply(function() {
569 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
572 /*assigning selected value*/
573 if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
574 res[i].isSelected = true;
575 res[i].selected = true;
576 res[i].indeterminate = false;
579 res[i].isSelected = false;
580 res[i].selected = false;
581 res[i].indeterminate = false;
585 for (var app_id in app_id_name_list) {
588 new_res.name = app_id_name_list[app_id];
590 new_res.displayCheckbox = true;
591 new_res.menuId = app_id;
592 new_res.parentMenuId = null;
593 new_res.appId = null;
594 new_res.can_check = true;
595 new_res.roleId = null;
596 new_res.onSelect = function() {
597 $scope.$apply(function() {
598 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
603 $scope.parentChildRoleIdDict = {};
604 //Adding actual child items to child array in res objects
605 for (let i = 0; i < res.length; i++) {
606 let parentId = res[i].menuId;
607 $scope.parentChildRoleIdDict[parentId] = [];
608 for (let j = 0; j < res.length; j++) {
609 let childId = res[j].parentMenuId;
610 if (parentId == childId) {
611 res[i].child.push(res[j]);
613 for (let k in res[j].roleId) {
614 $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
621 //Forming actual parent items
622 for (let i = 0; i < res.length; i++) {
623 let parentId = res[i].parentMenuId;
624 if (parentId === null) {
625 actualData.push(res[i]);
629 $scope.treedata = actualData;
630 //setting correct parameters for b2b tree
631 $scope.settingTreeParam();
633 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
635 this.isLoadingTable = false;
638 $scope.getFunctionalMenu = function() {
639 $scope.treeTitle = "Functional Menu";
642 $scope.getAppRoleIds = function() {
643 $scope.treeTitle = "Applications/Roles";
649 $scope.settingTreeParam = function() {
650 /**************first level****************/
651 for (var fi = 0; fi < $scope.treedata.length; fi++) {
652 var fLevel = $scope.treedata[fi];
653 var sLevel = $scope.treedata[fi].child;
654 var sLevelSelectedCount = 0;
655 var sLevelChildNumber = 0
656 if (fLevel.child.length == 0 && fLevel.roleId == null) {
659 /**************Second level****************/
660 var sLevelDelArray = [];
661 for (var si = 0; si < sLevel.length; si++) {
662 var deletThisSLev = false;
663 if (sLevel[si].child.length == 0 && sLevel[si].roleId == null) {
664 sLevel[si].displayCheckbox = false;
665 sLevelDelArray.push(sLevel[si].name);
666 sLevel[si].name = '';
667 sLevel[si].active = false;
668 delete sLevel[si].child;
669 } else if (sLevel[si].child.length == 0) {
670 delete sLevel[si].child;
672 /**************Third level****************/
673 var tLevel = sLevel[si].child;
674 var tLevelSelectedCount = 0;
675 var tLevelChildNumber = 0;
677 var tLevelDelArray = [];
678 var tLevelLen = tLevel.length;
679 var tLevelRoleIdUndefined = 0;
680 for (var ti = 0; ti < tLevel.length; ti++) {
681 delete tLevel[ti].child;
682 if (tLevel[ti].roleId == null) {
683 tLevel[ti].displayCheckbox = false;
684 tLevelDelArray.push(tLevel[ti].name);
685 tLevel[ti].name = '';
686 tLevel[ti].active = false;
687 tLevelRoleIdUndefined++
689 if (tLevel[ti].isSelected)
690 tLevelSelectedCount++;
692 if (tLevel[ti].displayCheckbox)
696 if (tLevelRoleIdUndefined == tLevelLen)
697 deletThisSLev = true;
698 if (tLevelSelectedCount == tLevelChildNumber) {
699 sLevel[si].isSelected = true;
700 sLevel[si].indeterminate = false;
701 sLevel[si].active = true;
702 } else if (tLevelSelectedCount > 0) {
703 sLevel[si].indeterminate = true;
704 sLevel[si].active = true;
707 /*Cleanup unused third level items*/
708 for (var i = 0; i < tLevelDelArray.length; i++) {
709 var name = tLevelDelArray[i];
710 for (var ti = 0; ti < tLevel.length; ti++) {
711 if (name == tLevel[ti].text) {
712 tLevel.splice(ti, 1);
719 if (deletThisSLev) { //remove the whole second level item if all it's child has no roleId
720 sLevel[si].displayCheckbox = false;
721 sLevelDelArray.push(sLevel[si].name);
722 sLevel[si].name = '';
723 sLevel[si].active = false;
725 if (sLevel[si].isSelected)
726 sLevelSelectedCount++;
727 if (sLevel[si].displayCheckbox)
731 if (sLevelSelectedCount == sLevelChildNumber && sLevelChildNumber != 0) {
732 fLevel.isSelected = true;
733 fLevel.indeterminate = false;
734 fLevel.active = true;
735 } else if (sLevelSelectedCount > 0) {
736 fLevel.indeterminate = true;
737 fLevel.active = true;
739 //fLevel.active=false;
740 fLevel.indeterminate = false;
742 /*Cleanup unused second level items*/
743 for (var i = 0; i < sLevelDelArray.length; i++) {
744 var name = sLevelDelArray[i];
745 for (var si = 0; si < sLevel.length; si++) {
746 if (name == sLevel[si].text) {
747 sLevel.splice(si, 1);
762 userNotificationsModalCtrl.$inject = ['$scope', '$log', 'functionalMenuService', 'confirmBoxService', 'notificationService', '$modal', 'ngDialog', '$state', '$filter', 'items'];
763 angular.module('ecompApp').controller('userNotificationsModalCtrl', userNotificationsModalCtrl);
765 angular.module('ecompApp').directive('attDatepickerCustom', ['$log', function($log) {
771 controller: ['$scope', '$element', '$attrs', '$compile', 'datepickerConfig', 'datepickerService', function($scope, $element, $attrs, $compile, datepickerConfig, datepickerService) {
772 var dateFormatString = angular.isDefined($attrs.dateFormat) ? $scope.$parent.$eval($attrs.dateFormat) : datepickerConfig.dateFormat;
773 var selectedDateMessage = '<div class="sr-focus hidden-spoken" tabindex="-1">the date you selected is {{$parent.current | date : \'' + dateFormatString + '\'}}</div>';
774 $element.removeAttr('att-datepicker-custom');
775 $element.removeAttr('ng-model');
776 $element.attr('ng-value', '$parent.current | date:"EEEE, MMMM d, y"');
777 $element.attr('aria-describedby', 'datepicker');
779 $element.attr('maxlength', 10);
781 var wrapperElement = angular.element('<div></div>');
782 wrapperElement.attr('datepicker-popup', '');
783 wrapperElement.attr('current', 'current');
785 datepickerService.setAttributes($attrs, wrapperElement);
786 datepickerService.bindScope($attrs, $scope);
788 wrapperElement.html('');
789 wrapperElement.append($element.prop('outerHTML'));
790 if (navigator.userAgent.match(/MSIE 8/) === null) {
791 wrapperElement.append(selectedDateMessage);
793 var elm = wrapperElement.prop('outerHTML');
794 elm = $compile(elm)($scope);
795 $element.replaceWith(elm);
797 link: function(scope, elem, attr, ctrl) {
799 // do nothing if no ng-model
800 $log.error("ng-model is required.");
804 scope.$watch('current', function(value) {
805 ctrl.$setViewValue(value);
807 ctrl.$render = function() {
808 scope.current = ctrl.$viewValue;