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

@@ -0,0 +1,347 @@
-- +goose Up
-- The candidate's acceptance of the current terms with the consent C4. One
-- transaction moves a delivered or viewed invitation to `accepted` before its
-- deadline, records the consent C4 in the consent ledger, stores the contact
-- grant that covers exactly the selected fields, and appends the audit event.
-- The acceptance never changes the publication (C2) and never lifts a hiding:
-- a grant alone does not make the contacts readable, and the read model shows
-- whether the organization may read them now.
ALTER TABLE invitation_events
DROP CONSTRAINT invitation_events_kind_known;
ALTER TABLE invitation_events
ADD CONSTRAINT invitation_events_kind_known CHECK (
kind IN (
'draft_created', 'draft_updated', 'delivered', 'viewed', 'declined',
'accepted'
)
);
ALTER TABLE invitation_events
DROP CONSTRAINT invitation_events_answer_changes_no_field;
ALTER TABLE invitation_events
ADD CONSTRAINT invitation_events_answer_changes_no_field CHECK (
kind NOT IN ('viewed', 'declined', 'accepted') OR cardinality(changed_fields) = 0
);
-- An accepted invitation records its answer, as a declined one does. The
-- acceptance from `delivered` leaves the view empty.
ALTER TABLE invitations
DROP CONSTRAINT invitations_answer_matches_state;
ALTER TABLE invitations
ADD CONSTRAINT invitations_answer_matches_state CHECK (
(state <> 'delivered' OR (viewed_at IS NULL AND responded_at IS NULL))
AND (state <> 'viewed' OR (viewed_at IS NOT NULL AND responded_at IS NULL))
AND (state NOT IN ('declined', 'accepted') OR responded_at IS NOT NULL)
AND (viewed_at IS NULL OR (delivered_at IS NOT NULL AND viewed_at >= delivered_at))
AND (
responded_at IS NULL
OR (delivered_at IS NOT NULL AND responded_at >= delivered_at)
)
);
-- The contact grant names the invitation together with its candidate, so the
-- grant can only belong to the candidate the invitation was addressed to.
ALTER TABLE invitations
ADD CONSTRAINT invitations_organization_id_id_candidate_id_key UNIQUE (
organization_id, id, candidate_id
);
-- A grant names exactly the C4 record of its acceptance: the same subject,
-- organization, invitation, and terms version. The record stays a ledger row
-- of its own and does not reference the invitation tables, which the ledger
-- migration leaves to a later change.
ALTER TABLE consent_records
ADD CONSTRAINT consent_records_contact_scope_key UNIQUE (
id, subject_id, organization_id, invitation_id, invitation_terms_version
);
-- The contact grant of one accepted invitation. It holds kinds only, never
-- contact values. The grant is created by the acceptance next to its C4 record
-- and the accepted state. Its revocation stamp is reserved for the revocation
-- procedure of a later change, and this migration never sets it.
CREATE TABLE contact_grants (
id uuid NOT NULL,
organization_id uuid NOT NULL,
candidate_id uuid NOT NULL,
source_invitation_id uuid NOT NULL,
consent_record_id uuid NOT NULL,
terms_version bigint NOT NULL,
fields text [] NOT NULL,
consent_text_version text NOT NULL,
granted_at timestamptz NOT NULL,
revoked_at timestamptz,
revocation_reason text,
CONSTRAINT contact_grants_pkey PRIMARY KEY (id),
-- One grant per accepted invitation, and one per consent record.
CONSTRAINT contact_grants_source_invitation_key UNIQUE (source_invitation_id),
CONSTRAINT contact_grants_consent_record_key UNIQUE (consent_record_id),
CONSTRAINT contact_grants_candidate_fkey FOREIGN KEY (candidate_id)
REFERENCES users (id) ON DELETE RESTRICT,
CONSTRAINT contact_grants_invitation_fkey FOREIGN KEY (
organization_id, source_invitation_id, candidate_id
) REFERENCES invitations (organization_id, id, candidate_id) ON DELETE RESTRICT,
CONSTRAINT contact_grants_terms_fkey FOREIGN KEY (
source_invitation_id, terms_version
) REFERENCES invitation_terms_versions (invitation_id, version) ON DELETE RESTRICT,
CONSTRAINT contact_grants_consent_fkey FOREIGN KEY (
consent_record_id,
candidate_id,
organization_id,
source_invitation_id,
terms_version
) REFERENCES consent_records (
id,
subject_id,
organization_id,
invitation_id,
invitation_terms_version
) ON DELETE RESTRICT,
CONSTRAINT contact_grants_terms_version_positive CHECK (terms_version > 0),
CONSTRAINT contact_grants_fields_canonical CHECK (
consent_field_set_is_canonical(fields)
),
CONSTRAINT contact_grants_consent_text_version_format CHECK (
consent_text_version ~ '^[A-Za-z0-9._-]{1,64}$'
),
CONSTRAINT contact_grants_revocation_stamp CHECK (
(revoked_at IS NULL) = (revocation_reason IS NULL)
AND (
revocation_reason IS NULL
OR revocation_reason IN (
'consent_revoked', 'participation_cancelled', 'visibility_closed'
)
)
AND (revoked_at IS NULL OR revoked_at >= granted_at)
)
);
-- The grant of an organization's invitations is read for one candidate while
-- the grant is active.
CREATE INDEX contact_grants_organization_candidate_active_idx
ON contact_grants (organization_id, candidate_id)
WHERE revoked_at IS NULL;
-- Outcome of an idempotent acceptance, kept beside the generic idempotency
-- record and removed with it after the 24-hour replay window. An accepted
-- invitation keeps its answer and its grant, so a replay rebuilds the response
-- from them and the outcome holds only the invitation and the result.
CREATE TABLE invitation_accept_results (
scope_hash bytea NOT NULL,
invitation_id uuid NOT NULL,
outcome text NOT NULL,
CONSTRAINT invitation_accept_results_pkey PRIMARY KEY (scope_hash),
CONSTRAINT invitation_accept_results_idempotency_fkey FOREIGN KEY (
scope_hash
) REFERENCES idempotency_records (scope_hash) ON DELETE CASCADE,
CONSTRAINT invitation_accept_results_scope_hash_sha256 CHECK (
octet_length(scope_hash) = 32
),
CONSTRAINT invitation_accept_results_outcome CHECK (
outcome IN ('accepted', 'not_found', 'state_conflict')
)
);
-- A view, an answer, and an acceptance are never undone, and a declined or
-- accepted invitation never returns to an earlier state.
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION invitations_guard_answer() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF (OLD.viewed_at IS NOT NULL AND NEW.viewed_at IS DISTINCT FROM OLD.viewed_at)
OR (OLD.responded_at IS NOT NULL AND NEW.responded_at IS DISTINCT FROM OLD.responded_at)
OR (OLD.state = 'declined' AND NEW.state <> 'declined')
OR (OLD.state = 'accepted' AND NEW.state IN ('draft', 'delivered', 'viewed', 'counter', 'declined')) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'invitations_answer_immutable',
TABLE = 'invitations',
MESSAGE = 'a view, an answer, a decline, and an acceptance are never undone';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
-- The grant must agree with its C4 record: the fields, the text version, and
-- the grant time are the record's own, so a grant cannot cover a field its
-- consent did not name.
-- +goose StatementBegin
CREATE FUNCTION contact_grants_match_consent() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM consent_records AS record
WHERE
record.id = NEW.consent_record_id
AND record.purpose = 'c4'
AND record.field_set = NEW.fields
AND record.text_version = NEW.consent_text_version
AND record.granted_at = NEW.granted_at
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'contact_grants_match_consent',
TABLE = 'contact_grants',
MESSAGE = 'a contact grant agrees with its consent C4 record';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER contact_grants_match_consent
BEFORE INSERT ON contact_grants
FOR EACH ROW EXECUTE FUNCTION contact_grants_match_consent();
-- A grant changes only by its one revocation stamp, which is set once. Every
-- other change, and every delete, is refused.
-- +goose StatementBegin
CREATE FUNCTION contact_grants_guard_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF OLD.revoked_at IS NOT NULL
OR NEW.revoked_at IS NULL
OR (
NEW.id, NEW.organization_id, NEW.candidate_id, NEW.source_invitation_id,
NEW.consent_record_id, NEW.terms_version, NEW.fields,
NEW.consent_text_version, NEW.granted_at
) IS DISTINCT FROM (
OLD.id, OLD.organization_id, OLD.candidate_id, OLD.source_invitation_id,
OLD.consent_record_id, OLD.terms_version, OLD.fields,
OLD.consent_text_version, OLD.granted_at
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'contact_grants_immutable',
TABLE = 'contact_grants',
MESSAGE = 'contact grants are append-only: only an active grant can be revoked, once';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER contact_grants_guard_change
BEFORE UPDATE ON contact_grants
FOR EACH ROW EXECUTE FUNCTION contact_grants_guard_change();
CREATE TRIGGER contact_grants_refuse_delete
BEFORE DELETE ON contact_grants
FOR EACH ROW EXECUTE FUNCTION invitations_refuse_history_change();
-- An accepted invitation has its contact grant by the end of the transaction
-- that accepted it.
-- +goose StatementBegin
CREATE FUNCTION invitations_require_contact_grant() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM contact_grants
WHERE
contact_grants.organization_id = NEW.organization_id
AND contact_grants.source_invitation_id = NEW.id
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'invitations_acceptance_recorded',
TABLE = 'invitations',
MESSAGE = 'an accepted invitation records its contact grant';
END IF;
RETURN NULL;
END;
$$;
-- +goose StatementEnd
CREATE CONSTRAINT TRIGGER invitations_require_contact_grant
AFTER UPDATE ON invitations
DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW
WHEN (new.state = 'accepted' AND old.state <> 'accepted')
EXECUTE FUNCTION invitations_require_contact_grant();
-- +goose Down
-- A rollback must not silently remove an acceptance, its consent, or its grant.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM invitation_events WHERE kind = 'accepted')
OR EXISTS (SELECT 1 FROM invitations WHERE state = 'accepted')
OR EXISTS (SELECT 1 FROM contact_grants)
OR EXISTS (SELECT 1 FROM consent_records WHERE purpose = 'c4')
OR EXISTS (SELECT 1 FROM invitation_accept_results) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'invitation_acceptances_retained',
TABLE = 'invitation_events',
MESSAGE = 'accepted invitations and their consents must be resolved before rollback';
END IF;
END;
$$;
-- +goose StatementEnd
DROP TRIGGER invitations_require_contact_grant ON invitations;
DROP FUNCTION invitations_require_contact_grant();
DROP TABLE invitation_accept_results;
DROP TABLE contact_grants;
DROP FUNCTION contact_grants_guard_change();
DROP FUNCTION contact_grants_match_consent();
ALTER TABLE consent_records
DROP CONSTRAINT consent_records_contact_scope_key;
ALTER TABLE invitations
DROP CONSTRAINT invitations_organization_id_id_candidate_id_key;
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION invitations_guard_answer() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF (OLD.viewed_at IS NOT NULL AND NEW.viewed_at IS DISTINCT FROM OLD.viewed_at)
OR (OLD.responded_at IS NOT NULL AND NEW.responded_at IS DISTINCT FROM OLD.responded_at)
OR (OLD.state = 'declined' AND NEW.state <> 'declined') THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'invitations_answer_immutable',
TABLE = 'invitations',
MESSAGE = 'a view, an answer, and a decline are never undone';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
ALTER TABLE invitations
DROP CONSTRAINT invitations_answer_matches_state;
ALTER TABLE invitations
ADD CONSTRAINT invitations_answer_matches_state CHECK (
(state <> 'delivered' OR (viewed_at IS NULL AND responded_at IS NULL))
AND (state <> 'viewed' OR (viewed_at IS NOT NULL AND responded_at IS NULL))
AND (state <> 'declined' OR responded_at IS NOT NULL)
AND (viewed_at IS NULL OR (delivered_at IS NOT NULL AND viewed_at >= delivered_at))
AND (
responded_at IS NULL
OR (delivered_at IS NOT NULL AND responded_at >= delivered_at)
)
);
ALTER TABLE invitation_events
DROP CONSTRAINT invitation_events_answer_changes_no_field;
ALTER TABLE invitation_events
ADD CONSTRAINT invitation_events_answer_changes_no_field CHECK (
kind NOT IN ('viewed', 'declined') OR cardinality(changed_fields) = 0
);
ALTER TABLE invitation_events
DROP CONSTRAINT invitation_events_kind_known;
ALTER TABLE invitation_events
ADD CONSTRAINT invitation_events_kind_known CHECK (
kind IN ('draft_created', 'draft_updated', 'delivered', 'viewed', 'declined')
);
@@ -0,0 +1,134 @@
-- +goose Up
-- Holdings group the organizations that one account founded, the way the
-- divisions of one company belong to one parent. A holding has no profile and
-- grants no right. It only names the scope of the E0 quota pool: the E0
-- organizations of one holding share one pool, so creating more organizations
-- does not multiply the unverified allowance. An organization at E1 or above
-- keeps its own pool and does not use its holding for quotas.
--
-- A holding belongs to its founder account, the account that created its first
-- organization. Every organization belongs to exactly one holding. Holdings of
-- different founders are never merged.
CREATE TABLE holdings (
-- Internal identifier, never shown by the API. The application creates it
-- as UUIDv7; the backfill below uses a random UUID.
id uuid NOT NULL,
founder_account_id uuid NOT NULL,
created_at timestamptz NOT NULL,
CONSTRAINT holdings_pkey PRIMARY KEY (id),
-- One holding per founder. A second creation of the same account that
-- races the first waits on this key and then reads the stored holding.
CONSTRAINT holdings_founder_account_id_key UNIQUE (founder_account_id),
CONSTRAINT holdings_founder_account_id_fkey FOREIGN KEY (
founder_account_id
) REFERENCES users (id) ON DELETE RESTRICT
);
-- The backfill and the NOT NULL constraint must see every organization and
-- membership. Writers of both tables wait until this migration commits.
LOCK TABLE organizations, memberships IN SHARE ROW EXCLUSIVE MODE;
-- Refuses an organization without any membership, because its founder cannot
-- be found. Creation stores the owner membership in the same transaction as
-- the organization, and no statement deletes memberships, so an existing
-- database should never reach this check.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM organizations AS organization
WHERE NOT EXISTS (
SELECT 1
FROM memberships AS membership
WHERE membership.organization_id = organization.id
)
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'organizations_founder_required',
TABLE = 'organizations',
MESSAGE = 'an organization without a membership has no founder for its holding';
END IF;
END;
$$;
-- +goose StatementEnd
ALTER TABLE organizations ADD COLUMN holding_id uuid;
-- The founder of an organization is the account of its earliest membership.
-- Creation inserts the organization and its owner membership in one
-- transaction, and a membership cannot exist before its organization, so the
-- earliest membership is the creator's. Ties are broken by identifier. A holding
-- is dated by the earliest creation of its founder.
WITH founders AS (
SELECT DISTINCT ON (membership.organization_id)
membership.organization_id,
membership.user_id
FROM memberships AS membership
ORDER BY membership.organization_id, membership.created_at, membership.id
)
INSERT INTO holdings (id, founder_account_id, created_at)
SELECT
gen_random_uuid(),
founders.user_id,
min(organization.created_at)
FROM founders
INNER JOIN organizations AS organization ON organization.id = founders.organization_id
GROUP BY founders.user_id;
WITH founders AS (
SELECT DISTINCT ON (membership.organization_id)
membership.organization_id,
membership.user_id
FROM memberships AS membership
ORDER BY membership.organization_id, membership.created_at, membership.id
)
UPDATE organizations AS organization
SET holding_id = holding.id
FROM founders
INNER JOIN holdings AS holding ON holding.founder_account_id = founders.user_id
WHERE founders.organization_id = organization.id;
ALTER TABLE organizations ALTER COLUMN holding_id SET NOT NULL;
ALTER TABLE organizations
ADD CONSTRAINT organizations_holding_id_fkey FOREIGN KEY (holding_id)
REFERENCES holdings (id) ON DELETE RESTRICT;
-- Serves the pool of an E0 organization, which reads the organizations of its
-- holding, and the foreign key.
CREATE INDEX organizations_holding_id_idx ON organizations (holding_id);
-- +goose Down
-- A rollback removes the grouping of organizations into holdings. It refuses
-- while a holding groups several organizations, because the schema without
-- holdings would count their E0 quotas separately and could not keep the pool
-- they share. The lock stops a concurrent creation from joining a holding
-- between the check and the removal.
LOCK TABLE organizations IN SHARE ROW EXCLUSIVE MODE;
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM organizations
GROUP BY holding_id
HAVING count(*) > 1
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'organizations_holding_grouped',
TABLE = 'organizations',
MESSAGE = 'holdings with several organizations must be resolved before rollback';
END IF;
END;
$$;
-- +goose StatementEnd
ALTER TABLE organizations DROP COLUMN holding_id;
DROP TABLE holdings;
+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