Skip to content

Preserve distance rate for unreported expenses from non-default policies#87537

Open
MelvinBot wants to merge 4 commits intomainfrom
claude-preserveDistanceRateOnUnreport
Open

Preserve distance rate for unreported expenses from non-default policies#87537
MelvinBot wants to merge 4 commits intomainfrom
claude-preserveDistanceRateOnUnreport

Conversation

@MelvinBot
Copy link
Copy Markdown
Contributor

@MelvinBot MelvinBot commented Apr 9, 2026

Held on https://github.com/Expensify/Web-Expensify/pull/52149

Explanation of Change

When a distance request is unreported from a report tied to a non-default policy, the distance rate was being lost because the unreported expense was associated with the user's default policy (policyForMovingExpenses), which doesn't contain the original customUnitRateID.

This PR applies the same pattern already used for perDiem requests: resolve the policy that owns the customUnitRateID and use it instead of the default policy. Specifically:

  1. PolicyUtils.ts: Added getPolicyByCustomUnitRateID() — searches all policies for one whose distance custom unit contains the transaction's customUnitRateID (mirrors getPolicyByCustomUnitID for perDiem).
  2. MoneyRequestView.tsx: For unreported distance requests, resolve the policy via the new function so the correct rate and policy are passed to the backend.
  3. usePolicyForTransaction.ts: Extended the self-DM expense policy resolution to also handle distance requests by looking up the policy that owns the customUnitRateID.

Fixed Issues

$ #87204
PROPOSAL: #87204 (comment)

Tests

  1. Create a workspace with distance rates enabled (including a rate with tax, e.g. 5%)
  2. Go to self DM
  3. Create a manual distance expense, selecting a rate from the workspace policy
  4. Open the expense
  5. Unreport the expense from a report tied to the workspace
  6. Verify the distance rate is preserved and does not change to the personal default rate
  7. Edit the distance and save
  8. Verify the distance rate remains the one originally selected from the workspace
  • Verify that no errors appear in the JS console

Offline tests

  1. Create a distance expense with a workspace rate while online
  2. Go offline
  3. Unreport the expense
  4. Verify the rate is preserved in the optimistic update
  5. Go back online and verify the rate is still correct after sync

QA Steps

  1. Create a workspace with distance rates enabled and at least one custom rate (e.g. with a tax rate)
  2. Go to self DM and create a manual distance expense using the custom workspace rate
  3. Unreport the expense from a report tied to that workspace
  4. Verify the distance rate is preserved (not changed to personal default)
  5. Edit the distance value and save
  6. Verify the rate remains correct
  • 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 shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I used JaimeGPT to get English > Spanish translation. I then posted it in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • 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)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • 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)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • 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.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • 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-04-16.at.20.20.49.mov

Checks run:

  • prettier — pass (no formatting changes)
  • eslint — pass (pre-existing warning in PolicyUtils.ts line 76 unrelated to this PR)
  • typecheck — pass
  • jest (PolicyUtils, MoneyRequestView tests) — pass (151 tests)

Co-authored-by: cristipaval cristipaval@users.noreply.github.com

When a distance request is unreported from a report tied to a non-default
policy, resolve the policy that owns the customUnitRateID and use it
instead of the default moving-expenses policy. This mirrors the existing
pattern for perDiem requests.

Co-authored-by: Cristi Paval <cristipaval@users.noreply.github.com>
@MelvinBot
Copy link
Copy Markdown
Contributor Author

The failing check is unrelated to this PR.

Failing test: tests/ui/ProfilePageTest.tsx — "shows info when there is an unvalidated secondary login" (expects none-brickRoadIndicator, gets info-brickRoadIndicator)

Analysis: This PR only modifies MoneyRequestView.tsx, usePolicyForTransaction.ts, and PolicyUtils.ts (distance rate preservation logic). The failing test is about the Profile page's contact method brick road indicator — completely unrelated.

Evidence: This appears to be a pre-existing/flaky test failure. The PR's changes have no connection to profile page contact methods or brick road indicators.

@cristipaval
Copy link
Copy Markdown
Contributor

@MelvinBot, could you please merge the latest main into this one?=

Also, we need to pass the policyID to the backend when updating the distance.

MelvinBot and others added 2 commits April 16, 2026 09:08
…ceRateOnUnreport

Co-authored-by: Cristi Paval <cristipaval@users.noreply.github.com>
Add policyID to UpdateMoneyRequestParams and include it in API params
for both getUpdateMoneyRequestParams and getUpdateTrackExpenseParams so
the backend receives the correct policy context when updating distance.

Co-authored-by: cristipaval <cristipaval@users.noreply.github.com>
Co-authored-by: Cristi Paval <cristipaval@users.noreply.github.com>
@MelvinBot
Copy link
Copy Markdown
Contributor Author

Done! I've:

  1. Merged latest main into this branch
  2. Added policyID to the backend params when updating distance — added it to UpdateMoneyRequestParams and included policy?.id in the API params for both getUpdateMoneyRequestParams and getUpdateTrackExpenseParams

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 16, 2026

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
src/hooks/usePolicyForTransaction.ts 100.00% <100.00%> (ø)
src/libs/PolicyUtils.ts 64.50% <100.00%> (+2.45%) ⬆️
src/libs/actions/IOU/UpdateMoneyRequest.ts 61.18% <ø> (ø)
...c/components/ReportActionItem/MoneyRequestView.tsx 65.47% <55.55%> (+0.01%) ⬆️
...es/iou/request/step/IOURequestStepDistanceRate.tsx 0.00% <0.00%> (ø)
... and 433 files with indirect coverage changes

