Fix category deletion and stale group tally after expense changes - #19
Merged
Merged
Conversation
Two bugs reported together: 1. Categories couldn't be deleted. `CategoriesRepository.deleteCategory` ran a plain `delete from categories`, which removes zero rows — with no error — whenever RLS hides the target (a global default, or another group's row), and fails on a foreign-key violation anywhere the 0012 `on delete set null` cascade isn't in place. Route it through a new `delete_category(id)` SECURITY DEFINER RPC (migration 0013) that authorizes the caller, raises a real error for the default / wrong-group cases, and detaches every expense/recurring/tag reference before deleting — the same pattern as `delete_expense`. 2. Deleting (or adding/editing/importing) an expense didn't update the "you owe / you're owed" tally on the groups list. The mutation sites invalidated `expensesProvider` and `balancesProvider` but not `groupSummariesProvider`, a separate view nothing else refreshes, so the list stayed stale until a manual pull-to-refresh. Add `invalidateGroupMoney(ref, groupId)` and use it at every expense / settlement mutation point. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EzXWJsUFffSSJAKGaRqUT4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two bugs, reported together
1. Categories couldn't be deleted
CategoriesRepository.deleteCategoryran a plaindelete from categories where id = ?. That has two failure modes, both of which look like "delete does nothing":group_id is null), or a category owned by another group. PostgREST returns 204, no error, the list refetches, the row is still there.0012on delete set nullcascade isn't in effect (an environment still on<= 0011) —expenses/recurring_expenses/merchant_rulesstill point at the row.Fix: new
delete_category(id)SECURITY DEFINERRPC (supabase/migrations/0013_delete_category_rpc.sql), mirroringdelete_expense/update_expense/update_group_photo. It:expenses/recurring_expenses/merchant_rulesreference explicitly, so it works regardless of the FK action,deleteCategorynow calls the RPC.2. Deleting an expense didn't fix the tally
The "you owe / you're owed" line on each groups-list card comes from
groupSummariesProvider(themy_group_summariesview). Every expense mutation site — add, edit, delete, import — and the settle-up flow invalidatedexpensesProviderandbalancesProviderbut notgroupSummariesProvider, which nothing else refreshes. Navigation iscontext.push, soGroupsListScreenstays mounted underneath and reuses its cached value: the tally stayed stale until a manual pull-to-refresh.Fix:
invalidateGroupMoney(ref, groupId)ingroups_provider.dartinvalidates all three, and replaces the ad-hoc pairs at every expense / settlement mutation point (group_detail_screen.dart,balances_tab.dart).Testing
flutter analyzeclean,flutter test— 180 existing + 1 new pass.test/features/groups/group_money_invalidation_test.dartassertsinvalidateGroupMoneyrefetches the groups-list summary, not just the in-group providers.delete_categoryRPC still needssupabase db push(or running0013in the SQL editor) against your project.🤖 Generated with Claude Code