diff --git a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx index a055a8a52243..3650649ae66c 100644 --- a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx +++ b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx @@ -77,7 +77,8 @@ function ReportField({selectedParticipants, iouType, reportID, reportActionID, a * We need to check if the transaction report exists first in order to prevent the outstanding reports from being used. * Also we need to check if transaction report exists in outstanding reports in order to show a correct report name. */ - const shouldUseTransactionReport = (!!transactionReportEntry && isReportOutstanding(transactionReportEntry, policyID, reportNameValuePairs, false)) || isUnreported; + const transactionReportNameValuePair = reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${transactionReportID}`]; + const shouldUseTransactionReport = (!!transactionReportEntry && isReportOutstanding(transactionReportEntry, policyID, transactionReportNameValuePair, false)) || isUnreported; const ownerAccountID = selectedParticipants?.at(0)?.ownerAccountID; diff --git a/src/libs/IOUUtils.ts b/src/libs/IOUUtils.ts index 2c2f81332c7a..e45aff7b04ef 100644 --- a/src/libs/IOUUtils.ts +++ b/src/libs/IOUUtils.ts @@ -3,7 +3,7 @@ import type {CurrencyListActionsContextType} from '@hooks/useCurrencyList'; import type {IOUAction, IOURequestType, IOUType} from '@src/CONST'; import CONST from '@src/CONST'; import ROUTES from '@src/ROUTES'; -import type {OnyxInputOrEntry, Policy, Report, ReportAction, Transaction} from '@src/types/onyx'; +import type {OnyxInputOrEntry, Policy, Report, ReportAction, ReportNameValuePairs, Transaction} from '@src/types/onyx'; import type {Attendee, Participant} from '@src/types/onyx/IOU'; import type {CurrentUserPersonalDetails} from '@src/types/onyx/PersonalDetails'; @@ -553,16 +553,19 @@ function resolveReportForMoneyRequest({ transactionReport, routeReport, policy, + reportNameValuePair, }: { transaction: OnyxEntry; transactionReport: OnyxEntry; routeReport: OnyxEntry; policy: OnyxEntry; + reportNameValuePair: OnyxInputOrEntry; }): OnyxEntry { if (transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID) { return undefined; } - const canUseTransactionReport = !(isProcessingReport(transactionReport) && !policy?.harvesting?.enabled) && isReportOutstanding(transactionReport, policy?.id, undefined, false); + const canUseTransactionReport = + !(isProcessingReport(transactionReport) && !policy?.harvesting?.enabled) && isReportOutstanding(transactionReport, policy?.id, reportNameValuePair, false); const shouldUseTransactionReport = !!transactionReport && (canUseTransactionReport || !routeReport); if (shouldUseTransactionReport) { return transactionReport; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index ef70facb75e3..f8ac31c84c5f 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5336,7 +5336,8 @@ function canEditFieldOfMoneyRequest({ return canUnreportedBeMoved(transaction, allPolicies); } - if (!isReportOutstanding(moneyRequestReport, moneyRequestReport.policyID, reportNameValuePairs)) { + const moneyRequestReportNameValuePair = reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${moneyRequestReport.reportID}`]; + if (!isReportOutstanding(moneyRequestReport, moneyRequestReport.policyID, moneyRequestReportNameValuePair)) { return false; } @@ -5365,7 +5366,7 @@ function canEditFieldOfMoneyRequest({ } // Check the cheaper condition first - if ((isOwner || isAdmin || isManager) && isReportOutstanding(moneyRequestReport, moneyRequestReport.policyID, reportNameValuePairs)) { + if ((isOwner || isAdmin || isManager) && isReportOutstanding(moneyRequestReport, moneyRequestReport.policyID, moneyRequestReportNameValuePair)) { return true; } @@ -12052,7 +12053,7 @@ function isReportOutstanding( iouReport: OnyxInputOrEntry, policyID: string | undefined, // Temporarily optional while archived report checks are migrated in smaller PRs. Remove this fallback as part of https://github.com/Expensify/App/issues/66422. - reportNameValuePairs?: OnyxCollection, + reportNameValuePair?: OnyxInputOrEntry, allowSubmitted = true, ): boolean { if ( @@ -12066,8 +12067,8 @@ function isReportOutstanding( ) { return false; } - const reportNameValuePair = (reportNameValuePairs ?? allReportNameValuePair)?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport.reportID}`]; - if (isArchivedReport(reportNameValuePair)) { + const resolvedReportNameValuePair = reportNameValuePair ?? allReportNameValuePair?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport.reportID}`]; + if (isArchivedReport(resolvedReportNameValuePair)) { return false; } const currentRoute = navigationRef.getCurrentRoute(); @@ -12100,7 +12101,7 @@ function getOutstandingReportsForUser( return Object.values(reports).filter( (report) => report?.pendingFields?.preview !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && - isReportOutstanding(report, policyID, reportNameValuePairs, allowSubmitted) && + isReportOutstanding(report, policyID, reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`], allowSubmitted) && report?.ownerAccountID === reportOwnerAccountID, ); } diff --git a/src/pages/Share/SubmitDetailsPage.tsx b/src/pages/Share/SubmitDetailsPage.tsx index 0e5269435a7f..802cf2bc6974 100644 --- a/src/pages/Share/SubmitDetailsPage.tsx +++ b/src/pages/Share/SubmitDetailsPage.tsx @@ -95,6 +95,7 @@ function SubmitDetailsPage({ const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`); const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`); const transactionReport = useReportOrReportDraft(transaction?.reportID); + const [reportNameValuePair] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(transactionReport?.reportID)}`); const iouType = isSelfDM(report) ? CONST.IOU.TYPE.TRACK : CONST.IOU.TYPE.SUBMIT; // Self-DM's FAKE policyID can't load real policy data — usePolicyForTransaction resolves the active workspace instead. const {policy} = usePolicyForTransaction({ @@ -266,7 +267,7 @@ function SubmitDetailsPage({ const [storedTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(existingTransactionID)}`); const listOfParticipants = participants.filter((participant) => participant.selected); const participant = listOfParticipants.at(0) ?? selectedParticipants.at(0); - const reportToSubmit = resolveReportForMoneyRequest({transaction, transactionReport, routeReport: report, policy}); + const reportToSubmit = resolveReportForMoneyRequest({transaction, transactionReport, routeReport: report, policy, reportNameValuePair}); const postSubmitNavigationReportID = (isSelfDM(report) ? report : reportToSubmit)?.reportID ?? reportOrAccountID; const isIouReport = isMoneyRequestReport(reportToSubmit); const policyTagsForRequestMoney = useMoneyRequestPolicyTags({ diff --git a/src/pages/iou/request/step/DynamicIOURequestStepReport.tsx b/src/pages/iou/request/step/DynamicIOURequestStepReport.tsx index fcb822ed6927..e521185b1b09 100644 --- a/src/pages/iou/request/step/DynamicIOURequestStepReport.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestStepReport.tsx @@ -14,6 +14,7 @@ import useRestartOnReceiptFailure from '@hooks/useRestartOnReceiptFailure'; import useShowNotFoundPageInIOUStep from '@hooks/useShowNotFoundPageInIOUStep'; import {createNewReport} from '@libs/actions/Report'; +import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/createDynamicRoute'; import Navigation from '@libs/Navigation/Navigation'; import {getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils'; @@ -58,9 +59,10 @@ function DynamicIOURequestStepReport({route, transaction}: DynamicIOURequestStep const backPath = useDynamicBackPath(DYNAMIC_ROUTES.MONEY_REQUEST_STEP_REPORT.path); const isUnreported = transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID; const [transactionReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transaction?.reportID}`); + const [reportNameValuePair] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(transaction?.reportID)}`); const participantReportID = transaction?.participants?.at(0)?.reportID; const [participantReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${participantReportID}`); - const shouldUseTransactionReport = (!!transactionReport && isReportOutstanding(transactionReport, transactionReport?.policyID)) || isUnreported; + const shouldUseTransactionReport = (!!transactionReport && isReportOutstanding(transactionReport, transactionReport?.policyID, reportNameValuePair)) || isUnreported; const outstandingReportID = isPolicyExpenseChat(participantReport) ? participantReport?.iouReportID : participantReportID; const selectedReportID = shouldUseTransactionReport ? transactionReport?.reportID : outstandingReportID; const [selectedReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${selectedReportID}`); diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index faa5d9f81ef7..7a110e50dc8c 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -190,6 +190,7 @@ function IOURequestStepConfirmation({ const [policyDraft] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${draftPolicyID}`); const [policyReal] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${realPolicyID}`); const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID); + const [reportNameValuePair] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(transaction?.reportID)}`); const expensifyIcons = useMemoizedLazyExpensifyIcons(['ReplaceReceipt', 'SmartScan']); @@ -209,8 +210,9 @@ function IOURequestStepConfirmation({ transactionReport, routeReport: reportWithDraftFallback, policy: policyReal, + reportNameValuePair, }), - [transaction, transactionReport, reportWithDraftFallback, policyReal], + [transaction, transactionReport, reportWithDraftFallback, policyReal, reportNameValuePair], ); const [reportDrafts] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT); diff --git a/tests/unit/IOUUtilsTest.ts b/tests/unit/IOUUtilsTest.ts index 21f48d8232fa..8e4f2e6e9843 100644 --- a/tests/unit/IOUUtilsTest.ts +++ b/tests/unit/IOUUtilsTest.ts @@ -15,7 +15,7 @@ import * as TransactionUtils from '@src/libs/TransactionUtils'; import {hasAnyTransactionWithoutRTERViolation} from '@src/libs/TransactionUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; -import type {Policy, Report, ReportMetadata, Transaction, TransactionViolations} from '@src/types/onyx'; +import type {Policy, Report, ReportMetadata, ReportNameValuePairs, Transaction, TransactionViolations} from '@src/types/onyx'; import type {OnyxCollection} from 'react-native-onyx'; @@ -919,6 +919,7 @@ describe('getExistingTransactionID', () => { describe('resolveReportForMoneyRequest', () => { const policyForResolve: Policy = {...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM, 'Resolve Test Policy'), id: 'resolve-policy'}; + const nonArchivedReportNameValuePair: ReportNameValuePairs = {}; const makeOutstandingReport = (reportID: string): Report => ({ ...createRandomReport(Number(reportID), undefined), @@ -942,14 +943,39 @@ describe('getExistingTransactionID', () => { const transaction = makeTransaction(CONST.REPORT.UNREPORTED_REPORT_ID); const transactionReport = makeOutstandingReport('500'); const routeReport = makeRouteReport('100'); - expect(IOUUtils.resolveReportForMoneyRequest({transaction, transactionReport, routeReport, policy: policyForResolve})).toBeUndefined(); + expect( + IOUUtils.resolveReportForMoneyRequest({ + transaction, + transactionReport, + routeReport, + policy: policyForResolve, + reportNameValuePair: nonArchivedReportNameValuePair, + }), + ).toBeUndefined(); }); it('returns the picked report when it is outstanding (user-selected report wins)', () => { const transaction = makeTransaction('500'); const transactionReport = makeOutstandingReport('500'); const routeReport = makeRouteReport('100'); - expect(IOUUtils.resolveReportForMoneyRequest({transaction, transactionReport, routeReport, policy: policyForResolve})?.reportID).toBe('500'); + expect( + IOUUtils.resolveReportForMoneyRequest({ + transaction, + transactionReport, + routeReport, + policy: policyForResolve, + reportNameValuePair: nonArchivedReportNameValuePair, + })?.reportID, + ).toBe('500'); + }); + + it('returns undefined when the picked report is archived', () => { + const transaction = makeTransaction('500'); + const transactionReport = makeOutstandingReport('500'); + const routeReport = makeRouteReport('100'); + const reportNameValuePair: ReportNameValuePairs = {private_isArchived: testDate}; + + expect(IOUUtils.resolveReportForMoneyRequest({transaction, transactionReport, routeReport, policy: policyForResolve, reportNameValuePair})).toBeUndefined(); }); it('returns undefined when the picked report is non-outstanding and differs from the route (forces a new optimistic IOU)', () => { @@ -959,20 +985,44 @@ describe('getExistingTransactionID', () => { policyID: 'someOtherPolicy', }; const routeReport = makeRouteReport('100'); - expect(IOUUtils.resolveReportForMoneyRequest({transaction, transactionReport: nonOutstandingPick, routeReport, policy: policyForResolve})).toBeUndefined(); + expect( + IOUUtils.resolveReportForMoneyRequest({ + transaction, + transactionReport: nonOutstandingPick, + routeReport, + policy: policyForResolve, + reportNameValuePair: nonArchivedReportNameValuePair, + }), + ).toBeUndefined(); }); it('returns the route report when no different transaction report has been picked', () => { const transaction = makeTransaction('100'); const transactionReport = makeRouteReport('100'); const routeReport = makeRouteReport('100'); - expect(IOUUtils.resolveReportForMoneyRequest({transaction, transactionReport, routeReport, policy: policyForResolve})?.reportID).toBe('100'); + expect( + IOUUtils.resolveReportForMoneyRequest({ + transaction, + transactionReport, + routeReport, + policy: policyForResolve, + reportNameValuePair: nonArchivedReportNameValuePair, + })?.reportID, + ).toBe('100'); }); it('falls back to the transaction report when no route report exists (the !routeReport branch)', () => { const transaction = makeTransaction('500'); const transactionReport = makeOutstandingReport('500'); - expect(IOUUtils.resolveReportForMoneyRequest({transaction, transactionReport, routeReport: undefined, policy: policyForResolve})?.reportID).toBe('500'); + expect( + IOUUtils.resolveReportForMoneyRequest({ + transaction, + transactionReport, + routeReport: undefined, + policy: policyForResolve, + reportNameValuePair: nonArchivedReportNameValuePair, + })?.reportID, + ).toBe('500'); }); it('returns undefined when the picked report is processing and policy harvesting is disabled', () => { @@ -984,7 +1034,15 @@ describe('getExistingTransactionID', () => { }; const routeReport = makeRouteReport('100'); const harvestingDisabledPolicy: Policy = {...policyForResolve, harvesting: {enabled: false}}; - expect(IOUUtils.resolveReportForMoneyRequest({transaction, transactionReport: processingPick, routeReport, policy: harvestingDisabledPolicy})).toBeUndefined(); + expect( + IOUUtils.resolveReportForMoneyRequest({ + transaction, + transactionReport: processingPick, + routeReport, + policy: harvestingDisabledPolicy, + reportNameValuePair: nonArchivedReportNameValuePair, + }), + ).toBeUndefined(); }); }); diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 626b2589af95..ada47afb937c 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -10141,8 +10141,8 @@ describe('ReportUtils', () => { statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, }; - const reportNameValuePairs = {[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]: {private_isArchived: '2024-01-01 00:00:00.000'}}; - expect(isReportOutstanding(report, policy.id, reportNameValuePairs)).toBe(false); + const reportNameValuePair = {private_isArchived: '2024-01-01 00:00:00.000'}; + expect(isReportOutstanding(report, policy.id, reportNameValuePair)).toBe(false); }); });