Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/MoneyRequestHeaderSecondaryActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import usePermissions from '@hooks/usePermissions';
import usePolicyForMovingExpenses from '@hooks/usePolicyForMovingExpenses';
import useReportIsArchived from '@hooks/useReportIsArchived';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useRestrictedActionPolicyID from '@hooks/useRestrictedActionPolicyID';
import useShouldDisplayButtonsInSeparateLine from '@hooks/useShouldDisplayButtonsInSeparateLine';
import useSplitEffectivePolicy from '@hooks/useSplitEffectivePolicy';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useThrottledButtonState from '@hooks/useThrottledButtonState';
Expand Down Expand Up @@ -148,6 +150,9 @@ function MoneyRequestHeaderSecondaryActions({reportID, onBackButtonPress}: Money
const [amountOwed] = useOnyx(ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED);
const [isSelfTourViewed = false] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
const [betas] = useOnyx(ONYXKEYS.BETAS);
const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID);
const splitEffectivePolicy = useSplitEffectivePolicy(report, undefined, transaction);
const restrictedActionPolicyID = useRestrictedActionPolicyID(policy);

// Custom hooks
const defaultExpensePolicy = useDefaultExpensePolicy();
Expand Down Expand Up @@ -341,7 +346,7 @@ function MoneyRequestHeaderSecondaryActions({reportID, onBackButtonPress}: Money
icon: expensifyIcons.ArrowSplit,
value: CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.SPLIT,
onSelected: () => {
initSplitExpense(transaction, policy, report, accountID, {isProduction});
initSplitExpense(transaction, report, splitEffectivePolicy, selfDMReportID, restrictedActionPolicyID, {isProduction});
},
},
[CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.MERGE]: {
Expand Down
8 changes: 7 additions & 1 deletion src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import useReportAttributes from '@hooks/useReportAttributes';
import useReportIsArchived from '@hooks/useReportIsArchived';
import useReportTransactions from '@hooks/useReportTransactions';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useRestrictedActionPolicyID from '@hooks/useRestrictedActionPolicyID';
import useSplitEffectivePolicy from '@hooks/useSplitEffectivePolicy';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -222,6 +224,7 @@ function MoneyRequestView({
const [policiesWithPerDiem] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {
selector: perDiemPoliciesSelector,
});
const splitEffectivePolicy = useSplitEffectivePolicy(transactionThreadReport, undefined, transaction);
const isPerDiemRequest = isPerDiemRequestTransactionUtils(transaction);
const perDiemOriginalPolicy = getPolicyByCustomUnitID(transaction, policiesWithPerDiem);

Expand All @@ -244,13 +247,16 @@ function MoneyRequestView({
policyID = parentReport?.policyID;
}

const restrictedActionPolicyID = useRestrictedActionPolicyID(policy);

const allPolicyCategories = usePolicyCategories();
const policyCategories = allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`];
const targetPolicyID = updatedTransaction?.reportID ? parentReport?.policyID : policyID;
const allPolicyTags = usePolicyTags();
const policyTagList = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${targetPolicyID}`];
const [nonPersonalAndWorkspaceCards] = useOnyx(ONYXKEYS.DERIVED.NON_PERSONAL_AND_WORKSPACE_CARD_LIST);
const [cardList] = useOnyx(ONYXKEYS.CARD_LIST);
const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID);

const [transactionBackup] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_BACKUP}${getNonEmptyStringOnyxID(linkedTransactionID)}`);
const transactionViolations = useTransactionViolations(transaction?.transactionID);
Expand Down Expand Up @@ -1091,7 +1097,7 @@ function MoneyRequestView({
}

if (shouldShowSplitIndicator && isSplitAvailable) {
initSplitExpense(transaction, policy, transactionThreadReport, currentUserAccountIDParam, {isProduction});
initSplitExpense(transaction, transactionThreadReport, splitEffectivePolicy, selfDMReportID, restrictedActionPolicyID, {isProduction});
return;
}

Expand Down
21 changes: 20 additions & 1 deletion src/hooks/useDeleteTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import useEnvironment from './useEnvironment';
import useNetwork from './useNetwork';
import useOnyx from './useOnyx';
import usePermissions from './usePermissions';
import usePolicyForMovingExpenses from './usePolicyForMovingExpenses';
import useRestrictedActionPolicyID from './useRestrictedActionPolicyID';
import {findSplitPolicyForCustomUnit, getSplitEffectivePolicy} from './useSplitEffectivePolicy';

type UseDeleteTransactionsParams = {
/** Report object (optional, can be used for context) */
Expand Down Expand Up @@ -81,6 +84,9 @@ function useDeleteTransactions({report, reportActions, policy}: UseDeleteTransac
const archivedReportsIDSet = useArchivedReportsIDSet();
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID);
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like allPolicies is only used with findSplitPolicyForCUstomUnit() which only references policy.customUnits. If that's correct, then please use a selector here to only subscribe to policy.customUnits.

@ZhenjaHorbach ZhenjaHorbach Jun 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this makes sense
You are right that findSplitPolicyForCustomUnit only reads policy.customUnits, but it uses them to find and return a specific full policy, which the deleteTransactions callback then passes downstream

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see that too. Thanks!

const {policyForMovingExpenses} = usePolicyForMovingExpenses();
const restrictedActionPolicyID = useRestrictedActionPolicyID(policy);
const {isOffline} = useNetwork();
const {isProduction} = useEnvironment();

Expand Down Expand Up @@ -154,7 +160,17 @@ function useDeleteTransactions({report, reportActions, policy}: UseDeleteTransac
const transactionReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${splitExpenseEditTransaction.reportID}`];
const selfDMReport = isSelfDM(report) ? report : allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`];
const splitExpenseEditTransactionReport = transactionReport ?? selfDMReport;
initSplitExpense(splitExpenseEditTransaction, policy, splitExpenseEditTransactionReport, currentUserPersonalDetails.accountID, {
// We intentionally don't pass `searchSnapshotPolicy` here (unlike the on-page split flow in
// `useSplitEffectivePolicy`). This preserves the pre-refactor behavior: `initSplitExpense` resolved
// the policy from the report policy and the transaction's customUnit only, never from a search-results
// snapshot. The snapshot-aware branch lives on the split pages, where the mileage rate is rendered.
const splitEffectivePolicy = getSplitEffectivePolicy({
Comment thread
ZhenjaHorbach marked this conversation as resolved.
Resolved
policy,
transaction: splitExpenseEditTransaction,
policyForCustomUnit: findSplitPolicyForCustomUnit(allPolicies, splitExpenseEditTransaction),
fallbackPolicy: policyForMovingExpenses,
});
initSplitExpense(splitExpenseEditTransaction, splitExpenseEditTransactionReport, splitEffectivePolicy, selfDMReportID, restrictedActionPolicyID, {
navigateToEditSplitExpense: true,
isProduction,
});
Expand Down Expand Up @@ -355,6 +371,9 @@ function useDeleteTransactions({report, reportActions, policy}: UseDeleteTransac
allPolicyTags,
personalDetails,
selfDMReportID,
allPolicies,
policyForMovingExpenses,
restrictedActionPolicyID,
getSplitExpenseEditTransactionOnDelete,
isOffline,
isProduction,
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useExpenseActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ import useLocalize from './useLocalize';
import useOnyx from './useOnyx';
import usePermissions from './usePermissions';
import useReportIsArchived from './useReportIsArchived';
import useRestrictedActionPolicyID from './useRestrictedActionPolicyID';
import useSplitEffectivePolicy from './useSplitEffectivePolicy';
import useTheme from './useTheme';
import useThrottledButtonState from './useThrottledButtonState';
import useTransactionsAndViolationsForReport from './useTransactionsAndViolationsForReport';
Expand Down Expand Up @@ -113,6 +115,8 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic
}

const currentTransaction = transactions.at(0);
const splitEffectivePolicy = useSplitEffectivePolicy(moneyRequestReport, undefined, currentTransaction);
const restrictedActionPolicyID = useRestrictedActionPolicyID(policy);
Comment thread
ZhenjaHorbach marked this conversation as resolved.
Resolved
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(iouTransactionID)}`);
const [originalTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transaction?.comment?.originalTransactionID)}`);
const {iouReport, chatReport: chatIOUReport, isChatIOUReportArchived} = useGetIOUReportFromReportAction(requestParentReportAction);
Expand Down Expand Up @@ -295,7 +299,7 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic
if (transactions.length !== 1) {
return;
}
initSplitExpense(currentTransaction, policy, moneyRequestReport, accountID, {isProduction});
initSplitExpense(currentTransaction, moneyRequestReport, splitEffectivePolicy, selfDMReportID, restrictedActionPolicyID, {isProduction});
},
},
[CONST.REPORT.SECONDARY_ACTIONS.MERGE]: {
Expand Down
27 changes: 27 additions & 0 deletions src/hooks/useRestrictedActionPolicyID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type {OnyxEntry} from 'react-native-onyx';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy} from '@src/types/onyx';
import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails';
import useOnyx from './useOnyx';

/**
* Resolves the policy ID to redirect to `RESTRICTED_ACTION` when the given policy's billable actions
* are restricted (e.g. expired/owing workspace), or `undefined` when the action is allowed.
*
* Centralizes the billing-grace / amount-owed Onyx reads + the `shouldRestrictUserBillableActions`
* check so callers (e.g. the split entry points) don't each duplicate them.
*/
function useRestrictedActionPolicyID(policy: OnyxEntry<Policy>): string | undefined {
const {accountID} = useCurrentUserPersonalDetails();
const [ownerBillingGracePeriodEnd] = useOnyx(ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END);
const [userBillingGracePeriodEnds] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END);
Comment thread
ZhenjaHorbach marked this conversation as resolved.
Resolved
const [amountOwed] = useOnyx(ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED);

if (!policy || !shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, accountID)) {
return undefined;
}
return policy.id;
}

export default useRestrictedActionPolicyID;
10 changes: 9 additions & 1 deletion src/hooks/useSearchBulkActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ import {getParticipantsInvoiceReport} from './useParticipantsInvoiceReport';
import usePaymentContext from './usePaymentContext';
import usePermissions from './usePermissions';
import usePolicyForMovingExpenses from './usePolicyForMovingExpenses';
import useRestrictedActionPolicyID from './useRestrictedActionPolicyID';
import useSelfDMReport from './useSelfDMReport';
import useSplitEffectivePolicy from './useSplitEffectivePolicy';
import useTheme from './useTheme';
import useThemeStyles from './useThemeStyles';
import useUndeleteTransactions from './useUndeleteTransactions';
Expand Down Expand Up @@ -359,6 +361,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const {isBetaEnabled} = usePermissions();
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID);

const defaultExpensePolicy = useDefaultExpensePolicy();
const {policyForMovingExpensesID} = usePolicyForMovingExpenses();
Expand Down Expand Up @@ -540,6 +543,8 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
const isFirstTransactionUnreported = firstTransaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID;
const firstTransactionReport = firstTransactionFetchedReport ?? (isFirstTransactionUnreported ? selfDMReport : undefined);
const [firstTransactionPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${firstTransactionReport?.policyID}`);
const splitEffectivePolicy = useSplitEffectivePolicy(firstTransactionReport, undefined, firstTransaction);
const restrictedActionPolicyID = useRestrictedActionPolicyID(firstTransactionPolicy);

