Skip to content

refactor trackExpense function to use useOnyx data - #95570

Closed
daledah wants to merge 29 commits into
Expensify:mainfrom
daledah:refactor/66414-4
Closed

daledah wants to merge 29 commits into
Expensify:mainfrom
daledah:refactor/66414-4

Conversation

@daledah

@daledah daledah commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Fixed Issues

$ #66414
PROPOSAL:

Tests

  1. Open Self-DM report
  2. Click the "+" button → select "Create expense"
  3. Enter an amount → click Next
  4. Fill in merchant/description → click Create expense
  5. Verify that: the expense is created successfully in the Self-DM
  6. Click More dropdown and select "duplicated expense"
  7. Verify that: a duplicated expense is created in workspace chat
  • Verify that no errors appear in the JS console

Offline tests

Same as tests

QA Steps

Same as tests

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
Screen.Recording.2026-07-09.at.10.20.02.mov

@daledah daledah changed the title refactor duplicateExpenseTransaction and createTransaction to use use… refactor trackExpense function to use useOnyx data Jul 8, 2026
@codecov

codecov Bot commented Jul 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
src/hooks/useBulkDuplicateAction.ts 100.00% <100.00%> (ø)
src/hooks/useExpenseActions.ts 67.13% <100.00%> (+1.09%) ⬆️
src/libs/IOUAmountSubmission.ts 86.70% <ø> (ø)
src/libs/ReportUtils.ts 87.16% <ø> (-0.05%) ⬇️
src/libs/actions/IOU/Duplicate.ts 91.94% <ø> (ø)
src/libs/actions/IOU/MoneyRequest.ts 76.15% <ø> (+0.33%) ⬆️
src/libs/actions/IOU/TrackExpense.ts 84.84% <100.00%> (+0.11%) ⬆️
...quest/step/DynamicIOURequestStepDistanceManual.tsx 2.20% <ø> (ø)
...stance/handleMoneyRequestStepDistanceNavigation.ts 83.33% <ø> (ø)
...RequestStepDistance/hooks/useDistanceNavigation.ts 91.30% <ø> (ø)
... and 6 more
... and 8 files with indirect coverage changes

@daledah
daledah marked this pull request as ready for review July 9, 2026 03:26
@daledah
daledah requested review from a team as code owners July 9, 2026 03:26
@melvin-bot
melvin-bot Bot requested review from DylanDylann and removed request for a team July 9, 2026 03:26
@melvin-bot

melvin-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

@DylanDylann Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot requested review from trjExpensify and removed request for a team July 9, 2026 03:26
@daledah

daledah commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

the failing test is not related to this PR and will merge main to fix soon

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bf2bbc3d83

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/libs/actions/IOU/TrackExpense.ts Resolved
@trjExpensify
trjExpensify removed their request for review July 9, 2026 15:02
@trjExpensify

Copy link
Copy Markdown
Contributor

PR doesn’t need product input as a refactor PR. Unassigning and unsubscribing myself.

@DylanDylann

Copy link
Copy Markdown
Contributor

@daledah Could you fix the failed check?

@daledah

daledah commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@DylanDylann i resolved conflict and the failed check

Comment thread src/hooks/useExpenseActions.ts Resolved
@daledah

daledah commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

@DylanDylann i updated, please check again

@daledah

daledah commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

In most cases, isDraftReport should be computed from chatReport.reportID in the line below. Even though we reassign chatReport afterward, in those cases, we can assume isDraftReport is false, since it will be a DM report.

let chatReport = !isEmptyObject(parentChatReport) && parentChatReport?.reportID ? parentChatReport : null;

Currently, to compute isDraftReport, we'd need to recompute the line above at the component level. My suggestion is to move this logic entirely to the component level and pass chatReport down as a param, so we avoid computing that line twice. Wdyt? Feel free to give your idea before updating

The catch is that chatReport inside getTrackExpenseInformation isn't just parentChatReport - it also falls back to (and optimistically creates) the self-DM report, and that optimistic creation has to stay in the action. So we can't fully resolve chatReport at the component level without moving the self-DM creation up too

