App icon indicating copy to clipboard operation
App copied to clipboard

[$175] Duplicating an expense should stay on the source report when that report can accept it

Open melvin-bot[bot] opened this issue 2 months ago • 30 comments

Problem

Duplicating an expense ignores the report you are looking at. The duplicate is routed to your default workspace, then to whatever report that workspace chat's iouReportID points at — usually the most recently created report from that chat. If you duplicate an expense while viewing an open, editable report, the copy can land on a different report or on a brand-new one.

That is surprising. People duplicate an expense to get a second copy here, next to the original.

Current behavior

  • useExpenseActions.ts builds the duplicate with targetPolicy: defaultExpensePolicy and targetReport: getPolicyExpenseChat(accountID, defaultExpensePolicy.id) — the default expense policy, not the source expense's policy.
  • Duplicate.ts passes that workspace chat to requestMoney, and MoneyRequestBuilder.ts resolves the target as allReports[`report_${chatReport.iouReportID}`] — one pointer, no list, no sorting. With two draft reports on the workspace, the second is never considered.
  • shouldCreateNewMoneyRequestReport then forces a new report when that pointer is missing, pending delete, or cannot accept transactions — so a third draft can appear instead of reusing an addable one.

Proposed behavior

When the source expense is already on a report and that report can accept the duplicate, target the source report. Fall back to today's default-workspace routing only when it cannot.

Roughly: pass the source expense's reportID (and its policy) as the explicit target when the source report is open or otherwise addable — reuse the same "can this report accept a transaction" check shouldCreateNewMoneyRequestReport already relies on, instead of adding a second definition.

Cases to cover

  • Source report is submitted, approved, closed, or pending delete → keep current behavior, do not reopen a locked report.
  • Source expense has no report (self-DM / tracked) → stays unreported, as today.
  • Source report belongs to a workspace other than your default → the duplicate should follow the source report, not jump workspaces.

Related

  • Expensify/App#99395 — new expense lands on a new report instead of the existing draft (manual-create path, same routing gap).

Reported in Slack.

Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @thelullabyy

melvin-bot[bot] avatar Sep 16 '26 12:09 melvin-bot[bot]

Triggered auto assignment to Contributor-plus team member for initial proposal review - @thelullabyy (External)

melvin-bot[bot] avatar Sep 16 '26 13:09 melvin-bot[bot]

Unable to auto-create job on Upwork. The BZ team member should create it manually for this issue.

melvin-bot[bot] avatar Sep 16 '26 13:09 melvin-bot[bot]

@thelullabyy Eep! 4 days overdue now. Issues have feelings too...

melvin-bot[bot] avatar Sep 21 '26 21:09 melvin-bot[bot]

@thelullabyy Still overdue 6 days?! Let's take care of this!

melvin-bot[bot] avatar Sep 23 '26 21:09 melvin-bot[bot]

@thelullabyy Now this issue is 8 days overdue. Are you sure this should be a Daily? Feel free to change it!

melvin-bot[bot] avatar Sep 25 '26 21:09 melvin-bot[bot]

@MelvinBot Please investigate and drop proposal

thelullabyy avatar Sep 27 '26 17:09 thelullabyy

Proposal

Please re-state the problem that we are trying to solve in this issue.

Duplicating an expense while viewing an open expense report puts the copy on a different report, or a new one. It should stay on the report you are viewing.

What is the root cause of that problem?

Both "Duplicate expense" entry points always target the default workspace chat, never the source report:

When the target is a chat, MoneyRequestBuilder.ts:1421-1432 picks whatever chatReport.iouReportID points to. If that report can't take the expense, shouldCreateNewMoneyRequestReport creates a new one.

The action layer already supports a specific report. If report is an expense report, requestMoney sets moneyRequestReportID to it (TrackExpense.ts:1741-1743). The distance and per diem paths do the same. This is how "Add expense" inside a report works. The hooks just never pass the source report.

What changes do you think we should make in order to solve the problem?

