wulf-pulse/migrations/082_company_scope.sql
lorentz db375fb0e6 feat(admin): client scope — filter analytics to recurring-revenue companies
Adds company-level opt-out scoping so white-label / subcontract clients
(TTG, LEC, PER, VCF, Trivium Packaging, TNT Pizza, etc.) can be excluded
from Wulf's own dashboard KPIs and ticket analytics without affecting
per-company drill-down views.

- migration 082: company_scope table (opt-out; absent row = in scope)
- GET/PATCH /api/admin/company-scope[/companyId] — list + upsert
- /admin/client-scope — searchable company list with Switch per row,
  type filter, and in/out scope filter; excluded rows are dimmed
- dashboard overview KPIs now exclude out-of-scope company tickets
- analyzer /tickets query excludes out-of-scope when no specific
  client is selected (explicit per-company selection still works)
- "Client Scope" tile added to admin Tools & Data section

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 11:40:47 -04:00

25 lines
1.2 KiB
SQL

-- =============================================================================
-- Company scope table
-- =============================================================================
-- Controls which companies are included in Wulf's own analytics (dashboard
-- KPIs, analyzer ticket views, etc.). Opt-out model: companies without a row
-- here are implicitly in scope. Only rows with in_scope=false are excluded.
--
-- Use case: white-label / subcontract clients (TTG, LEC, PER, VCF, TNT Pizza,
-- Trivium Packaging, etc.) have dedicated branded queues and should not pollute
-- Wulf recurring-revenue metrics. Mark them in_scope=false via /admin/client-scope.
-- =============================================================================
CREATE TABLE IF NOT EXISTS company_scope (
company_id BIGINT PRIMARY KEY REFERENCES companies(id) ON DELETE CASCADE,
in_scope BOOLEAN NOT NULL DEFAULT true,
updated_by TEXT,
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_company_scope_in_scope
ON company_scope(in_scope)
WHERE in_scope = false;
COMMENT ON TABLE company_scope IS
'Opt-out scope filter for analytics. Absent row = in scope. in_scope=false = excluded from dashboard KPIs and ticket views.';