"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .directive('approvalBanner', ['approvalModel', 'approvalService', 'events', 'systemAlertService', '$filter', 'userModel', '$window', 'authModel', function (approvalModel, approvalService, events, systemAlertService, $filter, userModel, $window, authModel) { return { restrict: 'E', replace: true, templateUrl: 'views/approval/approval-banner.html', scope: { context: '=' }, link: function (scope) { scope._ = _; scope.init = function () { scope.isSSOEnabled = false; scope.approval = {}; scope.state = { approvalRequestIsPending: true, isAlternateApprover: false, alternateFor: [], iconClass: { approve: 'icon-check_shield', reject: 'icon-cross_square', hold: 'icon-sandglass' } }; scope.review = { type: '', justification: '', justificationRequired: false }; scope.reviewActions = EntityVO.APPROVAL_ACTIONS; refreshApprovalList(true); scope.$on(events.REFRESH_APPROVAL_LIST, function (event, data) { refreshApprovalList(data && data.ignoreCache); }); scope.isSSOEnabled = authModel.isSSOEnabled(); }; scope.clearApprovalData = function () { scope.review = { type: '', justification: '', justificationRequired: false }; }; scope.checkForRequirement = function (action) { scope.review.justificationRequired = (action.statusLabel === EntityVO.APPROVAL_STATUS_REJECTED); }; scope.handleUserReviewAction = function () { scope.sendUserReview() .then(function () { scope.clearApprovalData(); scope.approval.isUserApprovalPending = false; }); }; scope.handleUserReviewActionViaSSO = function () { $window.open("/smartit/rest/sso/validate-reauth?reauth=true"); }; $window.approvalCallback = function () { console.log('Done with RSSO now submit approval!'); scope.handleUserReviewAction(); }; $window.approvalFailedCallback = function () { scope.$apply(function () { scope.handleUserReviewError(); }); }; scope.handleUserReviewError = function () { var msg = $filter('i18n')('approval.error.sso.change'); if (scope.context.type === EntityVO.TYPE_KNOWLEDGE) { msg = $filter('i18n')('approval.error.sso.knowledge'); } else if (scope.context.type === EntityVO.TYPE_RELEASE) { msg = $filter('i18n')('approval.error.sso.release'); } systemAlertService.error({ text: msg, clear: false }); }; scope.showApproversList = function () { scope.context.approvalList = scope.approval.approvalList; var showClosedTabDefault = true; if (scope.approval.approvalList && Array.isArray(scope.approval.approvalList.open) && scope.approval.approvalList.open.length > 0) { showClosedTabDefault = false; } approvalService.showApproversDialog(scope.context.approvalList, scope.context, showClosedTabDefault); }; scope.sendUserReview = function () { scope.state.approvalRequestIsPending = true; var approvalRequest = setApprovalRequestData(); return approvalModel.sendReviewResult(approvalRequest) .then(function () { scope.$emit(events.APPROVE_UPDATING_COMPLETE); scope.$emit(events.TICKET_PROFILE_RESEND_EVENT, { eventName: events.REFRESH_ACTIVITY_FEED }); systemAlertService.success({ text: $filter('i18n')('approval.updating.complete'), hide: 10000 }); }) .finally(function () { scope.state.approvalRequestIsPending = false; }); }; function refreshApprovalList(ignoreCache) { scope.state.approvalRequestIsPending = true; return approvalModel.getListOfApprovers(scope.context, ignoreCache) .then(function (approvalSummary) { scope.approval = approvalSummary; scope.context.approvalList = scope.approval.approvalList; checkForAlternateApprove(); }) .finally(function () { scope.state.approvalRequestIsPending = false; }); } function checkForAlternateApprove() { _.forEach(scope.approval.approvalList && scope.approval.approvalList.open, function (item) { if (item.userIsAlternateApprover) { scope.state.alternateFor.push(item.approver.fullName); } }); scope.state.isAlternateApprover = !!scope.state.alternateFor.length; } function setApprovalRequestData() { var userId = decodeURIComponent(userModel.userId), approvalRequest = []; _.forEach(scope.approval.approvalSummaries, function (summary) { if (EntityVO.PENDING_APPROVAL_STATUSES_LIST.indexOf(summary.status.value) >= 0 && summary.type === EntityVO.TYPE_PERSON && (summary.approver.id === userId || summary.userIsAlternateApprover)) { var approvalObj = { type: scope.review.type, parentId: scope.context.id, parentType: scope.context.type, signatureId: summary.signatureId, justification: scope.review.justification, processName: scope.context.approvalProcessName }; if (scope.review.password) { approvalObj.password = scope.review.password; } approvalRequest.push(approvalObj); } }); return approvalRequest; } scope.init(); } }; }]); })();