Skip to content

Fix duplicate distance rates after copying workspace settings - #99678

Draft
neil-marcellini wants to merge 1 commit into
mainfrom
neil/fix-copy-settings-duplicate-distance-rates
Draft

Fix duplicate distance rates after copying workspace settings#99678
neil-marcellini wants to merge 1 commit into
mainfrom
neil/fix-copy-settings-duplicate-distance-rates

Conversation

@neil-marcellini

Copy link
Copy Markdown
Contributor

Explanation of Change

(Neil's AI agent)

Copying distance rates (or per diem rates) between workspaces made every copied rate render twice.

The optimistic patch wrote the source unit's rates verbatim, so they landed keyed by the source's customUnitRateIDs. Auth's CopyPolicySettings persists those rates under the target's existing rate IDs where a rate of the same name is already there, and under freshly minted IDs otherwise, then pushes customUnits to Onyx as a merge keyed by rate ID. Nothing removed the optimistic entries, so the client ended up holding both sets. Since OpenApp also sends policies as a collection merge, the orphaned rates stuck around across reloads.

This change makes the optimistic patch mirror what Auth actually persists:

  • A source rate whose name matches a rate already on the target is written under the target's rate ID, with the source's values — the copy now updates that rate in place, exactly as the backend does.
  • Source rates the target has no same-named rate for are left out, and so is a whole unit the target doesn't have yet. Only Auth knows the IDs those will land under, so guessing one is what created the duplicate; they appear when the server push arrives.

As a side effect the target's own rates that the copy replaces now disappear optimistically instead of lingering, which also matches the backend outcome.

Not covered here: other admins viewing the same target policy still keep rates that the copy removed, because the Auth merge doesn't null out the pre-copy rate IDs (the way it already does for coding rules). That's a backend fix and I'd raise it separately if we want it.

Fixed Issues

$ #99285
PROPOSAL:

Tests

(Neil's AI agent)

  1. Create a USD workspace and enable Distance rates.
  2. Go to Distance rates > Settings and enable "Auto-update government rates", so the workspace holds the two US government rates (one expired, one active) alongside its default rate.
  3. Create a second workspace with a non-USD output currency and enable Distance rates on it.
  4. Go back to the workspace list and click the 3-dot menu on the USD workspace > Copy settings.
  5. Select the non-USD workspace > Next.
  6. Select Distance rates > Next > Copy settings, and wait for the copy to finish.
  7. Open the non-USD workspace > Distance rates and verify each rate from the USD workspace appears exactly once.
  8. Reload the page and verify the rate list is unchanged — still one entry per rate.
  9. Repeat steps 4-6 with Per diem selected instead, and verify the copied per diem rates each appear once.
  10. Copy Distance rates onto a workspace that does not have Distance rates enabled, and verify the rates show up once the copy completes (they arrive with the server response rather than instantly).
  • Verify that no errors appear in the JS console

Offline tests

(Neil's AI agent)

Copy settings requires a round trip to the server, so it can't complete offline.

  1. Go offline.
  2. Open the 3-dot menu on a workspace > Copy settings, pick a target, select Distance rates and confirm.
  3. Verify the copy stays in its loading state and no duplicate rates are written to the target workspace.
  4. Go back online and verify the copy completes with each rate appearing exactly once on the target.

QA Steps

(Neil's AI agent)

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 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 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

Copy settings wrote the source's rates under the source's own customUnitRateIDs,
while Auth persists them under the target's existing rate IDs (matched by name)
or freshly minted ones. The server push is an Onyx merge keyed by rate ID, so the
optimistic entries survived alongside it and every copied rate rendered twice.

The optimistic patch now reuses the target's rate ID whenever a rate of the same
name exists there, and leaves out rates - and whole units - the target doesn't
have, since only Auth can know the IDs those will land under.
@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Coverage Δ
src/libs/actions/Policy/CopyPolicySettings.ts 80.00% <95.00%> (+0.10%) ⬆️
... and 166 files with indirect coverage changes

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.

1 participant