// Use the split-aware delete hook for bulk transaction deletion so split children trigger
// updateSplitTransactions (with reverse-split when only one sibling is left), instead of plain
Expand Down Expand Up @@ -1956,7 +1961,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
icon: expensifyIcons.ArrowSplit,
value: CONST.SEARCH.BULK_ACTION_TYPES.SPLIT,
onSelected: () => {
initSplitExpense(firstTransaction, firstTransactionPolicy, firstTransactionReport, accountID, {isProduction});
initSplitExpense(firstTransaction, firstTransactionReport, splitEffectivePolicy, selfDMReportID, restrictedActionPolicyID, {isProduction});
},
});
}
Expand Down Expand Up @@ -2097,6 +2102,9 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
transactions,
isBetaEnabled,
defaultExpensePolicy,
selfDMReportID,
splitEffectivePolicy,
restrictedActionPolicyID,
doSelectedItemsBelongToSubmitPolicy,
openSearchReportSubmitToPopover,
searchResults?.data,
Expand Down
8 changes: 7 additions & 1 deletion src/hooks/useSelectedTransactionsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ import useNetworkWithOfflineStatus from './useNetworkWithOfflineStatus';
import useOnyx from './useOnyx';
import usePermissions from './usePermissions';
import useReportIsArchived from './useReportIsArchived';
import useRestrictedActionPolicyID from './useRestrictedActionPolicyID';
import {shouldShowBulkDuplicateOption} from './useSearchBulkActions';
import useSplitEffectivePolicy from './useSplitEffectivePolicy';

