Files
member-console/internal/organization/queries/organizations.sql
T
cgalo5758 257955c9d3 Add operator list-scale contract and People directory
Governed operator lists (organizations, grants, people, billing×4) gain
server-side search, status filters, and 50-row pages with true totals
from count(*) OVER(); state is URL-addressable, out-of-range pages
clamp,
and no-match is distinct from true-empty.

People is the eighth flat sidebar entry: /operator/persons lists persons
newest-joined first (excluding the reserved system person), rows linking
to the existing detail.

Billing gains an operator invoice detail at
/operator/billing/invoices/{invoiceID} reusing the member projection;
open invoices past due present as Overdue (derived, filterable, stored
status untouched); all four views lead with the linked organization and
mute object IDs.

Grants filter over the derived Live/Superseded/Inactive state, the SQL
HAVING predicate pinned to the Go derivation by test. Embedded lists
(org composite ledger, Tier changes) adopt the shared controls under
namespaced params with sibling-state-preserving URLs and scoped htmx
swaps that hold the viewport.

Review corrections: blocked ladder Delete renders disabled with tooltip
and mutations fire toasts; collapse triggers paint their open state;
sections use outside headings; plan topology drops the orphan-product
check; domains policy collapses behind a disclosure.
2026-08-24 03:58:18 -05:00

80 lines
3.4 KiB
SQL

-- name: CreateOrganization :one
INSERT INTO core.organizations (name, slug, org_type, owner_person_id)
VALUES ($1, $2, $3, $4)
RETURNING *;
-- name: GetOrganizationBySlug :one
SELECT * FROM core.organizations
WHERE slug = $1;
-- name: GetOrganizationByID :one
SELECT * FROM core.organizations
WHERE org_id = $1;
-- name: GetOrganizationsByOwner :many
SELECT * FROM core.organizations
WHERE owner_person_id = $1
ORDER BY name;
-- name: ListOrganizations :many
SELECT * FROM core.organizations
ORDER BY name;
-- name: ListOrganizationsPage :many
-- Organizations list operator surface (operator-list-scale UX-4): the
-- paged, searched, org-type-filtered successor to ListOrganizationsWithOwner
-- -- each row still carries its owner's display name so no organization can
-- read as unowned (ux-honest-surfaces UX-16). owner_person_id is NOT NULL
-- (every organization has an owner person, the System tenant included --
-- 00001_init.sql), so the LEFT JOIN is defensive rather than expected to
-- ever drop the owner's name.
--
-- sqlc.narg(q): NULL matches every row; set, a case-insensitive substring
-- match against the organization's name or slug, or its owner's display
-- name or primary email (operator-list-scale: "carries a scoped
-- server-side search" -- "organizations: name, slug, and owner name or
-- email").
-- sqlc.narg(org_type): NULL matches every org type; set, an exact facet
-- match (operator-list-scale: "Status filters exist ... organizations: org
-- type").
-- count(*) OVER() carries the true total for the filtered set alongside
-- the page's rows (design D2), so the stated total never reflects only the
-- page.
SELECT o.org_id, o.name, o.slug, o.org_type, o.owner_person_id, o.status, o.created_at, o.updated_at,
p.display_name AS owner_display_name,
count(*) OVER() AS total_count
FROM core.organizations o
LEFT JOIN core.persons p ON p.person_id = o.owner_person_id
WHERE (sqlc.narg(q)::text IS NULL
OR o.name ILIKE '%' || sqlc.narg(q)::text || '%'
OR o.slug ILIKE '%' || sqlc.narg(q)::text || '%'
OR p.display_name ILIKE '%' || sqlc.narg(q)::text || '%'
OR p.primary_email ILIKE '%' || sqlc.narg(q)::text || '%')
AND (sqlc.narg(org_type)::text IS NULL OR o.org_type = sqlc.narg(org_type)::text)
ORDER BY o.name
LIMIT sqlc.arg(page_limit)::int OFFSET sqlc.arg(page_offset)::int;
-- name: ListOrganizationsByType :many
SELECT * FROM core.organizations
WHERE org_type = $1
ORDER BY name;
-- name: CountTeamOrganizations :one
-- Operator overview headline. Personal orgs exist one-per-member by
-- structural convention (design/organization/architecture.md §2), so an
-- organizations count that includes them tracks the people count and reads
-- as an error. Negative-space predicate instead of org_type = 'team':
-- everything active that is neither the auto-created personal type nor a
-- reserved type counts, so team, enterprise, and deployment-defined types
-- need no code change here and the reserved system org never appears.
SELECT COUNT(*) FROM core.organizations o
JOIN core.org_types t ON t.org_type = o.org_type
WHERE o.status = 'active' AND o.org_type <> 'personal' AND NOT t.is_reserved;
-- name: EnsureSystemOrganization :exec
-- Idempotently insert the singleton System organization (keyed on the unique
-- slug). Read back with GetOrganizationBySlug.
INSERT INTO core.organizations (name, slug, org_type, owner_person_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (slug) DO NOTHING;