2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
42 class userNotificationsModalCtrl {
43 constructor($scope, $log, functionalMenuService, confirmBoxService, notificationService, $modal, ngDialog, $state, $filter, items) {
45 $scope.newNotifModel = {
46 'isOnlineUsersOnly': null,
47 'isForAllRolesOptions': null,
48 'selectedPriority': null,
53 'msgDescription': null,
55 'anyTreeItemSelected': false,
57 notificationRoleIds: null
61 $scope.notificationId = null;
62 $scope.selectedCat = null;
63 $scope.selectedEcompFunc = null;
64 this.YN_index_mapping = {
69 $scope.onlineAllUsersOptions = [{
72 "title": "Online Users Only"
77 "title": "Online & Offline Users"
82 $scope.isForAllRolesOptions = [{
94 $scope.priorityOptions = [{
106 $scope.isActiveOptions = [{
117 $scope.newNotifModel.isActive = $scope.isActiveOptions[0];
118 $scope.newNotifModel.selectPriority = $scope.priorityOptions[0];
119 $scope.newNotifModel.isOnlineUsersOnly = $scope.onlineAllUsersOptions[1];
120 $scope.newNotifModel.isForAllRoles = $scope.isForAllRolesOptions[0].value;
121 $scope.newNotifModel.isFunctionalMenu = "Y";
123 $scope.newNotifModel.selectedPriority = $scope.priorityOptions[0].value;
124 $scope.newNotifModel.msgHeader = '';
125 $scope.newNotifModel.msgDescription = '';
126 $scope.newNotifModel.treeTitle = "Functional Menu";
127 $scope.newNotifModel.notifObj = {
128 isCategoriesFunctionalMenu: true
133 $log.debug('userNotificationsModalCtrl::init');
134 this.isSaving = false;
135 var today = new Date();
136 $scope.minDate = today.toISOString().substring(0, 10);
137 var threeMonthsFromNow = new Date();
138 threeMonthsFromNow.setMonth(threeMonthsFromNow.getMonth() + 3);
139 $scope.maxDate = threeMonthsFromNow.toISOString().substring(0, 10);
140 if (items && items.notif) {
142 $log.debug('userNotificationsModalCtrl:init:: Edit notification mode for', items.notif);
143 $scope.isEditMode = true;
144 $scope.editModeObj = {
147 this.notif = _.clone(items.notif);
148 $scope.modalPgTitle = 'View Notification'
149 $scope.newNotifModel.isOnlineUsersOnly = $scope.onlineAllUsersOptions[this.YN_index_mapping[this.notif.isForOnlineUsers]];
150 $scope.newNotifModel.isForAllRoles = $scope.isForAllRolesOptions[this.YN_index_mapping[this.notif.isForAllRoles]].value;
151 $scope.newNotifModel.isActive = $scope.isActiveOptions[this.YN_index_mapping[this.notif.activeYn]];
152 $scope.newNotifModel.selectedPriority = $scope.priorityOptions[this.notif.priority - 1].value;
153 $scope.newNotifModel.startTime = new Date(this.notif.startTime);
154 $scope.newNotifModel.endTime = new Date(this.notif.endTime);
155 $scope.newNotifModel.msgHeader = this.notif.msgHeader;
156 $scope.newNotifModel.msgDescription = this.notif.msgDescription;
157 $scope.notificationId = this.notif.notificationId;
158 $scope.newNotifModel.notificationRoleIds = this.notif.roleIds;
160 notificationRoleIds: this.notif.roleIds
164 $log.debug('AppDetailsModalCtrl:init:: New app mode');
165 $scope.isEditMode = false;
166 $scope.editModeObj = {
169 $scope.modalPgTitle = 'Add a New Notification'
170 this.notif = _.clone($scope.newNotifModel);
172 notificationRoleIds: null
176 this.conflictMessages = {};
178 let handleConflictErrors = err => {
182 if (!err.data.length) { // support objects
183 err.data = [err.data]
185 _.forEach(err.data, item => {
186 _.forEach(item.fields, field => {
187 // set conflict message
188 this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
189 // set field as invalid
190 $scope.appForm[field.name].$setValidity('conflict', false);
191 // set watch once to clear error after user correction
192 watchOnce[field.name]();
195 this.scrollApi.scrollTop();
198 let resetConflict = fieldName => {
199 delete this.conflictMessages[fieldName];
200 if ($scope.appForm[fieldName]) {
201 $scope.appForm[fieldName].$setValidity('conflict', true);
204 $scope.addUserNotificationValidation = function() {
206 if (!($scope.isEditMode)) {
207 var validation = false;
208 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)) {
210 if ($scope.newNotifModel.isForAllRoles == 'N') {
211 validation = $scope.checkTreeSelect();
220 /* format the value for viewing a notification */
221 $scope.formatStartDate = function() {
222 if ($scope.newNotifModel.startTime) {
223 $scope.newNotifModel.startTime = $filter('date')($scope.startTime, 'medium');
227 /* format the value for viewing a notification */
228 $scope.formatEndDate = function() {
229 if ($scope.newNotifModel.endTime) {
230 $scope.newNotifModel.endTime = $filter('date')($scope.endTime, 'medium');
234 /*To validate the manual entry of date in MM/DD/YYYY Format*/
236 $scope.isDateValid = function(time) {
237 if (time == undefined) {
240 if (typeof time == 'object') {
243 var startDateformat = time.split('/');
244 if (startDateformat.length != 3) return false;
245 var day = startDateformat[1];
246 var month = startDateformat[0];
247 month = parseInt(month) - 1;
248 var year = startDateformat[2];
249 if (year.length != 4) return false;
250 var composedDate = new Date(year, month, day);
251 return composedDate.getDate() == day &&
252 composedDate.getMonth() == month &&
253 composedDate.getFullYear() == year;
256 /*The manual and drop down calendar should be consistent.
257 Start date must be greater than or equal to current date.The end dates are not allowed after the 3 months from current dates*/
259 $scope.isStartDateValidFromToday = function (time) {
260 if(time == undefined){
263 if(typeof time == 'object'){
266 var startDateformat =time.split('/');
267 if (startDateformat.length != 3) return true;
268 var day = startDateformat[1];
269 var month = startDateformat[0];
270 month= parseInt(month)-1;
271 var year = startDateformat[2];
272 if(year.length!=4) return true;
273 var composedDate = new Date(year, month, day);
274 /* As end dates are not allowed after the 3 months from current dates*/
275 var x = 3; //or whatever offset
276 var CurrentDate = new Date();
277 /*If composed date is less than the current date,error message should display*/
278 if(composedDate<CurrentDate)
280 CurrentDate.setMonth(CurrentDate.getMonth() + x);
281 if(composedDate>CurrentDate)
287 $scope.addUserNotification = function() {
288 $scope.notificationRoleIds = [];
291 for (var fi = 0; fi < $scope.treedata.length; fi++) {
292 var fLevel = $scope.treedata[fi];
294 var fLevelChild = fLevel.child;
296 for (var si = 0; si < fLevelChild.length; si++) {
297 var sLevel = fLevelChild[si];
299 var sLevelChild = sLevel.child;
301 for (var ti = 0; ti < sLevelChild.length; ti++) {
302 var tLevel = sLevelChild[ti];
303 if (tLevel.isSelected && tLevel.roleId) {
304 $scope.newNotifModel.anyTreeItemSelected = true;
305 for (var i in tLevel.roleId)
306 $scope.notificationRoleIds.push(tLevel.roleId[i]);
311 if (sLevel.isSelected && sLevel.roleId) {
312 for (var i in sLevel.roleId)
313 $scope.notificationRoleIds.push(sLevel.roleId[i]);
320 $scope.notificationRoleIds.sort();
321 if (($scope.newNotifModel.isOnlineUsersOnly) && ($scope.newNotifModel.isForAllRoles) && ($scope.newNotifModel.selectedPriority) && ($scope.newNotifModel.isActive) &&
322 ($scope.newNotifModel.startTime) && ($scope.newNotifModel.endTime) && ($scope.newNotifModel.msgHeader != '') && ($scope.newNotifModel.msgDescription != '')) {
323 this.newUserNotification = {
324 'notificationId': $scope.notificationId,
325 'isForOnlineUsers': $scope.newNotifModel.isOnlineUsersOnly.value,
326 'isForAllRoles': $scope.newNotifModel.isForAllRoles,
327 'priority': $scope.newNotifModel.selectedPriority,
328 'activeYn': $scope.newNotifModel.isActive.value,
329 'startTime': $scope.newNotifModel.startTime,
330 'endTime': $scope.newNotifModel.endTime,
331 'msgHeader': $scope.newNotifModel.msgHeader,
332 'msgDescription': $scope.newNotifModel.msgDescription,
333 'roleIds': $scope.notificationRoleIds,
334 'createdDate': new Date()
337 // POST ajax call here;
338 if ($scope.isEditMode) {
339 notificationService.updateAdminNotification(this.newUserNotification)
342 $log.debug('NotificationService:updateAdminNotification:: Admin notification update succeeded!');
343 $scope.closeThisDialog(true);
345 $log.error('notificationService.updateAdminNotfication failed: ' + JSON.stringify(err));
346 switch (err.status) {
347 case '409': // Conflict
348 // handleConflictErrors(err);
350 case '500': // Internal Server Error
351 confirmBoxService.showInformation('There was a problem updating the notification. ' +
352 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
354 case '403': // Forbidden... possible
355 // webjunction error to
357 confirmBoxService.showInformation('There was a problem updating the notification. ' +
358 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
361 confirmBoxService.showInformation('There was a problem updating the notification. ' +
362 'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
366 var objOffsetVersion = objAgent.indexOf("MSIE");
367 if (objOffsetVersion != -1) {
369 $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
370 $window.location.reload();
376 notificationService.addAdminNotification(this.newUserNotification)
379 $log.debug('notificationService:addAdminNotification:: Admin notification creation succeeded!,', res);
380 if (res.status == 'ERROR') {
381 confirmBoxService.showInformation('There was a problem adding the notification. ' +
382 ' Error: ' + res.response).then(isConfirmed => {});
386 // $scope.closeThisDialog(true);
387 $scope.$dismiss('cancel');
392 switch (err.status) {
393 case '409': // Conflict
394 // handleConflictErrors(err);
396 case '500': // Internal Server Error
397 confirmBoxService.showInformation('There was a problem adding the notification. ' +
398 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
401 confirmBoxService.showInformation('There was a problem adding the notification. ' +
402 'Please try again. If the problem persists, then try again later. Error: ' +
403 err.status).then(isConfirmed => {});
405 $log.error('notificationService:addAdminNotification error:: ' + JSON.stringify(err));
411 $log.warn('please fill in all required fields');
412 confirmBoxService.showInformation('Please fill in all required fields').then(isConfirmed => {});
415 $scope.functionalMenuRes = {};
416 $scope.checkTreeSelect = function() {
417 if ($scope.treedata) {
418 for (var fi = 0; fi < $scope.treedata.length; fi++) {
419 var fLevel = $scope.treedata[fi];
420 if (fLevel.isSelected) {
423 var sLevel = fLevel.child;
425 for (var si = 0; si < sLevel.length; si++) {
426 if (sLevel[si].isSelected) {
429 var tLevel = sLevel[si].child;
431 for (var ti = 0; ti < tLevel.length; ti++) {
432 if (tLevel[ti].isSelected) {
443 // Populate the category list for category dropdown list
444 let getFunctionalMenu = () => {
445 this.isLoadingTable = true;
447 $log.debug('getFunctionalMenu:init');
449 functionalMenuService.getFunctionalMenuRole().then(role_res => {
450 var menu_role_dict = {};
452 $log.debug('functionalMenuService:getFunctionalMenuRole:: getting result', role_res);
454 for (var i in role_res) {
455 // if first time appear in menu_role_dict
456 if (!(role_res[i].menuId in menu_role_dict)) {
457 menu_role_dict[role_res[i].menuId] = [role_res[i].roleId];
459 menu_role_dict[role_res[i].menuId].push(role_res[i].roleId);
463 functionalMenuService.getManagedFunctionalMenuForNotificationTree().then(res => {
465 $log.debug('functionalMenuService:getManagedFunctionalMenuForNotificationTree:: getting result', res);
466 var exclude_list = ['Favorites'];
468 $scope.functionalMenuRes = res;
470 //Adding children and label attribute to all objects in res
471 for (let i = 0; i < res.length; i++) {
473 res[i].name = res[i].text;
474 res[i].id = res[i].text;
475 res[i].displayCheckbox = true;
476 $scope.checkBoxObj = {
477 isAnyRoleSelected: false
479 res[i].roleId = menu_role_dict[res[i].menuId];
480 res[i].onSelect = function() {
481 $scope.$apply(function() {
482 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
486 if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
487 res[i].isSelected = true;
488 res[i].selected = true;
489 res[i].indeterminate = false;
492 res[i].isSelected = false;
493 res[i].selected = false;
494 res[i].indeterminate = false;
498 // Adding actual child items to children array in res
500 $scope.parentChildDict = {};
501 $scope.parentChildRoleIdDict = {};
502 for (let i = 0; i < res.length; i++) {
503 let parentId = res[i].menuId;
504 $scope.parentChildDict[parentId] = [];
505 $scope.parentChildRoleIdDict[parentId] = [];
506 for (let j = 0; j < res.length; j++) {
507 let childId = res[j].parentMenuId;
508 if (parentId === childId) {
509 res[i].child.push(res[j]);
510 $scope.parentChildDict[parentId].push(res[j].menuId);
511 //if res[j].roleId is defined
513 for (let k in res[j].roleId) {
514 $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
521 //check if grand children exist
522 for (var key in $scope.parentChildDict) {
523 var child = $scope.parentChildDict[key];
524 var isGrandParent = false;
525 if (child.length > 0) {
526 for (var i in child) {
527 if ($scope.parentChildDict[child[i]].length > 0) {
528 isGrandParent = true;
534 for (var i in child) {
535 // if the child has children
536 if ($scope.parentChildDict[child[i]].length > 0) {
537 for (var j in $scope.parentChildRoleIdDict[child[i]]) {
538 if ($scope.parentChildRoleIdDict[key].indexOf($scope.parentChildRoleIdDict[child[i]][j]) === -1) {
539 $scope.parentChildRoleIdDict[key].push($scope.parentChildRoleIdDict[child[i]][j]);
548 // Sort the top-level menu items in order based on the column
549 res.sort(function(a, b) {
550 return a.column - b.column;
553 // Sort all the child in order based on the column
554 for (let i = 0; i < res.length; i++) {
555 res[i].child.sort(function(a, b) {
556 return a.column - b.column;
560 //Forming actual parent items
561 for (let i = 0; i < res.length; i++) {
562 let parentId = res[i].parentMenuId;
563 if (parentId === null) {
564 actualData.push(res[i]);
567 var treedata = actualData[0].child;
568 $scope.treedata = [];
570 /*Remove favorite from the list */
571 for (var i in treedata) {
572 if (!(treedata[i].name.indexOf(exclude_list) > -1)) {
573 $scope.treedata.push(treedata[i])
576 //setting b2b tree parameter
577 $scope.settingTreeParam();
580 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
582 this.isLoadingTable = false;
586 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
591 let getAppRoleIds = () => {
593 isCategoriesFunctionalMenu: false
595 notificationService.getAppRoleIds().then(res => {
597 $log.debug('notificationService:getAppRoleIds:: getting result', res);
600 var app_id_name_list = {};
601 $scope.checkBoxObj = {
602 isAnyRoleSelected: false
604 for (let i = 0; i < res.length; i++) {
605 if (!(res[i].appId in app_id_name_list)) {
606 app_id_name_list[res[i].appId] = res[i].appName;
609 res[i].name = res[i].roleName;
610 res[i].displayCheckbox = true;
611 res[i].id = res[i].roleId;
612 res[i].menuId = res[i].roleId;
613 res[i].parentMenuId = res[i].appId;
614 res[i].can_check = true;
615 res[i].roleId = [res[i].roleId];
616 res[i].onSelect = function() {
617 $scope.$apply(function() {
618 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
621 /*assigning selected value*/
622 if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
623 res[i].isSelected = true;
624 res[i].selected = true;
625 res[i].indeterminate = false;
628 res[i].isSelected = false;
629 res[i].selected = false;
630 res[i].indeterminate = false;
634 for (var app_id in app_id_name_list) {
637 new_res.name = app_id_name_list[app_id];
639 new_res.displayCheckbox = true;
640 new_res.menuId = app_id;
641 new_res.parentMenuId = null;
642 new_res.appId = null;
643 new_res.can_check = true;
644 new_res.roleId = null;
645 new_res.onSelect = function() {
646 $scope.$apply(function() {
647 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
652 $scope.parentChildRoleIdDict = {};
653 //Adding actual child items to child array in res objects
654 for (let i = 0; i < res.length; i++) {
655 let parentId = res[i].menuId;
656 $scope.parentChildRoleIdDict[parentId] = [];
657 for (let j = 0; j < res.length; j++) {
658 let childId = res[j].parentMenuId;
659 if (parentId == childId) {
660 res[i].child.push(res[j]);
662 for (let k in res[j].roleId) {
663 $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
670 //Forming actual parent items
671 for (let i = 0; i < res.length; i++) {
672 let parentId = res[i].parentMenuId;
673 if (parentId === null) {
674 actualData.push(res[i]);
678 $scope.treedata = actualData;
679 //setting correct parameters for b2b tree
680 $scope.settingTreeParam();
682 $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
684 this.isLoadingTable = false;
687 $scope.getFunctionalMenu = function() {
688 $scope.treeTitle = "Functional Menu";
691 $scope.getAppRoleIds = function() {
692 $scope.treeTitle = "Applications/Roles";
698 $scope.settingTreeParam = function() {
699 /**************first level****************/
700 for (var fi = 0; fi < $scope.treedata.length; fi++) {
701 var fLevel = $scope.treedata[fi];
702 var sLevel = $scope.treedata[fi].child;
703 var sLevelSelectedCount = 0;
704 var sLevelChildNumber = 0
705 if (fLevel.child.length == 0 && fLevel.roleId == null) {
708 /**************Second level****************/
709 var sLevelDelArray = [];
710 for (var si = 0; si < sLevel.length; si++) {
711 var deletThisSLev = false;
712 if (sLevel[si].child.length == 0 && sLevel[si].roleId == null) {
713 sLevel[si].displayCheckbox = false;
714 sLevelDelArray.push(sLevel[si].name);
715 sLevel[si].name = '';
716 sLevel[si].active = false;
717 delete sLevel[si].child;
718 } else if (sLevel[si].child.length == 0) {
719 delete sLevel[si].child;
721 /**************Third level****************/
722 var tLevel = sLevel[si].child;
723 var tLevelSelectedCount = 0;
724 var tLevelChildNumber = 0;
726 var tLevelDelArray = [];
727 var tLevelLen = tLevel.length;
728 var tLevelRoleIdUndefined = 0;
729 for (var ti = 0; ti < tLevel.length; ti++) {
730 delete tLevel[ti].child;
731 if (tLevel[ti].roleId == null) {
732 tLevel[ti].displayCheckbox = false;
733 tLevelDelArray.push(tLevel[ti].name);
734 tLevel[ti].name = '';
735 tLevel[ti].active = false;
736 tLevelRoleIdUndefined++
738 if (tLevel[ti].isSelected)
739 tLevelSelectedCount++;
741 if (tLevel[ti].displayCheckbox)
745 if (tLevelRoleIdUndefined == tLevelLen)
746 deletThisSLev = true;
747 if (tLevelSelectedCount == tLevelChildNumber) {
748 sLevel[si].isSelected = true;
749 sLevel[si].indeterminate = false;
750 sLevel[si].active = true;
751 } else if (tLevelSelectedCount > 0) {
752 sLevel[si].indeterminate = true;
753 sLevel[si].active = true;
756 /*Cleanup unused third level items*/
757 for (var i = 0; i < tLevelDelArray.length; i++) {
758 var name = tLevelDelArray[i];
759 for (var ti = 0; ti < tLevel.length; ti++) {
760 if (name == tLevel[ti].text) {
761 tLevel.splice(ti, 1);
768 if (deletThisSLev) { //remove the whole second level item if all it's child has no roleId
769 sLevel[si].displayCheckbox = false;
770 sLevelDelArray.push(sLevel[si].name);
771 sLevel[si].name = '';
772 sLevel[si].active = false;
774 if (sLevel[si].isSelected)
775 sLevelSelectedCount++;
776 if (sLevel[si].displayCheckbox)
780 if (sLevelSelectedCount == sLevelChildNumber && sLevelChildNumber != 0) {
781 fLevel.isSelected = true;
782 fLevel.indeterminate = false;
783 fLevel.active = true;
784 } else if (sLevelSelectedCount > 0) {
785 fLevel.indeterminate = true;
786 fLevel.active = true;
788 //fLevel.active=false;
789 fLevel.indeterminate = false;
791 /*Cleanup unused second level items*/
792 for (var i = 0; i < sLevelDelArray.length; i++) {
793 var name = sLevelDelArray[i];
794 for (var si = 0; si < sLevel.length; si++) {
795 if (name == sLevel[si].text) {
796 sLevel.splice(si, 1);
811 userNotificationsModalCtrl.$inject = ['$scope', '$log', 'functionalMenuService', 'confirmBoxService', 'notificationService', '$modal', 'ngDialog', '$state', '$filter', 'items'];
812 angular.module('ecompApp').controller('userNotificationsModalCtrl', userNotificationsModalCtrl);