Choose the target in the two hooks. Duplicate.ts needs no changes.

  1. Add canDuplicateIntoSourceReport to useExpenseActions and MoneyRequestHeaderSecondaryActions. It is true when all of these hold:

    • the source report is an expense report and is not archived
    • its chat report is loaded and not archived
    • its policy is accessible (the same check "Duplicate report" uses)
    • !shouldCreateNewMoneyRequestReport(sourceReport, chatReport, false, isASAPSubmitBetaEnabled, rules, CONST.IOU.ACTION.CREATE, true)

    Reusing shouldCreateNewMoneyRequestReport (it wraps canAddTransaction) means the hook and the builder always agree. No second "addable" definition is needed.

  2. If true, use the source report and its policy as the target. Otherwise keep the default workspace chat and default policy.

  3. Point everything that depends on the target at the chosen policy and report: categories, tags, policyTagList, participants, and billing restriction checks.

  4. Compare the per diem cross-workspace block (useExpenseActions.ts:230) against the target policy, not the default one.

  5. Close the menu when the copy lands on the viewed report: OR the new flag into shouldDuplicateCloseModalOnSelect.

This covers each case in the issue:

  • Submitted, approved, closed, or pending-delete source: the check fails, so routing stays as it is today.
  • Self-DM or tracked source: there is no expense report, so the copy stays unreported through trackExpense.
  • Source on a non-default workspace: the copy follows the source report and policy.

Tests:

  • In tests/actions/IOUTest/DuplicateTest.ts, pass an open expense report as targetReport. Assert that REQUEST_MONEY uses that report's iouReportID.
  • Add a useExpenseActions hook test with Duplicate mocked. An open source report should target itself. An approved source report should fall back to the default chat.

What alternative solutions did you explore? (Optional)

  • Pass existingIOUReport instead: this skips the one-to-two-expenses handling that requestMoney runs when it gets a report.
  • Apply the same routing to bulk duplicate in the report selection toolbar: its iouReportID patching in Duplicate.ts assumes the target is a chat, so it's better handled as a follow-up.
Related issue

[Due for payment 2026-09-24] [Exploratory] Expense report- Create new expense added to new report instead of current draft report (closed) has the same routing gap in the manual create path.


Next Steps for Contributor+ team: To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/101327) to create a draft PR. To refine: @MelvinBot <your feedback> To reject: Explain why you are rejecting Melvin's proposal.


view run

MelvinBot avatar Sep 27 '26 17:09 MelvinBot

@thelullabyy Whoops! This issue is 2 days overdue. Let's get this updated quick!

melvin-bot[bot] avatar Sep 30 '26 21:09 melvin-bot[bot]

@thelullabyy Eep! 4 days overdue now. Issues have feelings too...

melvin-bot[bot] avatar Oct 02 '26 23:10 melvin-bot[bot]

@thelullabyy Huh... This is 4 days overdue. Who can take care of this?

melvin-bot[bot] avatar Oct 03 '26 01:10 melvin-bot[bot]

@MelvinBot Please open PR based on your proposal. Let's double check again if any other changes needed as well

thelullabyy avatar Oct 05 '26 17:10 thelullabyy

🤖 Draft PR created: Keep duplicated expenses on the source report when it can accept them Please mark it as "Ready for review" when it is ready for review.

The PR follows the proposal. I checked again for other changes and found two small ones beyond the proposal:

  • Shared check: both entry points need the same "can the source report take the copy" rule. I put it in one helper, canDuplicateExpenseIntoSourceReport in ReportUtils, instead of copying it into each hook. It wraps shouldCreateNewMoneyRequestReport.
  • Thread header categories and tags: MoneyRequestHeaderSecondaryActions subscribed to the default workspace's categories and tags. It now subscribes to the target workspace's.

I confirmed no other changes are needed:

  • The distance path (createDistanceRequest) and the per diem path (submitPerDiemExpense) already add to a given expense report. So Duplicate.ts stays unchanged.
  • Visibility of the duplicate action in ReportSecondaryActionUtils doesn't depend on the target.
  • Bulk duplicate (useBulkDuplicateAction) still uses the default workspace chat. It's left for a follow-up, as the proposal says.
Local checks
  • typecheck, lint-changed, spell-changed, and oxfmt passed.
  • The React Compiler check passed on both changed files.
  • Jest: 1,532 tests passed across ReportUtilsTest, DuplicateTest, and the related header and hook suites, including the new tests.
  • The automated web UI check didn't run. The test browser crashed during onboarding, before reaching the duplicate flow. A manual test is still needed.

view run · no recording available

MelvinBot avatar Oct 05 '26 18:10 MelvinBot

Job added to Upwork: https://www.upwork.com/jobs/~022108295391114710662

melvin-bot[bot] avatar Oct 08 '26 20:10 melvin-bot[bot]

Triggered auto assignment to @francoisl, see https://stackoverflow.com/c/expensify/questions/7972 for more details.