if (!chatReport) {
const currentTime = DateUtils.getDBTime();
const selfDMReport = buildOptimisticSelfDMReport(currentTime, optimisticChatReportID);
const selfDMCreatedReportAction = buildOptimisticCreatedReportAction({emailCreatingAction: currentUserEmailParam, created: currentTime});
optimisticReportID = selfDMReport.reportID;
optimisticReportActionID = selfDMCreatedReportAction.reportActionID;
chatReport = selfDMReport;

But your goal is already met: since isDraftReport only needs the initial chatReport (= parentChatReport), and the component already computes isDraftChatReport from exactly that report. So we're not computing it twice. I'd keep isDraftChatReport as a param. Wdyt?

@DylanDylann

Copy link
Copy Markdown
Contributor

@daledah Don't worry about the self-DM report case, because self-DMs don't have a draft report, so it will always be false

@DylanDylann

Copy link
Copy Markdown
Contributor

The only logic I suggest moving to the component level is


let chatReport = !isEmptyObject(parentChatReport) && parentChatReport?.reportID ? parentChatReport : null;

@daledah

daledah commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@DylanDylann I removed that line from the action - it now just trusts the passed report. This is equivalent for every caller (none pass an empty/report-less object), so the!isEmptyObject(...) && parentChatReport?.reportID guard was redundant here.

Comment thread src/libs/actions/IOU/TrackExpense.ts Outdated
Comment on lines 2399 to 2401
report,
parentChatReport: parentChatReportParam,
action,

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.

COuld we remove report param?

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.

I resolved - 5397500

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.

report and parentChatReport look duplicated. I mean, we should remove report param

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.

@DylanDylann They look duplicated in most callers, but they're not the same thing. parentChatReport is the (already-resolved) parent chat report, while report is the source report - which can be a money request/expense report (e.g. in the categorize/share/move flows via useExpenseSubmission). We still need report to compute isMoneyRequestReport / moneyRequestReportID, ... deriving those from parentChatReport (always a chat report) would make moneyRequestReportID empty and the expense would no longer be added to the existing money request report.

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.

In the useExpenseSubmission case, isMoneyRequestReport should be checked via parentChatReport. First, could you explain why you pass parentChatReport: isSelfDMDestination ? selfDMReport : currentChatReport in useExpenseSubmission?

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.

@DylanDylann i checked again, report is redundant, so I've removed it.

could you explain why you pass parentChatReport: isSelfDMDestination ? selfDMReport : currentChatReport

parentChatReport is the chat the expense attaches to, so it must always be a chat: for a self-DM destination we force the self-DM, otherwise currentChatReport (the report itself, or its parent chat if report is a money-request report). It always resolves to a chat, so isMoneyRequestReport is always false here - that's why dropping report is safe.

@DylanDylann

Copy link
Copy Markdown
Contributor

@daledah Could you please review the PR again? You marked some of my previous comments as resolved, but I still see them in the codebase. Please only resolve a discussion once it's actually done

@daledah

daledah commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

@DylanDylann i updated, please check again

@daledah

daledah commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

@DylanDylann i updated, please check again

@DylanDylann

Copy link
Copy Markdown
Contributor

@daledah replied #95570 (comment), let me know when the PR is ready

@DylanDylann

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d94b1d40ef

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +181 to +182
const targetChatForDuplicate = isSourcePolicyValid ? chatReport : activePolicyExpenseChat;
const [isDraftChatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${targetChatForDuplicate?.reportID}`, {selector: isDraftReportSelector});

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 Subscribe to the expense duplicate's target chat

When the source expense belongs to an accessible policy other than the default expense policy, this subscribes to the source chatReport draft state, but duplicateExpenseTransaction targets activePolicyExpenseChat with defaultExpensePolicy and forwards this boolean as its draft state. Consequently, duplicating into a draft default workspace can treat it as non-draft and omit the workspace-creation Onyx/API data; use the active policy expense chat's draft state for the single-expense duplicate path, separately from the report-duplicate target.

Useful? React with 👍 / 👎.

@daledah

daledah commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

@DylanDylann The scope of this PR is quite complex and difficult to test and review, so I think we should split it into multiple PRs. I'll close this PR, and we have another PR open here

@daledah daledah closed this Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants