Skip to content

Fetch all Home card totals in one grouped Search - #100022

Merged
mountiny merged 7 commits into
Expensify:mainfrom
callstack-internal:2913-home-search-api
Sep 3, 2026
Merged

Fetch all Home card totals in one grouped Search#100022
mountiny merged 7 commits into
Expensify:mainfrom
callstack-internal:2913-home-search-api

Conversation

@adhorodyski

@adhorodyski adhorodyski commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

Home reads every card total from one group-by:card Search.

useYourSpendData sends type:expense from:<accountID> date>=<30 days ago> group-by:card. Each row's {total, currency} comes from that response's group_<cardID> entry, filtered to the cards Home displays. No cardID filter, so the hash holds still as cards come and go. deletePersonalCard drops a card from CARD_LIST optimistically, even offline, and the surviving rows keep the snapshot already loaded.

Home request telemetry, 2026-08-31 10:00–11:00 UTC: 4,333 visits, median 2 Search calls, heaviest 14, eleven of them per-card. fireSearches sends at most three per fire: grouped card, awaiting-approval, repaid-last-30-days.

Not covered: groups come back for cards Home hides, and a newly displayable card gets its row on the next focused fire.

Fixed Issues

$ #99914
PROPOSAL: N/A

Tests

Automated — npx jest tests/unit/HomePage/YourSpendSection/ tests/unit/Search/yourSpendQueryBuildersTest.ts (130 passing). Nine tests cover this section, each driven red first by mutating the implementation:

Test Mutation it catches
costs one search() for a multi-card account, not one per card one request per card → 2 calls, not 1
fires no card search when the account has no displayable cards drop the length > 0 guard
keeps the surviving rows when a card is deleted, without refetching put the card set back in the query → the remaining row loses its total
resolves each card row total from its own group in the one grouped snapshot rows share one group's totals
reads per-card totals from data, so a wipe of the snapshot-level totals cannot drop a row selector gates on search.count
groups by card… / carries no cardID filter… builder drops groupBy / scopes by cardID
resolves from to the numeric accountID / windows the query to the same 30 days builder drops from / widens the window

Manual:

  1. Sign in to an account with two or more cards that have transactions in the last 30 days.
  2. Open DevTools → Network, filter to Search.
  3. Refresh the page on Home. Count the requests: 5 at most, and never more than 5 no matter how many cards the account has — one grouped card query, plus awaiting-approval, repaid-last-30-days, insights and recently-added. The approval and repaid ones only fire for a paid group workspace with payments enabled, so a personal account sees 3.
  4. Click each request → Payload → read the query field. Exactly one carries group-by:card, and no request carries a cardID: filter. All the query strings are distinct — two identical ones is a bug.
  5. Verify every card row in "Your spend" shows the total for that card's last 30 days.
  6. Tap a card row. Verify it opens the Search page filtered to that card, and that the list matches the row's total.
  7. Verify a card with no transactions in the last 30 days renders no row.
  8. Delete one card. Verify its row disappears, every other card row keeps its total, and no new Search fires for the card section.
  • Verify that no errors appear in the JS console

Offline tests

  1. With Home already loaded and card rows visible, go offline.
  2. Verify the card rows keep their last-known totals and neither disappear nor change value.
  3. Delete a card while offline. Verify only that card's row goes and the rest keep their totals.
  4. Navigate away from Home and back while offline. Verify no Search request is attempted and the rows are unchanged.
  5. Go back online. Verify one grouped Search fires and the totals refresh.

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

🤖 Generated with Claude Code

Opening Home fired one Search API call per displayable card. The queries
differed only by cardID, and each row consumed only {count, total, currency}
off the resulting snapshot.

Replace the per-card fan-out with a single `group-by:card` query scoped to the
displayed card set. The response carries a group per card with exactly the
fields a row reads. On the observed 14-request Home visit, 11 of those requests
were per-card; grouping leaves 4.

This also collapses a subscription over the entire SNAPSHOT collection - whose
selector re-ran on every Search-page write - into a single-key read.

The per-card query string stays: it is a card row's tap-through target. It is
just no longer sent to the API.

Drops the per-card wiped-count cache. It existed because tapping a card row
navigated the Search screen onto that card's own snapshot key, where
`shouldCalculateTotals: false` nulled count/total/currency out from under the
Home row. Neither half of that can happen now: the grouped hash is a different
query the Search screen never lands on, and that write only touches `search`,
never the `group_` entries the row now reads.

