Skip to content

refactor: eliminate workarounds, replace with idiomatic patterns#70

Merged
dacrab merged 1 commit intomainfrom
refactor/workarounds-and-patterns
Mar 17, 2026
Merged

refactor: eliminate workarounds, replace with idiomatic patterns#70
dacrab merged 1 commit intomainfrom
refactor/workarounds-and-patterns

Conversation

@dacrab
Copy link
Copy Markdown
Owner

@dacrab dacrab commented Mar 17, 2026

hooks.server.ts

  • Remove redundant getSession() call (getUser() is sufficient for JWT validation)
  • Remove manual expired-session check (supabase handles this internally)
  • Replace 2 separate DB queries (memberships + subscriptions) with get_user_context RPC — consistent with layout.server.ts, saves 2 round-trips per protected request

routes/+page.server.ts

  • Replace raw memberships query with get_user_context RPC (consistent with hooks)

admin/settings/+page.server.ts action

  • Replace get_user_context RPC call (fetches too much) with targeted memberships query for just tenant_id — right tool for the job

format.ts

  • Remove locale hack in formatCurrency (was manually mapping EUR→de-DE, USD→en-US, GBP→en-GB)
  • Use undefined locale so Intl.NumberFormat picks the correct locale for the currency automatically

supabase.ts

  • Replace typeof window !== 'undefined' with browser from $app/environment (consistent with rest of codebase)

(app)/+layout.svelte

  • Change $effect → $effect.pre for session/settings init — ensures stores are populated BEFORE first render, not after

bookings/[type]/+page.svelte

  • Remove dead client-side isValidType guard (server already throws 404 for invalid types)
  • Use $derived for type/icon to properly track reactive data prop changes
  • Use satisfies for icons record type safety

SQL: search_products

  • Change 'english' dictionary → 'simple' for language-agnostic full-text search
  • Adds ILIKE fallback for partial/typo matching
  • Fixes Greek product name search that was broken with English stemming

SQL: mv_best_sellers

  • Remove synchronous refresh trigger that was blocking every order INSERT transaction
  • Convert to scheduled refresh via keep-alive cron (daily) using service_role RPC
  • Add refresh_mv_best_sellers() call to keep-alive endpoint

mocks.ts

  • Add rpc() mock to createSupabaseMock (now required since hooks uses get_user_context)
  • Build get_user_context response from existing config shape
  • Add rpc to return type signature

tests

  • Remove session-expiry test (the feature was removed from hooks — getSession no longer called)

Summary by CodeRabbit

  • Bug Fixes

    • Currency formatting now respects device locale instead of using hardcoded defaults.
  • Improvements

    • Enhanced authentication and session management.
    • Improved type safety and code organization across the application.

## hooks.server.ts
- Remove redundant getSession() call (getUser() is sufficient for JWT validation)
- Remove manual expired-session check (supabase handles this internally)
- Replace 2 separate DB queries (memberships + subscriptions) with get_user_context RPC
  — consistent with layout.server.ts, saves 2 round-trips per protected request

## routes/+page.server.ts
- Replace raw memberships query with get_user_context RPC (consistent with hooks)

## admin/settings/+page.server.ts action
- Replace get_user_context RPC call (fetches too much) with targeted memberships query
  for just tenant_id — right tool for the job

## format.ts
- Remove locale hack in formatCurrency (was manually mapping EUR→de-DE, USD→en-US, GBP→en-GB)
- Use undefined locale so Intl.NumberFormat picks the correct locale for the currency automatically

## supabase.ts
- Replace typeof window !== 'undefined' with browser from $app/environment (consistent with rest of codebase)

## (app)/+layout.svelte
- Change $effect → $effect.pre for session/settings init
  — ensures stores are populated BEFORE first render, not after

## bookings/[type]/+page.svelte
- Remove dead client-side isValidType guard (server already throws 404 for invalid types)
- Use $derived for type/icon to properly track reactive data prop changes
- Use satisfies for icons record type safety

## SQL: search_products
- Change 'english' dictionary → 'simple' for language-agnostic full-text search
- Adds ILIKE fallback for partial/typo matching
- Fixes Greek product name search that was broken with English stemming

## SQL: mv_best_sellers
- Remove synchronous refresh trigger that was blocking every order INSERT transaction
- Convert to scheduled refresh via keep-alive cron (daily) using service_role RPC
- Add refresh_mv_best_sellers() call to keep-alive endpoint

## mocks.ts
- Add rpc() mock to createSupabaseMock (now required since hooks uses get_user_context)
- Build get_user_context response from existing config shape
- Add rpc to return type signature

## tests
- Remove session-expiry test (the feature was removed from hooks — getSession no longer called)
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Mar 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clubos Ready Ready Preview, Comment Mar 17, 2026 6:46pm

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 17, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: e3019c7f-1ea5-4ce7-9106-4caffcb5d10c

📥 Commits

Reviewing files that changed from the base of the PR and between a57af76 and 702783d.

⛔ Files ignored due to path filters (2)
  • supabase/migrations/0004_functions_triggers.sql is excluded by !supabase/migrations/**
  • supabase/migrations/0005_policies.sql is excluded by !supabase/migrations/**
📒 Files selected for processing (10)
  • src/hooks.server.test.ts
  • src/hooks.server.ts
  • src/lib/testing/mocks.ts
  • src/lib/utils/format.ts
  • src/lib/utils/supabase.ts
  • src/routes/(app)/+layout.svelte
  • src/routes/(app)/admin/settings/+page.server.ts
  • src/routes/(app)/bookings/[type]/+page.svelte
  • src/routes/+page.server.ts
  • src/routes/api/keep-alive/+server.ts

📝 Walkthrough

Walkthrough

The pull request refactors session and membership handling to use an RPC-based context fetch, updates client-side utilities, and improves type safety in the booking component. Most pages migrate to the get_user_context RPC, though admin settings reverts to a direct query. Test coverage for session expiry is removed.

Changes

Cohort / File(s) Summary
Core Authentication Refactor
src/hooks.server.ts, src/hooks.server.test.ts, src/lib/testing/mocks.ts
Replaces direct getSession database checks with lazy get_user_context RPC call; caching variable renamed from _membership to _ctx. Mock infrastructure updated to support RPC testing. Removes session expiry test block (17 lines).
RPC-Based Data Fetching
src/routes/+page.server.ts
Switches membership resolution from direct database query to get_user_context RPC call; removes is_primary fallback logic and simplifies redirect flow.
Direct Query Fallback
src/routes/(app)/admin/settings/+page.server.ts
Reverts to direct memberships table query instead of using get_user_context RPC; selects tenant_id ordered by is_primary.
Frontend & Type Improvements
src/routes/(app)/bookings/[type]/+page.svelte
Type-safe refactor: replaces as const with satisfies Record<...>, removes runtime isValidType check, derives type/icon as constants instead of inline logic.
Utility Updates
src/lib/utils/supabase.ts, src/lib/utils/format.ts
Replaces typeof window check with SvelteKit's browser boolean; currency formatting now relies on runtime locale instead of hard-coded USD/GBP branches.
Layout & API Maintenance
src/routes/(app)/+layout.svelte, src/routes/api/keep-alive/+server.ts
Changes $effect to $effect.pre for pre-render store sync; adds materialized view refresh (refresh_mv_best_sellers) to keep-alive endpoint.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/workarounds-and-patterns
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dacrab dacrab merged commit 924945d into main Mar 17, 2026
7 of 8 checks passed
@dacrab dacrab deleted the refactor/workarounds-and-patterns branch March 17, 2026 18:47
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