From d4d6ba8c390e2079781ff26859ad5dd377da5563 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Sat, 6 Dec 2025 15:08:38 +0530 Subject: [PATCH 1/2] Refactor: Deprecate getPolicy (part 6) --- src/libs/actions/Report.ts | 8 ++------ src/pages/NewReportWorkspaceSelectionPage.tsx | 2 +- src/pages/Search/EmptySearchView.tsx | 6 +++--- .../Search/SearchTransactionsChangeReport.tsx | 4 ++-- .../AttachmentPickerWithMenuItems.tsx | 6 +++--- .../sidebar/FloatingActionButtonAndPopover.tsx | 6 +++--- .../iou/request/step/IOURequestEditReport.tsx | 4 ++-- .../iou/request/step/IOURequestStepReport.tsx | 4 ++-- tests/actions/IOUTest.ts | 17 +++++++++-------- tests/actions/ReportTest.ts | 6 +++--- 10 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 5d7b08d21d3b..4b6b1a1b1bbe 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -89,7 +89,6 @@ import * as PhoneNumber from '@libs/PhoneNumber'; import { getDefaultApprover, getMemberAccountIDsForWorkspace, - getPolicy, isInstantSubmitEnabled, isPaidGroupPolicy, isPolicyAdmin as isPolicyAdminPolicyUtils, @@ -3052,12 +3051,9 @@ function createNewReport( creatorPersonalDetails: CurrentUserPersonalDetails, hasViolationsParam: boolean, isASAPSubmitBetaEnabled: boolean, - policyID?: string, + policy: OnyxEntry, shouldNotifyNewAction = false, ) { - // This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850 - // eslint-disable-next-line @typescript-eslint/no-deprecated - const policy = getPolicy(policyID); const optimisticReportID = generateReportID(); const reportActionID = rand64(); const reportPreviewReportActionID = rand64(); @@ -3074,7 +3070,7 @@ function createNewReport( API.write( WRITE_COMMANDS.CREATE_APP_REPORT, - {reportName: optimisticReportName, type: CONST.REPORT.TYPE.EXPENSE, policyID, reportID: optimisticReportID, reportActionID, reportPreviewReportActionID}, + {reportName: optimisticReportName, type: CONST.REPORT.TYPE.EXPENSE, policyID: policy?.id, reportID: optimisticReportID, reportActionID, reportPreviewReportActionID}, {optimisticData, successData, failureData}, ); if (shouldNotifyNewAction) { diff --git a/src/pages/NewReportWorkspaceSelectionPage.tsx b/src/pages/NewReportWorkspaceSelectionPage.tsx index 9a9705fa8657..59eb22fe6017 100644 --- a/src/pages/NewReportWorkspaceSelectionPage.tsx +++ b/src/pages/NewReportWorkspaceSelectionPage.tsx @@ -115,7 +115,7 @@ function NewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelectionPag const createReport = useCallback( (policyID: string) => { - const optimisticReport = createNewReport(currentUserPersonalDetails, isASAPSubmitBetaEnabled, hasViolations, policyID); + const optimisticReport = createNewReport(currentUserPersonalDetails, isASAPSubmitBetaEnabled, hasViolations, policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]); const selectedTransactionsKeys = Object.keys(selectedTransactions); if (isMovingExpenses && (!!selectedTransactionsKeys.length || !!selectedTransactionIDs.length)) { diff --git a/src/pages/Search/EmptySearchView.tsx b/src/pages/Search/EmptySearchView.tsx index f13ef605272c..4b676ee530ed 100644 --- a/src/pages/Search/EmptySearchView.tsx +++ b/src/pages/Search/EmptySearchView.tsx @@ -202,15 +202,15 @@ function EmptySearchViewContent({ const hasEmptyReport = useMemo(() => hasEmptyReportsForPolicy(reportSummaries, defaultChatEnabledPolicyID, accountID), [accountID, defaultChatEnabledPolicyID, reportSummaries]); const handleCreateWorkspaceReport = useCallback(() => { - if (!defaultChatEnabledPolicyID) { + if (!defaultChatEnabledPolicy?.id) { return; } - const {reportID: createdReportID} = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, defaultChatEnabledPolicyID); + const {reportID: createdReportID} = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, defaultChatEnabledPolicy); Navigation.setNavigationActionToMicrotaskQueue(() => { Navigation.navigate(ROUTES.SEARCH_MONEY_REQUEST_REPORT.getRoute({reportID: createdReportID, backTo: Navigation.getActiveRoute()})); }); - }, [currentUserPersonalDetails, hasViolations, defaultChatEnabledPolicyID, isASAPSubmitBetaEnabled]); + }, [currentUserPersonalDetails, hasViolations, defaultChatEnabledPolicy, isASAPSubmitBetaEnabled]); const {openCreateReportConfirmation: openCreateReportFromSearch, CreateReportConfirmationModal} = useCreateEmptyReportConfirmation({ policyID: defaultChatEnabledPolicyID, diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index d598f750ab43..025edc61ed02 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -92,7 +92,7 @@ function SearchTransactionsChangeReport() { }, [selectedTransactions, selectedTransactionsKeys]); const createReportForPolicy = () => { - const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForMovingExpensesID); + const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForMovingExpenses); const reportNextStep = allReportNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${optimisticReport.reportID}`]; setNavigationActionToMicrotaskQueue(() => { changeTransactionsReport( @@ -101,7 +101,7 @@ function SearchTransactionsChangeReport() { session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '', optimisticReport, - policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined, + policyForMovingExpenses, reportNextStep, allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyForMovingExpensesID}`], ); diff --git a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx index 3e4f24512a15..510a7a8fd21c 100644 --- a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx +++ b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx @@ -177,7 +177,7 @@ function AttachmentPickerWithMenuItems({ const {openCreateReportConfirmation, CreateReportConfirmationModal} = useCreateEmptyReportConfirmation({ policyID: report?.policyID, policyName: policy?.name ?? '', - onConfirm: () => selectOption(() => createNewReport(currentUserPersonalDetails, isASAPSubmitBetaEnabled, hasViolations, report?.policyID, true), true), + onConfirm: () => selectOption(() => createNewReport(currentUserPersonalDetails, isASAPSubmitBetaEnabled, hasViolations, policy, true), true), }); const openCreateReportConfirmationRef = useRef(openCreateReportConfirmation); @@ -187,9 +187,9 @@ function AttachmentPickerWithMenuItems({ if (hasEmptyReport) { openCreateReportConfirmationRef.current(); } else { - createNewReport(currentUserPersonalDetails, isASAPSubmitBetaEnabled, hasViolations, report?.policyID, true); + createNewReport(currentUserPersonalDetails, isASAPSubmitBetaEnabled, hasViolations, policy, true); } - }, [currentUserPersonalDetails, hasEmptyReport, isASAPSubmitBetaEnabled, hasViolations, report?.policyID]); + }, [currentUserPersonalDetails, hasEmptyReport, isASAPSubmitBetaEnabled, hasViolations, policy]); const teacherUnitePolicyID = isProduction ? CONST.TEACHERS_UNITE.PROD_POLICY_ID : CONST.TEACHERS_UNITE.TEST_POLICY_ID; const isTeachersUniteReport = report?.policyID === teacherUnitePolicyID; diff --git a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx index 11255c6d1fca..960c95a58f7d 100644 --- a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx +++ b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx @@ -205,7 +205,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, ref ); const handleCreateWorkspaceReport = useCallback(() => { - if (!defaultChatEnabledPolicyID) { + if (!defaultChatEnabledPolicy?.id) { return; } @@ -213,7 +213,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, ref clearLastSearchParams(); } - const {reportID: createdReportID} = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, defaultChatEnabledPolicyID); + const {reportID: createdReportID} = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, defaultChatEnabledPolicy); Navigation.setNavigationActionToMicrotaskQueue(() => { Navigation.navigate( isSearchTopmostFullScreenRoute() @@ -222,7 +222,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, ref {forceReplace: isReportInSearch}, ); }); - }, [currentUserPersonalDetails, hasViolations, defaultChatEnabledPolicyID, isASAPSubmitBetaEnabled, isReportInSearch]); + }, [currentUserPersonalDetails, hasViolations, defaultChatEnabledPolicy, isASAPSubmitBetaEnabled, isReportInSearch]); const {openCreateReportConfirmation: openFabCreateReportConfirmation, CreateReportConfirmationModal: FabCreateReportConfirmationModal} = useCreateEmptyReportConfirmation({ policyID: defaultChatEnabledPolicyID, diff --git a/src/pages/iou/request/step/IOURequestEditReport.tsx b/src/pages/iou/request/step/IOURequestEditReport.tsx index 0cf3d3c13ecf..59617874186a 100644 --- a/src/pages/iou/request/step/IOURequestEditReport.tsx +++ b/src/pages/iou/request/step/IOURequestEditReport.tsx @@ -98,11 +98,11 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) { }; const createReportForPolicy = () => { - if (!policyForMovingExpensesID) { + if (!policyForMovingExpenses?.id) { return; } - const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForMovingExpensesID); + const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForMovingExpenses); selectReport({value: optimisticReport.reportID}, optimisticReport); }; diff --git a/src/pages/iou/request/step/IOURequestStepReport.tsx b/src/pages/iou/request/step/IOURequestStepReport.tsx index 4fa2897ec967..7683fb425e42 100644 --- a/src/pages/iou/request/step/IOURequestStepReport.tsx +++ b/src/pages/iou/request/step/IOURequestStepReport.tsx @@ -175,11 +175,11 @@ function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) { const shouldShowNotFoundPage = useShowNotFoundPageInIOUStep(action, iouType, reportActionID, reportOrDraftReport, transaction); const createReportForPolicy = () => { - if (!policyForMovingExpensesID) { + if (!policyForMovingExpenses?.id) { return; } - const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForMovingExpensesID); + const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForMovingExpenses); handleRegularReportSelection({value: optimisticReport.reportID}, optimisticReport); }; diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 25c0b659b772..6c5e30c8bc4e 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -8088,17 +8088,18 @@ describe('actions/IOU', () => { const creatorPersonalDetails = personalDetailsList?.[CARLOS_ACCOUNT_ID] ?? {accountID: CARLOS_ACCOUNT_ID}; const policyID = generatePolicyID(); - createWorkspace({ - policyOwnerEmail: CARLOS_EMAIL, - makeMeAdmin: true, - policyName: "Carlos's Workspace", - policyID, - currency: CONST.CURRENCY.USD, - }); + const mockPolicy: Policy = { + ...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM, "Carlos's Workspace"), + id: policyID, + outputCurrency: CONST.CURRENCY.USD, + owner: CARLOS_EMAIL, + ownerAccountID: CARLOS_ACCOUNT_ID, + pendingAction: undefined, + }; await waitForBatchedUpdates(); - createNewReport(creatorPersonalDetails, true, false, policyID); + createNewReport(creatorPersonalDetails, true, false, mockPolicy); // Create a tracked expense const selfDMReport: Report = { ...createRandomReport(1, CONST.REPORT.CHAT_TYPE.SELF_DM), diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index 02e9b055f3ca..8f490ccb3123 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -1759,7 +1759,7 @@ describe('actions/Report', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policy); mockFetchData.pause(); - const {reportID} = Report.createNewReport({accountID}, true, false, policyID); + const {reportID} = Report.createNewReport({accountID}, true, false, policy); const parentReport = ReportUtils.getPolicyExpenseChat(accountID, policyID); const reportPreviewAction = await new Promise>>((resolve) => { @@ -1832,7 +1832,7 @@ describe('actions/Report', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policy); mockFetchData.pause(); - Report.createNewReport({accountID}, true, false, policyID); + Report.createNewReport({accountID}, true, false, policy); const parentReport = ReportUtils.getPolicyExpenseChat(accountID, policyID); await new Promise((resolve) => { @@ -1870,7 +1870,7 @@ describe('actions/Report', () => { } // When create new report - Report.createNewReport({accountID}, true, false, policyID); + Report.createNewReport({accountID}, true, false, policy); // Then the parent report's hasOutstandingChildRequest property should remain unchanged await new Promise((resolve) => { From 1a2e03065a4598f247ae922d070bc76100198c43 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Mon, 15 Dec 2025 19:17:41 +0530 Subject: [PATCH 2/2] Fix after merge --- src/pages/NewReportWorkspaceSelectionPage.tsx | 9 ++++++++- src/pages/iou/request/step/IOURequestEditReport.tsx | 5 +++-- src/pages/iou/request/step/IOURequestStepReport.tsx | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/pages/NewReportWorkspaceSelectionPage.tsx b/src/pages/NewReportWorkspaceSelectionPage.tsx index c64ad76e57f5..6370f05b3803 100644 --- a/src/pages/NewReportWorkspaceSelectionPage.tsx +++ b/src/pages/NewReportWorkspaceSelectionPage.tsx @@ -116,7 +116,14 @@ function NewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelectionPag const createReport = useCallback( (policyID: string, shouldDismissEmptyReportsConfirmation?: boolean) => { - const optimisticReport = createNewReport(currentUserPersonalDetails, isASAPSubmitBetaEnabled, hasViolations, policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`], false, shouldDismissEmptyReportsConfirmation); + const optimisticReport = createNewReport( + currentUserPersonalDetails, + isASAPSubmitBetaEnabled, + hasViolations, + policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`], + false, + shouldDismissEmptyReportsConfirmation, + ); const selectedTransactionsKeys = Object.keys(selectedTransactions); if (isMovingExpenses && (!!selectedTransactionsKeys.length || !!selectedTransactionIDs.length)) { diff --git a/src/pages/iou/request/step/IOURequestEditReport.tsx b/src/pages/iou/request/step/IOURequestEditReport.tsx index 57a57bc94353..80a58b2e7e2c 100644 --- a/src/pages/iou/request/step/IOURequestEditReport.tsx +++ b/src/pages/iou/request/step/IOURequestEditReport.tsx @@ -46,6 +46,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) { const [allPolicyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}`, {canBeMissing: true}); const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: true}); const currentUserPersonalDetails = useCurrentUserPersonalDetails(); + const selectedReportPolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${selectedReport?.policyID}`]; const hasPerDiemTransactions = useMemo(() => { return selectedTransactionIDs.some((transactionID) => { @@ -102,8 +103,8 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) { return; } - const policyForNewReportID = hasPerDiemTransactions ? selectedReport?.policyID : policyForMovingExpensesID; - const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForMovingExpenses, false, shouldDismissEmptyReportsConfirmation); + const policyForNewReport = hasPerDiemTransactions ? selectedReportPolicy : policyForMovingExpenses; + const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForNewReport, false, shouldDismissEmptyReportsConfirmation); selectReport({value: optimisticReport.reportID}, optimisticReport); }; diff --git a/src/pages/iou/request/step/IOURequestStepReport.tsx b/src/pages/iou/request/step/IOURequestStepReport.tsx index 54032431218e..b112d70d9207 100644 --- a/src/pages/iou/request/step/IOURequestStepReport.tsx +++ b/src/pages/iou/request/step/IOURequestStepReport.tsx @@ -182,8 +182,8 @@ function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) { return; } - const policyForNewReport = isPerDiemTransaction && perDiemOriginalPolicy ? perDiemOriginalPolicy.id : policyForMovingExpensesID; - const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForMovingExpenses, false, shouldDismissEmptyReportsConfirmation); + const policyForNewReport = isPerDiemTransaction && perDiemOriginalPolicy ? perDiemOriginalPolicy : policyForMovingExpenses; + const optimisticReport = createNewReport(currentUserPersonalDetails, hasViolations, isASAPSubmitBetaEnabled, policyForNewReport, false, shouldDismissEmptyReportsConfirmation); handleRegularReportSelection({value: optimisticReport.reportID}, optimisticReport); };