@cristipaval cristipaval changed the title Preserve distance rate for unreported expenses from non-default policies [HOLD] Preserve distance rate for unreported expenses from non-default policies Apr 16, 2026
@cristipaval cristipaval marked this pull request as ready for review April 16, 2026 17:22
@cristipaval cristipaval requested a review from a team as a code owner April 16, 2026 17:22
@melvin-bot melvin-bot Bot requested review from FitseTLT and trjExpensify and removed request for a team April 16, 2026 17:22
@melvin-bot
Copy link
Copy Markdown

melvin-bot Bot commented Apr 16, 2026

@FitseTLT 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 removed the request for review from a team April 16, 2026 17:22
cristipaval
cristipaval previously approved these changes Apr 16, 2026
@cristipaval cristipaval changed the title [HOLD] Preserve distance rate for unreported expenses from non-default policies Preserve distance rate for unreported expenses from non-default policies Apr 17, 2026
@cristipaval
Copy link
Copy Markdown
Contributor

cristipaval commented Apr 17, 2026

HOLD removed, @FitseTLT. This can be tested in staging

@FitseTLT
Copy link
Copy Markdown
Contributor

Ok

trjExpensify
trjExpensify previously approved these changes Apr 17, 2026
Copy link
Copy Markdown
Contributor

@trjExpensify trjExpensify left a comment

Choose a reason for hiding this comment

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

Unreported expense project PR 👍

@MelvinBot
Copy link
Copy Markdown
Contributor Author

⚠️ Something went wrong. This may be a transient issue — try again. If it persists, check status.anthropic.com.

Run details

@cristipaval
Copy link
Copy Markdown
Contributor

bump @FitseTLT

@FitseTLT
Copy link
Copy Markdown
Contributor

@MelvinBot assign me

@MelvinBot
Copy link
Copy Markdown
Contributor Author

📋 Done! @FitseTLT has been added as an assignee to this PR.

@FitseTLT
Copy link
Copy Markdown
Contributor

@MelvinBot BUG:
Unreport a distance request from a non-default workspace
Open the unreported report in self dm and click on the rate field
BUG: you will see the list of rates from that non-default workspace instead of the default workspace

@MelvinBot
Copy link
Copy Markdown
Contributor Author

👋 FitseTLT — confirmed, this is a valid bug introduced by this PR.

Root cause: The policy variable serves a dual purpose — it's used both to display the current rate value (DistanceRequestUtils.getRate() at MoneyRequestView.tsx:469) and to populate the rate picker dropdown (DistanceRequestUtils.getMileageRates(policy) at IOURequestStepDistanceRate.tsx:94).

By resolving policy = distanceOriginalPolicy for unreported distance expenses (in both MoneyRequestView.tsx:219-235 and usePolicyForTransaction.ts:55-65), the PR correctly preserves the displayed rate but also causes the picker to show rates from the non-default workspace instead of the user's default workspace.

To fix: These two concerns need to be decoupled — the rate display should use the original policy (to show the correct rate name/value), but the rate picker should use policyForMovingExpenses (the user's default policy) so users see the correct set of available rates when editing.

@FitseTLT
Copy link
Copy Markdown
Contributor

Yep make sense @MelvinBot

@cristipaval
Copy link
Copy Markdown
Contributor

@MelvinBot, @FitseTLT agreed with your suggested fix for the bug. Could you please implement it?

@cristipaval
Copy link
Copy Markdown
Contributor

@MelvinBot, I see your process was canceled. Could you try again?

The rate picker was showing rates from the original non-default workspace
instead of the user's default workspace for unreported distance expenses.
This decouples the rate display (which correctly uses the original policy)
from the rate picker list (which now uses policyForMovingExpenses).

Co-authored-by: Cristi Paval <cristipaval@users.noreply.github.com>
@MelvinBot MelvinBot dismissed stale reviews from trjExpensify and cristipaval via f7da923 April 21, 2026 08:05
@MelvinBot MelvinBot requested a review from a team as a code owner April 21, 2026 08:05
@melvin-bot melvin-bot Bot requested a review from cristipaval April 21, 2026 08:06
@melvin-bot
Copy link
Copy Markdown

melvin-bot Bot commented Apr 21, 2026

@cristipaval 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 removed the request for review from a team April 21, 2026 08:06
@MelvinBot
Copy link
Copy Markdown
Contributor Author

Fixed! The rate picker now uses policyForMovingExpenses (the user's default policy) to populate the rate list for unreported distance expenses, while policy (from usePolicyForTransaction) continues to be used for displaying the current rate value.

Change: IOURequestStepDistanceRate.tsx — introduced policyForRates which resolves to policyForMovingExpenses for unreported distance expenses, and falls back to policy otherwise. Updated all rate-list, default-rate, tax, and save operations to use policyForRates.

Checks: prettier, eslint, typecheck, and 152 unit tests all pass.

@cristipaval
Copy link
Copy Markdown
Contributor

@FitseTLT, could you please test again? 🙏

@FitseTLT
Copy link
Copy Markdown
Contributor

@cristipaval This pr is making a lot of changes. Like tag, categories etc pages all are using the non-default workspace. I don't think this is a correct behavior. If I understood it correctly the fix is just passing this correct non-default workspace policyID param to the BE when updating distance, right? If it is correct then why don't we make the changes in this scope. 😕

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.

4 participants