Fixes callstack-internal/expensify-issues#2913

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@adhorodyski adhorodyski changed the title [No QA] Fetch all Home card totals in one grouped Search Fetch all Home card totals in one grouped Search Sep 1, 2026
@codecov

codecov Bot commented Sep 1, 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/pages/home/YourSpendSection/queries.ts 100.00% <100.00%> (ø)
...rc/pages/home/YourSpendSection/useYourSpendData.ts 97.36% <88.88%> (-0.35%) ⬇️
... and 24 files with indirect coverage changes

adhorodyski and others added 2 commits September 1, 2026 13:50
- Remove three stray `mockedBuildCardGroupQuery.mockImplementation` copies that
  duplicated the `beforeEach` and left the following line mis-indented.
- Drop `displayableCardIDsKey`. The grouped query hash already encodes the card
  set, so key the search effect on it directly.
- Stop mocking `buildCardGroupQuery` in the hook test. The test derives the
  grouped snapshot key from it, so a stand-in could drift from what ships.
- Delete `covers the whole third-party card set with one grouped request`. It
  re-asserted what `costs one search() for a multi-card account` already pins;
  `fireSearches` does not branch on card kind.
- Drop a redundant `toBeDefined` in the hash test.
- Add `refires the grouped search when the displayable card set changes`. The
  effect-key swap above had no coverage; this fails if the card set is dropped
  from the dep list.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
adhorodyski and others added 2 commits September 1, 2026 14:38
@adhorodyski
adhorodyski marked this pull request as ready for review September 1, 2026 13:07
@adhorodyski
adhorodyski requested review from a team as code owners September 1, 2026 13:07
@melvin-bot
melvin-bot Bot requested review from daledah and mountiny September 1, 2026 13:07
@adhorodyski

Copy link
Copy Markdown
Contributor Author

@codex review

@melvin-bot
melvin-bot Bot requested review from dylanexpensify and removed request for a team September 1, 2026 13:07
@melvin-bot

melvin-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

@daledah @mountiny One of you needs to 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 September 1, 2026 13:07
Comment thread tests/unit/HomePage/YourSpendSection/useYourSpendDataTest.ts Outdated

@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: 92cde9960b

ℹ️ 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/pages/home/YourSpendSection/useYourSpendData.ts Outdated
@mountiny
mountiny removed the request for review from daledah September 1, 2026 13:25
Comment thread src/pages/home/YourSpendSection/useYourSpendData.ts
Comment thread src/pages/home/YourSpendSection/useYourSpendData.ts
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions

This comment has been minimized.

@ZhenjaHorbach

