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,
+45
View File
@@ -0,0 +1,45 @@
-- Store the contact grant of an acceptance. The row is inserted in the same
-- transaction as its C4 consent record and the accepted state; the database
-- checks that the record, the invitation, and the candidate agree.
-- name: InsertContactGrant :exec
INSERT INTO contact_grants (
id,
organization_id,
candidate_id,
source_invitation_id,
consent_record_id,
terms_version,
fields,
consent_text_version,
granted_at
) VALUES (
sqlc.arg(id),
sqlc.arg(organization_id),
sqlc.arg(candidate_id),
sqlc.arg(source_invitation_id),
sqlc.arg(consent_record_id),
sqlc.arg(terms_version),
sqlc.arg(fields)::text [],
sqlc.arg(consent_text_version),
sqlc.arg(granted_at)
);
-- Read the grant of one invitation of one organization. An invitation of
-- another organization finds nothing.
-- name: GetContactGrantByInvitation :one
SELECT
id,
organization_id,
candidate_id,
source_invitation_id,
consent_record_id,
terms_version,
fields,
consent_text_version,
granted_at,
revoked_at,
revocation_reason
FROM contact_grants
WHERE
organization_id = sqlc.arg(organization_id)
AND source_invitation_id = sqlc.arg(invitation_id);
+20
View File
@@ -0,0 +1,20 @@
-- Stores the holding of a founder account unless the account already has one.
-- A concurrent creation of the same account waits on the founder key until the
-- other transaction ends; then this insert does nothing, and the caller reads
-- the stored holding with GetHoldingIDByFounder.
-- name: InsertHoldingIfAbsent :exec
INSERT INTO holdings (
id,
founder_account_id,
created_at
) VALUES (
sqlc.arg(id),
sqlc.arg(founder_account_id),
sqlc.arg(created_at)
)
ON CONFLICT (founder_account_id) DO NOTHING;
-- name: GetHoldingIDByFounder :one
SELECT id
FROM holdings
WHERE founder_account_id = sqlc.arg(founder_account_id);
+39
View File
@@ -127,3 +127,42 @@ SELECT
outcome
FROM invitation_decline_results
WHERE scope_hash = sqlc.arg(scope_hash);
-- The compare-and-swap of an acceptance: it applies only to a delivered or
-- viewed invitation at the version and the terms version that the candidate
-- read, and before its deadline. The answer is the acceptance time, and the
-- deadline is kept.
-- name: AcceptInvitation :one
UPDATE invitations
SET
state = 'accepted',
version = sqlc.arg(next_version),
responded_at = sqlc.arg(accepted_at),
updated_at = sqlc.arg(accepted_at)
WHERE
organization_id = sqlc.arg(organization_id)
AND id = sqlc.arg(invitation_id)
AND state IN ('delivered', 'viewed')
AND version = sqlc.arg(expected_version)
AND terms_version = sqlc.arg(terms_version)
AND response_deadline_at > sqlc.arg(accepted_at)
AND sqlc.arg(next_version)::bigint = sqlc.arg(expected_version)::bigint + 1
RETURNING id;
-- name: InsertInvitationAcceptResult :exec
INSERT INTO invitation_accept_results (
scope_hash,
invitation_id,
outcome
) VALUES (
sqlc.arg(scope_hash),
sqlc.arg(invitation_id),
sqlc.arg(outcome)
);
-- name: GetInvitationAcceptResult :one
SELECT
invitation_id,
outcome
FROM invitation_accept_results
WHERE scope_hash = sqlc.arg(scope_hash);
+7 -5
View File
@@ -1,7 +1,7 @@
-- Stores a new organization with the verification level that the domain
-- gave it. The deferred constraint organizations_owner_required fails the
-- transaction at commit unless an active owner membership was inserted in
-- it too.
-- gave it and the holding of its creator. The deferred constraint
-- organizations_owner_required fails the transaction at commit unless an
-- active owner membership was inserted in it too.
-- name: InsertOrganization :exec
INSERT INTO organizations (
id,
@@ -17,7 +17,8 @@ INSERT INTO organizations (
verification_changed_at,
version,
created_at,
updated_at
updated_at,
holding_id
) VALUES (
sqlc.arg(id),
sqlc.arg(name),
@@ -32,7 +33,8 @@ INSERT INTO organizations (
sqlc.arg(verification_changed_at),
sqlc.arg(version),
sqlc.arg(created_at),
sqlc.arg(updated_at)
sqlc.arg(updated_at),
sqlc.arg(holding_id)
);
-- Stores one representative at its position in the list. The adapter calls it