diff --git a/src/components/MoneyReportHeader.tsx b/src/components/MoneyReportHeader.tsx index 3d0cb8c07911..e61d348df376 100644 --- a/src/components/MoneyReportHeader.tsx +++ b/src/components/MoneyReportHeader.tsx @@ -255,9 +255,17 @@ function MoneyReportHeader({ return Object.values(reportTransactions); }, [reportTransactions]); - const shouldBlockSubmit = useMemo(() => { + // When prevent self-approval is enabled & the current user is submitter AND they're submitting to themselves, we need to show the optimistic next step + // We should always show this optimistic message for policies with preventSelfApproval + // to avoid any flicker during transitions between online/offline states + const nextApproverAccountID = getNextApproverAccountID(moneyRequestReport); + const isSubmitterSameAsNextApprover = isReportOwner(moneyRequestReport) && nextApproverAccountID === moneyRequestReport?.ownerAccountID; + const isBlockSubmitDueToStrictPolicyRules = useMemo(() => { return shouldBlockSubmitDueToStrictPolicyRules(moneyRequestReport?.reportID, violations, areStrictPolicyRulesEnabled, accountID, email ?? '', transactions); }, [moneyRequestReport?.reportID, violations, areStrictPolicyRulesEnabled, accountID, email, transactions]); + const isBlockSubmitDueToPreventSelfApproval = isSubmitterSameAsNextApprover && policy?.preventSelfApproval; + + const shouldBlockSubmit = isBlockSubmitDueToStrictPolicyRules || isBlockSubmitDueToPreventSelfApproval; const iouTransactionID = isMoneyRequestAction(requestParentReportAction) ? getOriginalMessage(requestParentReportAction)?.IOUTransactionID : undefined; const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(iouTransactionID)}`, { @@ -444,14 +452,9 @@ function MoneyReportHeader({ hasDuplicates || shouldShowMarkAsResolved; - // When prevent self-approval is enabled & the current user is submitter AND they're submitting to themselves, we need to show the optimistic next step - // We should always show this optimistic message for policies with preventSelfApproval - // to avoid any flicker during transitions between online/offline states - const nextApproverAccountID = getNextApproverAccountID(moneyRequestReport); - const isSubmitterSameAsNextApprover = isReportOwner(moneyRequestReport) && nextApproverAccountID === moneyRequestReport?.ownerAccountID; let optimisticNextStep = isSubmitterSameAsNextApprover && policy?.preventSelfApproval ? buildOptimisticNextStepForPreventSelfApprovalsEnabled() : nextStep; - if (shouldBlockSubmit && isReportOwner(moneyRequestReport) && isOpenExpenseReport(moneyRequestReport)) { + if (isBlockSubmitDueToStrictPolicyRules && isReportOwner(moneyRequestReport) && isOpenExpenseReport(moneyRequestReport)) { optimisticNextStep = buildOptimisticNextStepForStrictPolicyRuleViolations(); } @@ -824,6 +827,7 @@ function MoneyReportHeader({ success onPress={confirmApproval} text={translate('iou.approve')} + isDisabled={isBlockSubmitDueToPreventSelfApproval} /> ), [CONST.REPORT.PRIMARY_ACTIONS.PAY]: ( diff --git a/src/libs/ReportPrimaryActionUtils.ts b/src/libs/ReportPrimaryActionUtils.ts index f16b80ace338..5f43a76ef11e 100644 --- a/src/libs/ReportPrimaryActionUtils.ts +++ b/src/libs/ReportPrimaryActionUtils.ts @@ -98,7 +98,7 @@ function isSubmitAction(report: Report, reportTransactions: Transaction[], polic const submitToAccountID = getSubmitToAccountID(policy, report); - if (submitToAccountID === report.ownerAccountID && policy?.preventSelfApproval) { + if (submitToAccountID === report.ownerAccountID && policy?.preventSelfApproval && !isReportSubmitter) { return false; } @@ -129,13 +129,6 @@ function isApproveAction(report: Report, reportTransactions: Transaction[], poli return false; } - const isPreventSelfApprovalEnabled = policy?.preventSelfApproval; - const isReportSubmitter = isCurrentUserSubmitter(report); - - if (isPreventSelfApprovalEnabled && isReportSubmitter) { - return false; - } - return isProcessingReportUtils(report); } diff --git a/src/libs/SearchUIUtils.ts b/src/libs/SearchUIUtils.ts index b7460981d5e3..d601e3b83917 100644 --- a/src/libs/SearchUIUtils.ts +++ b/src/libs/SearchUIUtils.ts @@ -75,7 +75,7 @@ import {translateLocal} from './Localize'; import Navigation from './Navigation/Navigation'; import Parser from './Parser'; import {getDisplayNameOrDefault} from './PersonalDetailsUtils'; -import {arePaymentsEnabled, canSendInvoice, getGroupPaidPoliciesWithExpenseChatEnabled, getPolicy, isPaidGroupPolicy, isPolicyPayer} from './PolicyUtils'; +import {arePaymentsEnabled, canSendInvoice, getGroupPaidPoliciesWithExpenseChatEnabled, getPolicy, getSubmitToAccountID, isPaidGroupPolicy, isPolicyPayer} from './PolicyUtils'; import { getIOUActionForReportID, getOriginalMessage, @@ -1287,7 +1287,8 @@ function getActions( const hasOnlyPendingCardOrScanningTransactions = allReportTransactions.length > 0 && allReportTransactions.every((t) => isScanning(t) || isPending(t)); - const isAllowedToApproveExpenseReport = isAllowedToApproveExpenseReportUtils(report, undefined, policy); + const submitToAccountID = getSubmitToAccountID(policy, report); + const isAllowedToApproveExpenseReport = isAllowedToApproveExpenseReportUtils(report, submitToAccountID, policy); // We're not supporting approve partial amount on search page now if (