Replace the entity slugs on organizations, workspaces, resource pools, and plan ladders with nullable `key` columns and add keys to products, prices, and entitlement sets. Rename `providers.slug` to `provider` and add partial unique indexes for system and org role names. Assign invoice numbers per billing account from a gapless transactional counter; Stripe's number moves to the invoice mapping as an external reference. Seeds, fixtures, and the operator lookup address rows by key, and the returning-login resync no longer blanks a display name when the IdP sends no `name` claim.
58 lines
1.8 KiB
SQL
58 lines
1.8 KiB
SQL
-- name: CreateOrgMember :one
|
|
INSERT INTO core.org_members (org_id, person_id, role_id)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING *;
|
|
|
|
-- name: GetOrgMemberByPersonAndOrg :one
|
|
SELECT * FROM core.org_members
|
|
WHERE person_id = $1 AND org_id = $2;
|
|
|
|
-- name: GetOrgMembersByOrgID :many
|
|
SELECT om.*, p.display_name, p.primary_email, r.role_name
|
|
FROM core.org_members om
|
|
JOIN core.persons p ON p.person_id = om.person_id
|
|
JOIN core.roles r ON r.role_id = om.role_id
|
|
WHERE om.org_id = $1
|
|
ORDER BY p.display_name;
|
|
|
|
-- name: ListActiveMembersByOrgID :many
|
|
-- Operator composite's Members section. Active members only (status='active',
|
|
-- excluding removed/historical rows). Defensive LIMIT 200 per design D5;
|
|
-- pagination is YAGNI until a real org pushes 50+ members.
|
|
SELECT
|
|
om.org_member_id,
|
|
om.person_id,
|
|
om.role_id,
|
|
om.joined_at,
|
|
p.display_name,
|
|
p.primary_email,
|
|
r.role_name
|
|
FROM core.org_members om
|
|
JOIN core.persons p ON p.person_id = om.person_id
|
|
JOIN core.roles r ON r.role_id = om.role_id
|
|
WHERE om.org_id = $1
|
|
AND om.status = 'active'
|
|
ORDER BY p.display_name ASC
|
|
LIMIT 200;
|
|
|
|
-- name: ListActiveMembershipsByPersonID :many
|
|
-- Person-detail "Org memberships" section. Returns each org the person
|
|
-- belongs to (active memberships only) with the org's display fields and
|
|
-- the person's role at that org. Sorted by joined_at DESC so the most
|
|
-- recent membership appears first — operators usually want "what's the
|
|
-- person doing now" rather than "where did they start."
|
|
SELECT
|
|
om.org_member_id,
|
|
om.org_id,
|
|
om.role_id,
|
|
om.joined_at,
|
|
o.name AS org_name,
|
|
r.role_name
|
|
FROM core.org_members om
|
|
JOIN core.organizations o ON o.org_id = om.org_id
|
|
JOIN core.roles r ON r.role_id = om.role_id
|
|
WHERE om.person_id = $1
|
|
AND om.status = 'active'
|
|
ORDER BY om.joined_at DESC
|
|
LIMIT 200;
|