Accounts Domain
The Accounts domain handles multi-tenancy, user management, and authentication through Auth0 integration.
Entity Hierarchy
Key Concepts
| Entity | Description |
|---|---|
| Account | Top-level tenant container (like a company). All data is scoped to an account. |
| User | Individual person. Can belong to multiple accounts with different roles. |
| Session | Tracks which account a user currently has selected (stored in Redis). |
| Auth0 Organisation | External SSO tenant in Auth0, linked 1:1 with Account. |
Multi-Tenancy Model
Users can belong to multiple accounts, each with a specific role:
| Role | Permissions |
|---|---|
| admin | Full access, can manage members |
| member | Standard access, can view and edit |
| viewer | Read-only access |
Account Selection Flow
- Session stores
selectedAccountId- all queries scope to this account - Switching accounts updates the session, not the user record
- Sessions are stored in Redis with TTL-based expiration
Business Rules
- Users must belong to at least one account
- Users with multiple accounts must select one before proceeding
- Admins can manage members; members can view only
- New users must complete registration before accessing features
- Account names should stay in sync with Auth0 org names
- Sessions expire after inactivity (TTL-based)
Common Operations
Getting Current User Session
import { getSession } from '@repo/core';
const session = await getSession();
// session.userId - current user
// session.selectedAccountId - active accountCreating a New Account
import { CreateAccount } from '@repo/core';
const useCase = new CreateAccount();
const account = await useCase.execute({
name: 'Acme Corp',
adminEmail: 'admin@acme.com',
});Adding a User to an Account
import { AddUserToAccount } from '@repo/core';
const useCase = new AddUserToAccount();
await useCase.execute({
accountId: 'account-123',
email: 'user@example.com',
role: 'member',
});Switching Accounts
import { SwitchAccount } from '@repo/core';
const useCase = new SwitchAccount();
await useCase.execute({
accountId: 'account-456',
});
// Session now scoped to new accountAuth0 Integration
Account creation and user management sync with Auth0:
| Platform Action | Auth0 Action |
|---|---|
| Create account | Create Auth0 Organisation |
| Add user | Invite to Auth0 Organisation |
| Remove user | Remove from Auth0 Organisation |
| Update account name | Update Auth0 Organisation name |
Important Considerations
- Auth0 API calls cannot be rolled back in transactions
- If Neo4j write succeeds but Auth0 fails, handle orphaned records
- Use background jobs for Auth0 sync when possible
Related Files
| Type | Location |
|---|---|
| GraphQL Schema | packages/core/src/infrastructure/neo4j/schemas/account.graphql |
| Repository | packages/core/src/infrastructure/repositories/account-repository.ts |
| Use Cases | packages/core/src/application/use-cases/account/ |
| Auth0 Integration | packages/core/src/infrastructure/auth0/ |
| Session Management | packages/core/src/infrastructure/redis/session.ts |