Skip to content

refactor: separate hooks and services across all features#4

Open
pysnooLab wants to merge 13 commits intomainfrom
feat/refactor-hooks-services
Open

refactor: separate hooks and services across all features#4
pysnooLab wants to merge 13 commits intomainfrom
feat/refactor-hooks-services

Conversation

@pysnooLab
Copy link
Copy Markdown
Collaborator

Summary

  • Adds a `services/` folder per feature with plain async functions wrapping Supabase — no React dependencies
  • Splits multi-action hook files into one file per action (e.g. `useCreateCustomer.ts`, `useUpdateCustomer.ts`)
  • Hooks no longer import from `@/lib/supabase` directly — only from their feature's service layer
  • Hook tests mock the service module; service tests mock `@/lib/supabase`
  • Documents the convention in CLAUDE.md and updates the Testing section to reflect the new mocking strategy

Features refactored

  • customers: `services/customers.ts` + 4 hooks (useCustomers, useCreateCustomer, useUpdateCustomer, useUserCompany)
  • companies: `services/companies.ts` + `services/invites.ts` + 5 hooks (useCompanies, useCreateCompany, useUpdateCompany, useDeleteCompany, useCreateInvite)
  • agents: `services/agents.ts` + 2 hooks (useAgents, useUpdateAgentCompanies)
  • auth: `services/auth.ts` + `services/profile.ts` + `services/invite.ts` + updated useAuth, useProfile, AuthProvider, SignupForm

Test plan

  • All 80 unit tests pass (`npm test`)
  • Lint passes with 0 errors (`npm run lint`)
  • TypeScript build succeeds (`npm run build`)
  • Service tests mock `@/lib/supabase` directly
  • Hook tests mock service modules (not supabase)
  • E2E: login, customers CRUD, invite flow still work end-to-end

🤖 Generated with Claude Code

pysnooLab and others added 13 commits April 7, 2026 17:26
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Extract Supabase calls for customers into a dedicated service module with fetchCustomers, createCustomer, updateCustomer, and fetchUserCompany. All functions covered by unit tests (7 passing).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…y constraint

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Remove manual `as { email: string }[]` cast in validateInvite — the RPC
return type is already inferred from the generated database types.

Remove `.select().single()` from deleteCompany — these caused a PGRST116
error when no row was found; a void delete needs no row returned. Also
update the test chain mock to be thenable so await works without .single().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Move useCreateCompany, useUpdateCompany, useDeleteCompany into dedicated
hook files; wire useCompanies, useCompany, useCreateInvite through the
service layer; update component imports and test mocks accordingly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… and SignupForm

Extract auth, profile, and invite logic into dedicated service modules under
src/features/auth/services/. Update AuthProvider, useAuth, useProfile, and
SignupForm to consume the services instead of calling supabase directly.
Update AuthProvider.test.tsx to mock the service layer instead of @/lib/supabase.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…nupForm

- Rewrite useAuth.test.ts to mock ../services/auth instead of @/lib/supabase,
  aligning with the refactored hook that delegates to the service layer
- Add noValidate to SignupForm <form> to prevent browser constraint validation
  from interfering with react-hook-form/zod validation
- Replace broken vi.doMock-based third test in AuthProvider.test.tsx with a
  straightforward assertion that onAuthStateChange is called once on mount

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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