ORGANIZATION MODEL
Organization Data Model
Section titled “Organization Data Model”Overview
Section titled “Overview”Organizations allow multiple users to collaborate under a shared workspace with centralized settings, branding, and data management.
Database Schema
Section titled “Database Schema”organizations table
Section titled “organizations table”CREATE TABLE organizations ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT NOT NULL, slug TEXT UNIQUE NOT NULL, logo_url TEXT, brand_colors JSONB DEFAULT '{"primary": "#000000", "secondary": "#ffffff"}', settings JSONB DEFAULT '{}', created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW());organization_members table
Section titled “organization_members table”CREATE TABLE organization_members ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE, user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, role TEXT NOT NULL CHECK (role IN ('owner', 'admin', 'member')), invited_by UUID REFERENCES auth.users(id), invited_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), joined_at TIMESTAMP WITH TIME ZONE, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), UNIQUE(organization_id, user_id));organization_invitations table
Section titled “organization_invitations table”CREATE TABLE organization_invitations ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE, email TEXT NOT NULL, role TEXT NOT NULL CHECK (role IN ('admin', 'member')), invited_by UUID NOT NULL REFERENCES auth.users(id), token TEXT UNIQUE NOT NULL, expires_at TIMESTAMP WITH TIME ZONE NOT NULL, accepted_at TIMESTAMP WITH TIME ZONE, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), UNIQUE(organization_id, email));Roles & Permissions
Section titled “Roles & Permissions”- Full access to all organization settings
- Can delete the organization
- Can transfer ownership
- Can manage all members
- Can manage organization settings (branding, templates, etc.)
- Can invite/remove members (except owners)
- Can view all organization data
Member
Section titled “Member”- Can view organization resources
- Cannot modify organization settings
- Can use organization templates and branding
Migration from Current System
Section titled “Migration from Current System”Current Issues
Section titled “Current Issues”- Enterprise settings (
enterprise_settingstable) are per-user, not per-organization - Branding settings are in global user settings instead of org-level admin
- No concept of shared resources across team members
Migration Steps
Section titled “Migration Steps”- Create new organization tables
- Migrate existing
enterprise_settingsto organizations - Create default organization for each existing user (if they have enterprise settings)
- Move branding/templates tabs from settings to admin pages
- Update storage structure:
workspaces/{user_id}/→organizations/{org_id}/
Frontend Structure
Section titled “Frontend Structure”/admin/├── organization/ # Organization settings (name, slug, members)├── branding/ # Logo, colors (moved from settings)├── templates/ # Agent/prompt templates (moved from settings)├── data/ # Data management (moved from settings)└── members/ # Member management & invitationsAPI Endpoints
Section titled “API Endpoints”GET /api/organizations # List user's organizationsPOST /api/organizations # Create new organizationGET /api/organizations/:id # Get organization detailsPATCH /api/organizations/:id # Update organizationDELETE /api/organizations/:id # Delete organization
GET /api/organizations/:id/members # List membersPOST /api/organizations/:id/members # Invite memberDELETE /api/organizations/:id/members/:id # Remove member
POST /api/organizations/:id/invitations # Create invitationGET /api/invitations/:token # Get invitation detailsPOST /api/invitations/:token/accept # Accept invitationUsage Flow
Section titled “Usage Flow”- New User: Automatically creates a personal organization on first login
- Create Organization: User creates a new organization (becomes owner)
- Invite Members: Owner/admin sends invitation emails
- Accept Invitation: User clicks email link, joins organization
- Switch Organization: User can switch between organizations they belong to
- Admin Settings: Only owners/admins can access /admin/* pages