ZhenjaHorbach commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible 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 checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (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 verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • 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 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.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this 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.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
2026-09-02.09.49.07.mov
Android: mWeb Chrome
2026-09-02.09.49.07.mov
iOS: HybridApp
2026-09-02.09.44.37.mov
iOS: mWeb Safari
2026-09-02.09.42.57.mov
MacOS: Chrome / Safari
2026-09-02.09.41.53.mov

adhorodyski and others added 2 commits September 1, 2026 18:03
Putting the displayed cardIDs in the query put them in the snapshot hash.
deletePersonalCard merges the card out of CARD_LIST optimistically, moving
Home to a hash nothing has written, so every remaining card row loses its
total, offline until reconnect since fireSearches skips while offline.

Scope the query to from:accountID + groupBy:card and filter groups by card
in the hook. The hash holds still across a card add or delete, so the rows
read the snapshot already loaded.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

@mountiny mountiny left a comment

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.

Great find if this all works well

@JmillsExpensify
JmillsExpensify requested review from JmillsExpensify and removed request for dylanexpensify September 3, 2026 08:34

@JmillsExpensify JmillsExpensify left a comment

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.

Tested the adhoc and saw the same as staging/production.

@mountiny
mountiny merged commit 70059ff into Expensify:main Sep 3, 2026
39 of 41 checks passed
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

OSBotify commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@adhorodyski
adhorodyski deleted the 2913-home-search-api branch September 3, 2026 11:18
@OSBotify

OSBotify commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 9.4.70-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

MelvinBot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

🤖 No help site changes are required for this PR. I did not create a draft docs PR.

This is a data-fetching refactor with no change to documented behavior. It replaces one Search request per card with a single group-by:card request and reads each row's total from that response's group_<cardID> entry.

The only article that covers this surface is Expensify Home Overview, and every statement it makes still holds:

Documented behavior Still true after this PR
Your spend shows Awaiting approval, Repaid last 30 days, and Recent transactions • 1234 rows Yes — row set and labels are untouched
A row appears for each card, with the last four digits of the card number Yes — rows are still built from the same displayable-card list
Selecting a row opens the matching expenses on the Spend page Yes — the per-row query is still buildRecentCardTransactionsQuery
No recent card spending and nothing awaiting approval or repaid → the section doesn't appear Yes — a card with no transactions in the last 30 days returns no group, so no row
Section visibility conditions (awaiting approval, repaid last 30 days, active Expensify Card) Yes — applicabilityKey logic is unchanged
What I checked
  • Diff of src/pages/home/YourSpendSection/queries.ts and src/pages/home/YourSpendSection/useYourSpendData.ts — the new buildCardGroupQuery adds groupBy: CARD and drops the per-card cardID filter; getCardTotalsByCardID maps group entries to row totals. No user-facing strings, labels, or section-visibility rules are touched.
  • Searched docs/articles for Your spend, Recent transactions, and Home coverage. Only Expensify-Home-Overview.md documents this section.
  • The two limitations you called out in the PR body — hidden cards still returning groups, and a newly displayable card getting its row on the next focused fire — are internal fetch details. Neither is described on the help site, and neither changes what a member sees for a card that has spend.
  • Offline behavior (rows keep their last-known totals, deleting a card removes only that row) matches the pre-refactor cached behavior, and the help site does not document offline specifics for this section.

One pre-existing gap, unrelated to this PR and out of scope here: the FAQ Why don't I see the Your spend section? lists only "an active Expensify Card assigned to you," while the code also builds rows for displayable third-party and personal cards via getDisplayableThirdPartyCards. Happy to open a separate docs PR for that if you want it corrected.

Since no docs PR was created, there is nothing to label HelpDot, assign, or link.


view run · view recording

@OSBotify

OSBotify commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 9.4.72-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

MelvinBot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

No help site changes are required, so I did not open a docs PR.

This PR swaps N per-card Search calls for one group-by:card Search (queries.ts:45-52). It changes how the totals are fetched, not what a member sees. Every statement in Expensify-Home-Overview.md about Your spend still holds.

What I checked

The only article covering this surface is docs/articles/new-expensify/getting-started/Expensify-Home-Overview.md, section "How the Your spend section works on Home" plus its FAQ entry. I checked each documented claim against the diff:

Documented behavior Still accurate?
Awaiting approval and Repaid last 30 days rows Yes — untouched by this PR
Recent transactions • 1234 — one row per card with the last four digits Yes — same displayableCards list, same lastFour derivation
Selecting a row opens the matching expenses on Spend Yes — the row still carries buildRecentCardTransactionsQuery(accountID, cardID)
A card with no spend in the last 30 days shows no row Yes — the 30-day window moved from the per-card query to the grouped query; the row is still dropped when the card's group has no count
Conditions for the whole section appearing Yes — isApprovalApplicable / isPaymentApplicable / displayable-cards logic is unchanged

No UI strings, labels, section names, or visibility rules were added or removed, so there is nothing new to name or capitalize in the docs.

One thing worth your call, not a docs change I'd make unprompted: the search effect now keys on the grouped query hash rather than the card set (useYourSpendData.ts:587). A card that becomes displayable while Home is already open gets its row on the next focused fire instead of immediately — which matches the "Not covered" note in your PR body. That is refresh timing, not a workflow, and the help site does not document refresh timing anywhere. If you consider it durable rather than a rough edge to fix, tell me and I will add an FAQ entry for it.

You asked me to close with the line below. There is no linked help site PR to review, since none was needed — if you disagree with that call, reply and I will open one.

@adhorodyski, please review the linked help site PR and confirm it reflects the current behavior. Then mark the linked help site PR Ready for review


view run · view recording

@IuliiaHerets

IuliiaHerets commented Sep 4, 2026

Copy link
Copy Markdown

Hi @adhorodyski. QA team tried using some internal accounts. Most of them have card transactions from the last 30 days and have more than 5 requests. Is there a specific account that should be used? Or can you check it internally?

cc @JmillsExpensify @mountiny @ZhenjaHorbach

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.

7 participants