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
@@ -0,0 +1,111 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: contact_grants.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getContactGrantByInvitation = `-- 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 = $1
AND source_invitation_id = $2
`
type GetContactGrantByInvitationParams struct {
OrganizationID pgtype.UUID
InvitationID pgtype.UUID
}
// Read the grant of one invitation of one organization. An invitation of
// another organization finds nothing.
func (q *Queries) GetContactGrantByInvitation(ctx context.Context, arg GetContactGrantByInvitationParams) (ContactGrant, error) {
row := q.db.QueryRow(ctx, getContactGrantByInvitation, arg.OrganizationID, arg.InvitationID)
var i ContactGrant
err := row.Scan(
&i.ID,
&i.OrganizationID,
&i.CandidateID,
&i.SourceInvitationID,
&i.ConsentRecordID,
&i.TermsVersion,
&i.Fields,
&i.ConsentTextVersion,
&i.GrantedAt,
&i.RevokedAt,
&i.RevocationReason,
)
return i, err
}
const insertContactGrant = `-- 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 (
$1,
$2,
$3,
$4,
$5,
$6,
$7::text [],
$8,
$9
)
`
type InsertContactGrantParams struct {
ID pgtype.UUID
OrganizationID pgtype.UUID
CandidateID pgtype.UUID
SourceInvitationID pgtype.UUID
ConsentRecordID pgtype.UUID
TermsVersion int64
Fields []string
ConsentTextVersion string
GrantedAt pgtype.Timestamptz
}
// 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.
func (q *Queries) InsertContactGrant(ctx context.Context, arg InsertContactGrantParams) error {
_, err := q.db.Exec(ctx, insertContactGrant,
arg.ID,
arg.OrganizationID,
arg.CandidateID,
arg.SourceInvitationID,
arg.ConsentRecordID,
arg.TermsVersion,
arg.Fields,
arg.ConsentTextVersion,
arg.GrantedAt,
)
return err
}
@@ -0,0 +1,53 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: holdings.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getHoldingIDByFounder = `-- name: GetHoldingIDByFounder :one
SELECT id
FROM holdings
WHERE founder_account_id = $1
`
func (q *Queries) GetHoldingIDByFounder(ctx context.Context, founderAccountID pgtype.UUID) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, getHoldingIDByFounder, founderAccountID)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const insertHoldingIfAbsent = `-- name: InsertHoldingIfAbsent :exec
INSERT INTO holdings (
id,
founder_account_id,
created_at
) VALUES (
$1,
$2,
$3
)
ON CONFLICT (founder_account_id) DO NOTHING
`
type InsertHoldingIfAbsentParams struct {
ID pgtype.UUID
FounderAccountID pgtype.UUID
CreatedAt pgtype.Timestamptz
}
// 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.
func (q *Queries) InsertHoldingIfAbsent(ctx context.Context, arg InsertHoldingIfAbsentParams) error {
_, err := q.db.Exec(ctx, insertHoldingIfAbsent, arg.ID, arg.FounderAccountID, arg.CreatedAt)
return err
}
@@ -11,6 +11,51 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const acceptInvitation = `-- name: AcceptInvitation :one
UPDATE invitations
SET
state = 'accepted',
version = $1,
responded_at = $2,
updated_at = $2
WHERE
organization_id = $3
AND id = $4
AND state IN ('delivered', 'viewed')
AND version = $5
AND terms_version = $6
AND response_deadline_at > $2
AND $1::bigint = $5::bigint + 1
RETURNING id
`
type AcceptInvitationParams struct {
NextVersion int64
AcceptedAt pgtype.Timestamptz
OrganizationID pgtype.UUID
InvitationID pgtype.UUID
ExpectedVersion int64
TermsVersion int64
}
// 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.
func (q *Queries) AcceptInvitation(ctx context.Context, arg AcceptInvitationParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, acceptInvitation,
arg.NextVersion,
arg.AcceptedAt,
arg.OrganizationID,
arg.InvitationID,
arg.ExpectedVersion,
arg.TermsVersion,
)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const declineInvitation = `-- name: DeclineInvitation :one
UPDATE invitations
SET
@@ -51,6 +96,26 @@ func (q *Queries) DeclineInvitation(ctx context.Context, arg DeclineInvitationPa
return id, err
}
const getInvitationAcceptResult = `-- name: GetInvitationAcceptResult :one
SELECT
invitation_id,
outcome
FROM invitation_accept_results
WHERE scope_hash = $1
`
type GetInvitationAcceptResultRow struct {
InvitationID pgtype.UUID
Outcome string
}
func (q *Queries) GetInvitationAcceptResult(ctx context.Context, scopeHash []byte) (GetInvitationAcceptResultRow, error) {
row := q.db.QueryRow(ctx, getInvitationAcceptResult, scopeHash)
var i GetInvitationAcceptResultRow
err := row.Scan(&i.InvitationID, &i.Outcome)
return i, err
}
const getInvitationDeclineResult = `-- name: GetInvitationDeclineResult :one
SELECT
invitation_id,
@@ -71,6 +136,29 @@ func (q *Queries) GetInvitationDeclineResult(ctx context.Context, scopeHash []by
return i, err
}
const insertInvitationAcceptResult = `-- name: InsertInvitationAcceptResult :exec
INSERT INTO invitation_accept_results (
scope_hash,
invitation_id,
outcome
) VALUES (
$1,
$2,
$3
)
`
type InsertInvitationAcceptResultParams struct {
ScopeHash []byte
InvitationID pgtype.UUID
Outcome string
}
func (q *Queries) InsertInvitationAcceptResult(ctx context.Context, arg InsertInvitationAcceptResultParams) error {
_, err := q.db.Exec(ctx, insertInvitationAcceptResult, arg.ScopeHash, arg.InvitationID, arg.Outcome)
return err
}
const insertInvitationDecline = `-- name: InsertInvitationDecline :exec
INSERT INTO invitation_declines (
invitation_id,
@@ -476,6 +476,20 @@ type ConsumerProcessedEvent struct {
ProcessedAt pgtype.Timestamptz
}
type ContactGrant struct {
ID pgtype.UUID
OrganizationID pgtype.UUID
CandidateID pgtype.UUID
SourceInvitationID pgtype.UUID
ConsentRecordID pgtype.UUID
TermsVersion int64
Fields []string
ConsentTextVersion string
GrantedAt pgtype.Timestamptz
RevokedAt pgtype.Timestamptz
RevocationReason pgtype.Text
}
type Delivery struct {
ID pgtype.UUID
SourceEventID pgtype.UUID
@@ -563,6 +577,12 @@ type EmailToken struct {
PendingOnboardingStep pgtype.Text
}
type Holding struct {
ID pgtype.UUID
FounderAccountID pgtype.UUID
CreatedAt pgtype.Timestamptz
}
type IdempotencyRecord struct {
ScopeHash []byte
Fingerprint []byte
@@ -592,6 +612,12 @@ type Invitation struct {
RespondedAt pgtype.Timestamptz
}
type InvitationAcceptResult struct {
ScopeHash []byte
InvitationID pgtype.UUID
Outcome string
}
type InvitationCreationResult struct {
ScopeHash []byte
Snapshot []byte
@@ -715,6 +741,7 @@ type Organization struct {
Version int64
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
HoldingID pgtype.UUID
}
type OrganizationCreationResult struct {
@@ -229,7 +229,8 @@ INSERT INTO organizations (
verification_changed_at,
version,
created_at,
updated_at
updated_at,
holding_id
) VALUES (
$1,
$2,
@@ -244,7 +245,8 @@ INSERT INTO organizations (
$11,
$12,
$13,
$14
$14,
$15
)
`
@@ -263,12 +265,13 @@ type InsertOrganizationParams struct {
Version int64
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
HoldingID pgtype.UUID
}
// 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.
func (q *Queries) InsertOrganization(ctx context.Context, arg InsertOrganizationParams) error {
_, err := q.db.Exec(ctx, insertOrganization,
arg.ID,
@@ -285,6 +288,7 @@ func (q *Queries) InsertOrganization(ctx context.Context, arg InsertOrganization
arg.Version,
arg.CreatedAt,
arg.UpdatedAt,
arg.HoldingID,
)
return err
}
@@ -549,6 +553,23 @@ type UpdateOrganizationProfileParams struct {
ManagerRoles []string
}
type UpdateOrganizationProfileRow struct {
ID pgtype.UUID
Name string
Description pgtype.Text
Sphere pgtype.Text
ContactWebsite pgtype.Text
ContactEmail pgtype.Text
ContactPhone pgtype.Text
ContactTelegram pgtype.Text
VerificationLevel string
VerificationBasis string
VerificationChangedAt pgtype.Timestamptz
Version int64
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
}
// Changes the profile of an organization over the version the caller read
// and advances the version in the same statement. It names no verification
// column, so a profile update cannot move the trust level. The change
@@ -562,7 +583,7 @@ type UpdateOrganizationProfileParams struct {
// follow it). No row returns for a stale version, an account that is no
// active manager, or an unknown organization; the adapter reads the
// membership to tell them apart.
func (q *Queries) UpdateOrganizationProfile(ctx context.Context, arg UpdateOrganizationProfileParams) (Organization, error) {
func (q *Queries) UpdateOrganizationProfile(ctx context.Context, arg UpdateOrganizationProfileParams) (UpdateOrganizationProfileRow, error) {
row := q.db.QueryRow(ctx, updateOrganizationProfile,
arg.Name,
arg.Description,
@@ -577,7 +598,7 @@ func (q *Queries) UpdateOrganizationProfile(ctx context.Context, arg UpdateOrgan
arg.ActorID,
arg.ManagerRoles,
)
var i Organization
var i UpdateOrganizationProfileRow
err := row.Scan(
&i.ID,
&i.Name,
@@ -11,6 +11,11 @@ import (
)
type Querier interface {
// 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.
AcceptInvitation(ctx context.Context, arg AcceptInvitationParams) (pgtype.UUID, error)
// Adds recipients to the day and the month row. The caller holds their
// locks from LockMailQuotaRows and has checked both limits.
AddMailQuotaUsage(ctx context.Context, arg AddMailQuotaUsageParams) (int64, error)
@@ -131,7 +136,16 @@ type Querier interface {
CountAssessmentSignalEvents(ctx context.Context, attemptID pgtype.UUID) (int64, error)
// Counts the hiding records of one candidate for the settings read.
CountCandidateHiddenOrganizations(ctx context.Context, userID pgtype.UUID) (int64, error)
// 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.
CountCompanyCardViewQuotaHoldingWindow(ctx context.Context, arg CountCompanyCardViewQuotaHoldingWindowParams) (CountCompanyCardViewQuotaHoldingWindowRow, error)
CountCompanyCardViewQuotaWindow(ctx context.Context, arg CountCompanyCardViewQuotaWindowParams) (CountCompanyCardViewQuotaWindowRow, error)
// 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.
CountCompanyInvitationQuotaHoldingWindow(ctx context.Context, arg CountCompanyInvitationQuotaHoldingWindowParams) (CountCompanyInvitationQuotaHoldingWindowRow, error)
CountCompanyInvitationQuotaWindow(ctx context.Context, arg CountCompanyInvitationQuotaWindowParams) (CountCompanyInvitationQuotaWindowRow, error)
// Counts the families of a category that have at least one published
// version, however many versions each has.
@@ -275,6 +289,9 @@ type Querier interface {
GetCandidateHiddenOrganization(ctx context.Context, arg GetCandidateHiddenOrganizationParams) (GetCandidateHiddenOrganizationRow, error)
GetConsentText(ctx context.Context, arg GetConsentTextParams) (ConsentText, error)
GetContactConsentRecordByInvitation(ctx context.Context, arg GetContactConsentRecordByInvitationParams) (GetContactConsentRecordByInvitationRow, error)
// Read the grant of one invitation of one organization. An invitation of
// another organization finds nothing.
GetContactGrantByInvitation(ctx context.Context, arg GetContactGrantByInvitationParams) (ContactGrant, error)
GetCurrentAssessmentCategoryOpening(ctx context.Context, category string) (AssessmentCategoryOpening, error)
// The newest unrevoked record of a purpose other than C4.
GetCurrentConsentRecord(ctx context.Context, arg GetCurrentConsentRecordParams) (GetCurrentConsentRecordRow, error)
@@ -304,6 +321,8 @@ type Querier interface {
GetEmailTokenByHash(ctx context.Context, arg GetEmailTokenByHashParams) (EmailToken, error)
// Explains why ConsumeEmailToken matched no row.
GetEmailTokenState(ctx context.Context, arg GetEmailTokenStateParams) (GetEmailTokenStateRow, error)
GetHoldingIDByFounder(ctx context.Context, founderAccountID pgtype.UUID) (pgtype.UUID, error)
GetInvitationAcceptResult(ctx context.Context, scopeHash []byte) (GetInvitationAcceptResultRow, error)
GetInvitationCreationResult(ctx context.Context, scopeHash []byte) (string, error)
GetInvitationDeclineResult(ctx context.Context, scopeHash []byte) (GetInvitationDeclineResultRow, error)
GetInvitationSendResult(ctx context.Context, scopeHash []byte) (GetInvitationSendResultRow, error)
@@ -470,6 +489,10 @@ type Querier interface {
// when no row returns; the insert then changes nothing and does not abort
// the transaction.
InsertContactConsentRecord(ctx context.Context, arg InsertContactConsentRecordParams) (InsertContactConsentRecordRow, error)
// 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.
InsertContactGrant(ctx context.Context, arg InsertContactGrantParams) error
// Stores a new delivery. A delivery with the same ID already exists when the
// same request is handled again; the statement then inserts nothing and
// returns 0 rows, and the stored content stays as it was. While another
@@ -477,10 +500,16 @@ type Querier interface {
// for it.
InsertDelivery(ctx context.Context, arg InsertDeliveryParams) (int64, error)
InsertEmailToken(ctx context.Context, arg InsertEmailTokenParams) error
// 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.
InsertHoldingIfAbsent(ctx context.Context, arg InsertHoldingIfAbsentParams) error
// The invitation row, its first terms version, and its first audit event are
// inserted in one caller-owned transaction. The deferred reference to the
// current terms version is checked when the transaction commits.
InsertInvitation(ctx context.Context, arg InsertInvitationParams) error
InsertInvitationAcceptResult(ctx context.Context, arg InsertInvitationAcceptResultParams) error
InsertInvitationDecline(ctx context.Context, arg InsertInvitationDeclineParams) error
InsertInvitationDeclineResult(ctx context.Context, arg InsertInvitationDeclineResultParams) error
InsertInvitationEvent(ctx context.Context, arg InsertInvitationEventParams) error
@@ -493,9 +522,9 @@ type Querier interface {
// unique key absorbs at-least-once redelivery without copying event content.
InsertNotificationInboxEvent(ctx context.Context, arg InsertNotificationInboxEventParams) (int64, error)
// 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.
InsertOrganization(ctx context.Context, arg InsertOrganizationParams) error
// The parent and first immutable snapshot are inserted in one caller-owned
// transaction. Deferred version references are checked when it commits.
@@ -741,10 +770,19 @@ type Querier interface {
// canceled, or reclaimed after the lease expired. While the lock is held,
// other workers skip the row, so the handler never runs twice at once.
LockClaimedScheduledJob(ctx context.Context, arg LockClaimedScheduledJobParams) (int64, error)
// 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.
LockCompanyQuotaHolding(ctx context.Context, holdingID pgtype.UUID) (pgtype.UUID, error)
LockCompanyQuotaOrganization(ctx context.Context, organizationID pgtype.UUID) (LockCompanyQuotaOrganizationRow, error)
// 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
// SHARE, so readers of one organization queue up instead of deadlocking on a
@@ -752,8 +790,9 @@ type Querier interface {
// 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.
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.
LockCompanyQuotaOrganizationFirst(ctx context.Context, organizationID pgtype.UUID) (LockCompanyQuotaOrganizationFirstRow, error)
// Lock the current C2 before locking the publication. C2 revocation updates
// this row before it locks the publication, so preserving this order avoids
// a lock inversion and makes the current consent stable through the read.
@@ -1086,7 +1125,7 @@ type Querier interface {
// follow it). No row returns for a stale version, an account that is no
// active manager, or an unknown organization; the adapter reads the
// membership to tell them apart.
UpdateOrganizationProfile(ctx context.Context, arg UpdateOrganizationProfileParams) (Organization, error)
UpdateOrganizationProfile(ctx context.Context, arg UpdateOrganizationProfileParams) (UpdateOrganizationProfileRow, error)
// Replaces the registration of an unverified account and advances the
// registration version in the same statement, so no caller can choose it.
// The version check rejects a writer that read an older row; a verified