feat(invitations): accept invitations with C4 consent and contact grants; share the E0 quota across holdings

This commit is contained in:
shira committed 2026-10-09 18:03:35 +03:00
1 parent a5990d3fc6
commit 676b3c6e24
59 files changed
+4926 -194

No files matched your search

+54 -6
View File
@@ -1,6 +1,8 @@
-- Writers lock the organization first, then invitation/card-view state, and
-- finally usage rows. The organization lock serializes quota decisions for
-- the tenant and makes the subsequent count and insert atomic.
-- Writers lock the organization first, then, for an E0 organization, the holding
-- whose pool it shares, then invitation/card-view state, and finally usage rows.
-- The organization lock serializes quota decisions for the tenant, the holding
-- lock serializes the E0 pool across the organizations of one holding, and
-- together they make the subsequent count and insert atomic.
-- Takes the organization lock ahead of every share lock of a reader that will
-- reserve usage later. FOR NO KEY UPDATE conflicts with itself and with FOR
@@ -9,9 +11,13 @@
-- FOR KEY SHARE that a foreign key takes, such as the hidden-organization
-- insert of a candidate, which holds the candidate's row and then needs this
-- one. The reservation itself still takes FOR UPDATE inside the same
-- transaction.
-- transaction. The result carries the holding, whose pool an E0 organization
-- shares; the caller locks that holding right after this lock.
-- name: LockCompanyQuotaOrganizationFirst :one
SELECT id
SELECT
id,
verification_level,
holding_id
FROM organizations
WHERE id = sqlc.arg(organization_id)
FOR NO KEY UPDATE;
@@ -21,11 +27,24 @@ SELECT
id,
verification_level,
verification_basis,
verification_changed_at
verification_changed_at,
holding_id
FROM organizations
WHERE id = sqlc.arg(organization_id)
FOR UPDATE;
-- Takes the pool lock of an E0 holding. It follows the organization lock, so
-- every writer locks an organization first and its holding second. Two
-- reservations of one holding queue here, and the second one counts the
-- usage that the first one committed. The lock does not conflict with the
-- FOR KEY SHARE that organizations.holding_id takes when an organization joins
-- the holding.
-- name: LockCompanyQuotaHolding :one
SELECT id
FROM holdings
WHERE id = sqlc.arg(holding_id)
FOR NO KEY UPDATE;
-- name: FindCompanyInvitationQuotaUsage :one
SELECT invitation_id
FROM company_invitation_quota_usage
@@ -43,6 +62,20 @@ WHERE
organization_id = sqlc.arg(organization_id)
AND reserved_at > sqlc.arg(window_start);
-- Counts the invitation usage of the E0 pool of a holding: the usage of every
-- organization of the holding whose trust level is E0 when the count runs. Each
-- row stays attributed to the organization that consumed it.
-- name: CountCompanyInvitationQuotaHoldingWindow :one
SELECT
count(*)::bigint AS usage_count,
min(usage.reserved_at) AS earliest_at
FROM company_invitation_quota_usage AS usage
INNER JOIN organizations AS organization ON organization.id = usage.organization_id
WHERE
organization.holding_id = sqlc.arg(holding_id)
AND organization.verification_level = 'e0'
AND usage.reserved_at > sqlc.arg(window_start);
-- name: InsertCompanyInvitationQuotaUsage :exec
INSERT INTO company_invitation_quota_usage (
organization_id,
@@ -95,6 +128,21 @@ WHERE
organization_id = sqlc.arg(organization_id)
AND last_seen_at > sqlc.arg(window_start);
-- Counts the distinct-card usage of the E0 pool of a holding, with the same
-- rule as CountCompanyInvitationQuotaHoldingWindow. A view is still counted
-- per organization: the same candidate seen by two organizations of one
-- holding is two charges.
-- name: CountCompanyCardViewQuotaHoldingWindow :one
SELECT
count(*)::bigint AS usage_count,
min(usage.last_seen_at) AS earliest_at
FROM company_card_view_quota_usage AS usage
INNER JOIN organizations AS organization ON organization.id = usage.organization_id
WHERE
organization.holding_id = sqlc.arg(holding_id)
AND organization.verification_level = 'e0'
AND usage.last_seen_at > sqlc.arg(window_start);
-- name: SaveCompanyCardViewQuotaUsage :exec
INSERT INTO company_card_view_quota_usage (
organization_id,