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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 5 additions & 2 deletions src/libs/IOUUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -553,16 +553,19 @@ function resolveReportForMoneyRequest({
transactionReport,
routeReport,
policy,
reportNameValuePair,
}: {
transaction: OnyxEntry<Transaction>;
transactionReport: OnyxEntry<Report>;
routeReport: OnyxEntry<Report>;
policy: OnyxEntry<Policy>;
reportNameValuePair: OnyxInputOrEntry<ReportNameValuePairs>;
}): OnyxEntry<Report> {
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);
Comment on lines +568 to 569

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Don’t reuse archived reports when no route report exists

When an archived transaction report is passed in from submit/share flows without a routeReport, the archive check here is ignored: isReportOutstanding(..., reportNameValuePair, false) returns false, but the next line still makes shouldUseTransactionReport true because !routeReport. That means these flows can still submit to the archived report instead of forcing a new/open report, which undermines the new archived-report rejection this helper is now responsible for.

Useful? React with 👍 / 👎.

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.

@MelvinBot It seems this problem was already happening before our change. Could you verify again?

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.

You're right — this pre-dates the PR. ✅

The behavior codex flagged is driven entirely by the || !routeReport clause in shouldUseTransactionReport, and that line is unchanged by this PR. It's byte-for-byte identical on main:

// origin/main (before this PR) — IOUUtils.ts:566
const canUseTransactionReport = !(isProcessingReport(transactionReport) && !policy?.harvesting?.enabled) && isReportOutstanding(transactionReport, policy?.id, undefined, false);
const shouldUseTransactionReport = !!transactionReport && (canUseTransactionReport || !routeReport);

The only change this PR makes to resolveReportForMoneyRequest is passing reportNameValuePair into isReportOutstanding instead of undefined — i.e. it only affects canUseTransactionReport. When there's no routeReport, shouldUseTransactionReport short-circuits to true regardless of canUseTransactionReport, so an archived transaction report would be returned in that path both before and after this change. So codex's concern isn't a regression introduced here.

Two things worth noting:

  • If anything, this PR makes canUseTransactionReport more reliable at rejecting archived reports (explicit NVPs vs. the old Onyx.connect() fallback) — it just doesn't change the !routeReport override.
  • Whether that override should be reused for an archived report is a real question, but it's a separate, pre-existing issue and out of scope for this refactor. If you'd like it addressed, it'd be cleaner as a follow-up.
Evidence
  • Before (main edc4aad): IOUUtils.ts:566
  • After (this PR 9b69721): IOUUtils.ts:569
  • The PR diff shows the shouldUseTransactionReport line as unchanged context; the only edited part of this function is isReportOutstanding(..., undefined, false) → isReportOutstanding(..., reportNameValuePair, false).

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.

Re-verified — the conclusion holds: this is pre-existing, not introduced by this PR. ✅

The shouldUseTransactionReport = !!transactionReport && (canUseTransactionReport || !routeReport) line codex flags is byte-for-byte identical on main (before, edc4aad L566 vs. after, 9b69721 L569). This PR's only edit to resolveReportForMoneyRequest is isReportOutstanding(..., undefined, false) → isReportOutstanding(..., reportNameValuePair, false), which touches only canUseTransactionReport — not the !routeReport override.

I also checked the two callers to see if codex's scenario (archived transactionReport + missing routeReport) is actually reachable:

In the normal flow routeReport is populated, so the !routeReport branch only fires in the fallback case — and that behavior is identical to main. So there's no regression here to block on. If we want to stop the archived report from being reused even in the no-route-report fallback, that's a separate, pre-existing improvement best done as a follow-up rather than in this refactor.

if (shouldUseTransactionReport) {
return transactionReport;
Expand Down
13 changes: 7 additions & 6 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -12052,7 +12053,7 @@ function isReportOutstanding(
iouReport: OnyxInputOrEntry<Report>,
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<ReportNameValuePairs>,
reportNameValuePair?: OnyxInputOrEntry<ReportNameValuePairs>,
allowSubmitted = true,
): boolean {
if (
Expand All @@ -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();
Expand Down Expand Up @@ -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,
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Share/SubmitDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`);
Expand Down
4 changes: 3 additions & 1 deletion src/pages/iou/request/step/IOURequestStepConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand All @@ -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);

Expand Down
72 changes: 65 additions & 7 deletions tests/unit/IOUUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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),
Expand All @@ -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)', () => {
Expand All @@ -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', () => {
Expand All @@ -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();
});
});

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
Loading