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

@@ -11,6 +11,39 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const countCompanyCardViewQuotaHoldingWindow = `-- 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 = $1
AND organization.verification_level = 'e0'
AND usage.last_seen_at > $2
`
type CountCompanyCardViewQuotaHoldingWindowParams struct {
HoldingID pgtype.UUID
WindowStart pgtype.Timestamptz
}
type CountCompanyCardViewQuotaHoldingWindowRow struct {
UsageCount int64
EarliestAt interface{}
}
// 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.
func (q *Queries) CountCompanyCardViewQuotaHoldingWindow(ctx context.Context, arg CountCompanyCardViewQuotaHoldingWindowParams) (CountCompanyCardViewQuotaHoldingWindowRow, error) {
row := q.db.QueryRow(ctx, countCompanyCardViewQuotaHoldingWindow, arg.HoldingID, arg.WindowStart)
var i CountCompanyCardViewQuotaHoldingWindowRow
err := row.Scan(&i.UsageCount, &i.EarliestAt)
return i, err
}
const countCompanyCardViewQuotaWindow = `-- name: CountCompanyCardViewQuotaWindow :one
SELECT
count(*)::bigint AS usage_count,
@@ -38,6 +71,38 @@ func (q *Queries) CountCompanyCardViewQuotaWindow(ctx context.Context, arg Count
return i, err
}
const countCompanyInvitationQuotaHoldingWindow = `-- 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 = $1
AND organization.verification_level = 'e0'
AND usage.reserved_at > $2
`
type CountCompanyInvitationQuotaHoldingWindowParams struct {
HoldingID pgtype.UUID
WindowStart pgtype.Timestamptz
}
type CountCompanyInvitationQuotaHoldingWindowRow struct {
UsageCount int64
EarliestAt interface{}
}
// 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.
func (q *Queries) CountCompanyInvitationQuotaHoldingWindow(ctx context.Context, arg CountCompanyInvitationQuotaHoldingWindowParams) (CountCompanyInvitationQuotaHoldingWindowRow, error) {
row := q.db.QueryRow(ctx, countCompanyInvitationQuotaHoldingWindow, arg.HoldingID, arg.WindowStart)
var i CountCompanyInvitationQuotaHoldingWindowRow
err := row.Scan(&i.UsageCount, &i.EarliestAt)
return i, err
}
const countCompanyInvitationQuotaWindow = `-- name: CountCompanyInvitationQuotaWindow :one
SELECT
count(*)::bigint AS usage_count,
@@ -185,12 +250,33 @@ func (q *Queries) InsertCompanyInvitationQuotaUsage(ctx context.Context, arg Ins
return err
}
const lockCompanyQuotaHolding = `-- name: LockCompanyQuotaHolding :one
SELECT id
FROM holdings
WHERE id = $1
FOR NO KEY 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.
func (q *Queries) LockCompanyQuotaHolding(ctx context.Context, holdingID pgtype.UUID) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, lockCompanyQuotaHolding, holdingID)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const lockCompanyQuotaOrganization = `-- name: LockCompanyQuotaOrganization :one
SELECT
id,
verification_level,
verification_basis,
verification_changed_at
verification_changed_at,
holding_id
FROM organizations
WHERE id = $1
FOR UPDATE
@@ -201,6 +287,7 @@ type LockCompanyQuotaOrganizationRow struct {
VerificationLevel string
VerificationBasis string
VerificationChangedAt pgtype.Timestamptz
HoldingID pgtype.UUID
}
func (q *Queries) LockCompanyQuotaOrganization(ctx context.Context, organizationID pgtype.UUID) (LockCompanyQuotaOrganizationRow, error) {
@@ -211,21 +298,33 @@ func (q *Queries) LockCompanyQuotaOrganization(ctx context.Context, organization
&i.VerificationLevel,
&i.VerificationBasis,
&i.VerificationChangedAt,
&i.HoldingID,
)
return i, err
}
const lockCompanyQuotaOrganizationFirst = `-- name: LockCompanyQuotaOrganizationFirst :one
SELECT id
SELECT
id,
verification_level,
holding_id
FROM organizations
WHERE id = $1
FOR NO KEY UPDATE
`
// 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.
type LockCompanyQuotaOrganizationFirstRow struct {
ID pgtype.UUID
VerificationLevel string
HoldingID pgtype.UUID
}
// 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
// SHARE, so readers of one organization queue up instead of deadlocking on a
@@ -233,12 +332,13 @@ FOR NO KEY UPDATE
// 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.
func (q *Queries) LockCompanyQuotaOrganizationFirst(ctx context.Context, organizationID pgtype.UUID) (pgtype.UUID, error) {
// transaction. The result carries the holding, whose pool an E0 organization
// shares; the caller locks that holding right after this lock.
func (q *Queries) LockCompanyQuotaOrganizationFirst(ctx context.Context, organizationID pgtype.UUID) (LockCompanyQuotaOrganizationFirstRow, error) {
row := q.db.QueryRow(ctx, lockCompanyQuotaOrganizationFirst, organizationID)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
var i LockCompanyQuotaOrganizationFirstRow
err := row.Scan(&i.ID, &i.VerificationLevel, &i.HoldingID)
return i, err
}
const refreshCompanyCardViewQuotaUsage = `-- name: RefreshCompanyCardViewQuotaUsage :execrows