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
39 changes: 34 additions & 5 deletions src/hooks/useSearchBulkActions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {InteractionManager} from 'react-native';
import type {OnyxCollection} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type {DropdownOption} from '@components/ButtonWithDropdownMenu/types';
import {useDelegateNoAccessActions, useDelegateNoAccessState} from '@components/DelegateNoAccessModalProvider';
Expand Down Expand Up @@ -44,14 +45,15 @@ import {
isIOUReport as isIOUReportUtil,
} from '@libs/ReportUtils';
import {navigateToSearchRHP, shouldShowDeleteOption} from '@libs/SearchUIUtils';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import {hasTransactionBeenRejected} from '@libs/TransactionUtils';
import variables from '@styles/variables';
import {canIOUBePaid, dismissRejectUseExplanation} from '@userActions/IOU';
import {openOldDotLink} from '@userActions/Link';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Report, SearchResults, Transaction, TransactionViolations} from '@src/types/onyx';
import type {BillingGraceEndPeriod, Report, SearchResults, Transaction, TransactionViolations} from '@src/types/onyx';
import useAllTransactions from './useAllTransactions';
import useBulkPayOptions from './useBulkPayOptions';
import useConfirmModal from './useConfirmModal';
Expand All @@ -69,6 +71,10 @@ type UseSearchBulkActionsParams = {
queryJSON: SearchQueryJSON | undefined;
};

function getRestrictedPolicyID(items: Array<{policyID?: string}>, billingGracePeriods: OnyxCollection<BillingGraceEndPeriod>): string | undefined {
return items.map((item) => item.policyID).find((policyID): policyID is string => !!policyID && shouldRestrictUserBillableActions(policyID, billingGracePeriods));
}

function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
const {translate, localeCompare, formatPhoneNumber} = useLocalize();
const styles = useThemeStyles();
Expand All @@ -91,6 +97,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
const [csvExportLayouts] = useOnyx(ONYXKEYS.NVP_CSV_EXPORT_LAYOUTS);
const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION);
const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const [userBillingGraceEndPeriodCollection] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END);

// Cache the last search results that had data, so the merge option remains available
// while results are temporarily unset (e.g. during sorting/loading).
Expand Down Expand Up @@ -359,9 +366,15 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
return;
}

const selectedPolicyIDList = selectedReports.length
? selectedReports.map((report) => report.policyID)
: Object.values(selectedTransactions).map((transaction) => transaction.policyID);
const selectedItems = selectedReports.length ? selectedReports : Object.values(selectedTransactions);

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.

❌ CONSISTENCY-3 (docs)

The billing restriction check pattern (map policyIDs -> find restricted -> navigate to RESTRICTED_ACTION) is duplicated three times in this file: once in handleApproveWithDEWCheck, once in onBulkPaySelected, and once in the submit onSelected handler. All three blocks perform the same logic with only minor variations in how they obtain the list of policy IDs.

Extract a shared helper function, for example:

function getRestrictedPolicyID(
    items: Array<{policyID?: string}>,
    billingGracePeriods: OnyxCollection<BillingGraceEndPeriod>,
): string | undefined {
    return items
        .map((item) => item.policyID)
        .find((policyID): policyID is string =>
            !!policyID && shouldRestrictUserBillableActions(policyID, billingGracePeriods),
        );
}

Then replace each duplicated block with:

const restrictedPolicyID = getRestrictedPolicyID(selectedOptions, userBillingGraceEndPeriodCollection);
if (restrictedPolicyID) {
    Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(restrictedPolicyID));
    return;
}

Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

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.

Fixed — extracted a getRestrictedPolicyID helper function that all 3 call sites now use.

const restrictedPolicyID = getRestrictedPolicyID(selectedItems, userBillingGraceEndPeriodCollection);
if (restrictedPolicyID) {
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(restrictedPolicyID));
return;
}

const selectedPolicyIDList = selectedItems.map((item) => item.policyID);
const hasDEWPolicy = selectedPolicyIDList.some((policyID) => {
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
return hasDynamicExternalWorkflow(policy);
Expand Down Expand Up @@ -404,6 +417,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
translate,
hash,
clearSelectedTransactions,
userBillingGraceEndPeriodCollection,
]);

const {expenseCount, uniqueReportCount} = useMemo(() => {
Expand Down Expand Up @@ -521,9 +535,16 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
return;
}

const activeRoute = Navigation.getActiveRoute();
const selectedOptions = selectedReports.length ? selectedReports : Object.values(selectedTransactions);

const restrictedPolicyID = getRestrictedPolicyID(selectedOptions, userBillingGraceEndPeriodCollection);
if (restrictedPolicyID) {
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(restrictedPolicyID));
return;
}

const activeRoute = Navigation.getActiveRoute();

for (const item of selectedOptions) {
const itemPolicyID = item.policyID;
const itemReportID = item.reportID;
Expand Down Expand Up @@ -634,6 +655,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
personalPolicyID,
allTransactions,
allReports,
userBillingGraceEndPeriodCollection,
],
);

Expand Down Expand Up @@ -807,6 +829,12 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {

const itemList = !selectedReports.length ? Object.values(selectedTransactions).map((transaction) => transaction) : (selectedReports?.filter((report) => !!report) ?? []);

const restrictedPolicyID = getRestrictedPolicyID(itemList, userBillingGraceEndPeriodCollection);
if (restrictedPolicyID) {
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(restrictedPolicyID));
return;
}

for (const item of itemList) {
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${item.policyID}`];
if (policy) {
Expand Down Expand Up @@ -1047,6 +1075,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
styles.colorMuted,
styles.fontWeightNormal,
styles.textWrap,
userBillingGraceEndPeriodCollection,
]);

const handleOfflineModalClose = useCallback(() => {
Expand Down
12 changes: 12 additions & 0 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,32 @@ function handleActionButtonPress({
onDelegateAccessRestricted?.();
return;
}
if (snapshotReport.policyID && shouldRestrictUserBillableActions(snapshotReport.policyID)) {
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(snapshotReport.policyID));
return;
}
getPayActionCallback(hash, item, goToItem, snapshotReport, snapshotPolicy, lastPaymentMethod, currentSearchKey, personalPolicyID);
return;
case CONST.SEARCH.ACTION_TYPES.APPROVE:
if (isDelegateAccessRestricted) {
onDelegateAccessRestricted?.();
return;
}
if (snapshotReport.policyID && shouldRestrictUserBillableActions(snapshotReport.policyID)) {
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(snapshotReport.policyID));
return;
}
if (hasDynamicExternalWorkflow(snapshotPolicy) && !isDEWBetaEnabled) {
onDEWModalOpen?.();
return;
}
approveMoneyRequestOnSearch(hash, item.reportID ? [item.reportID] : [], currentSearchKey);
return;
case CONST.SEARCH.ACTION_TYPES.SUBMIT: {
if (snapshotReport.policyID && shouldRestrictUserBillableActions(snapshotReport.policyID)) {
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(snapshotReport.policyID));
return;
}
if (hasDynamicExternalWorkflow(snapshotPolicy) && !isDEWBetaEnabled) {
onDEWModalOpen?.();
return;
Expand Down
Loading