const {HOLD, UNHOLD, MOVE, MERGE, SPLIT, DUPLICATE} = CONST.REPORT.SELECTED_TRANSACTIONS_BULK_ACTION_TYPES;

Expand Down Expand Up @@ -94,6 +96,10 @@ function useSelectedTransactionsActions({
const [csvExportLayouts] = useOnyx(ONYXKEYS.NVP_CSV_EXPORT_LAYOUTS);
const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const [allReportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS);
const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID);
const firstSelectedTransaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${selectedTransactionIDs.at(0)}`];
const splitEffectivePolicy = useSplitEffectivePolicy(report, undefined, firstSelectedTransaction);
const restrictedActionPolicyID = useRestrictedActionPolicyID(policy);
const {getCurrencyDecimals} = useCurrencyListActions();

const expensifyIcons = useMemoizedLazyExpensifyIcons([
Expand Down Expand Up @@ -497,7 +503,7 @@ function useSelectedTransactionsActions({
icon: expensifyIcons.ArrowSplit,
value: SPLIT,
onSelected: () => {
initSplitExpense(firstTransaction, policy, report, currentUserAccountID, {isProduction});
initSplitExpense(firstTransaction, report, splitEffectivePolicy, selfDMReportID, restrictedActionPolicyID, {isProduction});
},
});
}
Expand Down
Loading
Loading