melvin-bot[bot] avatar Oct 08 '26 20:10 melvin-bot[bot]

Proposal

Please re-state the problem that we are trying to solve in this issue.

Duplicating an expense always sends the copy to the default workspace's latest report (or a new one), even when the expense's own report is open and could take it.

What is the root cause of that problem?

Each duplicate entry point picks its own target, and all three hard-code the default workspace chat:

  • the report header: useExpenseActions.ts#L282
  • the expense thread header: MoneyRequestHeaderSecondaryActions.tsx#L298
  • bulk duplicate: useBulkDuplicateAction.ts#L94

duplicateExpenseTransaction then hands that chat to the create flow, which uses whatever report chatReport.iouReportID points to, or makes a new one. Nothing ever looks at where the source expense lives. Because the choice is copied across three callers, fixing only some of them would leave bulk duplicate behaving differently from single duplicate.

The create flow already supports adding to a specific expense report. It's what "Add expense" inside a report uses. The callers just never pass the source report.

What changes do you think we should make in order to solve the problem?

  1. One pure helper for "where should this copy go?" Add getDuplicateExpenseTarget({sourceReport, sourceChatReport, sourcePolicy, defaultPolicy, defaultPolicyExpenseChat, …}) next to the other duplicate helpers. It returns {targetReport, targetPolicy}:

    • Source report is an expense report that can take the copy: return the source report and its policy. "Can take it" means the report and its chat aren't archived, the policy is accessible, and shouldCreateNewMoneyRequestReport(…) returns false, so it reuses the existing rule instead of defining a second one.
    • Source report is submitted, approved, closed or pending delete: return today's default workspace target.
    • Unreported or Self DM expense: return today's behavior, so the copy stays unreported.

    The helper only takes data as arguments, which makes it easy to unit test.

  2. Use it in all three entry points, including bulk duplicate. For bulk, work out the target for each selected expense. Selections from a single report then all land on that report, and a selection from Search that spans reports sends each copy to its own source report.

  3. Use the target workspace's categories and tags, not the default workspace's. Otherwise a copy on another workspace's report gets checked against the wrong category and tag lists.

  4. No changes to Duplicate.ts, which already accepts any target report.

  5. Tests:

    • helper cases: open report, submitted report, approved report, archived report, pending-delete report, a report on another workspace, and an unreported expense
    • a bulk duplicate test where expenses from two open reports each land on their own report

What alternative solutions did you explore? (Optional)

Changing the target only in the two single-expense headers. That fixes the reported case, but bulk duplicate would still send every copy to the default workspace. The same action would then put copies in different places depending on how many expenses were selected.

Contributor details Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~010efadac9ff3c3d2a/

voxturrlabs avatar Oct 09 '26 06:10 voxturrlabs

Deploy Blocker https://github.com/Expensify/App/issues/103866 was identified to be related to the PR https://github.com/Expensify/App/pull/103072 that attempted to fix this issue.

nlemma avatar Oct 13 '26 11:10 nlemma

⚠️ Looks like this issue was linked to a Deploy Blocker here

If you are the assigned CME please investigate whether the linked PR caused a regression and leave a comment with the results.

If a regression has occurred and you are the assigned CM follow the instructions here.

If this regression could have been avoided please consider also proposing a recommendation to the PR checklist so that we can avoid it in the future.

melvin-bot[bot] avatar Oct 13 '26 12:10 melvin-bot[bot]

@thelullabyy

The solution for this issue has been :rocket: deployed to production :rocket: in version 9.5.8-5 and is now subject to a 7-day regression period :calendar:. Here is the list of pull requests that resolve this issue:

  • https://github.com/Expensify/App/pull/103072

If no regressions arise, payment will be issued on 2026-10-21. :confetti_ball:

The following checklist (instructions) will need to be completed before the issue can be closed. Please copy/paste the Contributor+ Checklist from here into a new comment on this GH and complete it. If you have the K2 extension, you can simply click: [this button]. If no checklist is needed for this issue, you can click: [no checklist button]

melvin-bot[bot] avatar Oct 14 '26 13:10 melvin-bot[bot]

@thelullabyy Whoops! This issue is 2 days overdue. Let's get this updated quick!

melvin-bot[bot] avatar Oct 16 '26 21:10 melvin-bot[bot]

Triggered auto assignment to @mallenexpensify (Awaiting Payment)

melvin-bot[bot] avatar Oct 20 '26 21:10 melvin-bot[bot]