-- Queries of the candidate visibility settings: the publication of -- the card, the search status, the hidden organizations, and the history of -- these settings. Every statement is scoped by the candidate's user_id, and -- no statement selects another account's row. No statement reads or writes a -- profile value, a name, or a contact. A candidate with no row is closed, not -- looking, and hides nobody; no statement creates a row except an explicit -- act of the candidate. -- Reads the publication with the revocation state of the C2 record behind it -- in one statement, so the stored setting and the ledger come from one -- snapshot. The effective state is computed from them: published only while -- consent_revoked_at is NULL. No row means the candidate never published. -- name: ReadCandidatePublication :one SELECT candidate_publications.user_id, candidate_publications.state, candidate_publications.fields, candidate_publications.consent_record_id, candidate_publications.published_at, candidate_publications.closed_at, candidate_publications.version, candidate_publications.created_at, candidate_publications.updated_at, consent_records.revoked_at AS consent_revoked_at FROM candidate_publications INNER JOIN consent_records ON consent_records.id = candidate_publications.consent_record_id WHERE candidate_publications.user_id = sqlc.arg(user_id); -- Stores the first publication at version 1. No row returns when the account -- already has a row, so a first publication that lost a race with another -- one is answered as a version conflict. The foreign key refuses a record -- that is not a C2 record of this account. -- name: InsertCandidatePublication :one INSERT INTO candidate_publications ( user_id, state, fields, consent_record_id, published_at, closed_at, version, created_at, updated_at ) VALUES ( sqlc.arg(user_id), sqlc.arg(state), sqlc.arg(fields), sqlc.arg(consent_record_id), sqlc.arg(published_at), sqlc.narg(closed_at), 1, sqlc.arg(at), sqlc.arg(at) ) ON CONFLICT (user_id) DO NOTHING RETURNING user_id, version; -- Replaces the publication over the version the caller read and advances the -- version in the same statement. No row returns for a stale version, which -- the caller answers as a version conflict. The update time never moves back, -- as in the profile. -- name: UpdateCandidatePublication :one UPDATE candidate_publications SET state = sqlc.arg(state), fields = sqlc.arg(fields), consent_record_id = sqlc.arg(consent_record_id), published_at = sqlc.arg(published_at), closed_at = sqlc.narg(closed_at), version = candidate_publications.version + 1, updated_at = greatest(candidate_publications.updated_at, sqlc.arg(at)) WHERE candidate_publications.user_id = sqlc.arg(user_id) AND candidate_publications.version = sqlc.arg(expected_version) RETURNING user_id, version; -- No row means the status was never set, which reads as not looking. The -- lapse is computed by the caller from lapses_at and its clock and is never -- written. -- name: ReadCandidateSearchStatus :one SELECT user_id, status, confirmed_at, lapses_at, policy_version_id, version, created_at, updated_at FROM candidate_search_statuses WHERE user_id = sqlc.arg(user_id); -- Stores the first status at version 1; no row returns when one exists. -- name: InsertCandidateSearchStatus :one INSERT INTO candidate_search_statuses ( user_id, status, confirmed_at, lapses_at, policy_version_id, version, created_at, updated_at ) VALUES ( sqlc.arg(user_id), sqlc.arg(status), sqlc.narg(confirmed_at), sqlc.narg(lapses_at), sqlc.narg(policy_version_id), 1, sqlc.arg(at), sqlc.arg(at) ) ON CONFLICT (user_id) DO NOTHING RETURNING user_id, version; -- Replaces the status over the version the caller read. Looking also stores -- the new confirmation, lapse time, and policy version; not looking clears -- them. It never names the publication or the hidden organizations. -- name: UpdateCandidateSearchStatus :one UPDATE candidate_search_statuses SET status = sqlc.arg(status), confirmed_at = sqlc.narg(confirmed_at), lapses_at = sqlc.narg(lapses_at), policy_version_id = sqlc.narg(policy_version_id), version = candidate_search_statuses.version + 1, updated_at = greatest(candidate_search_statuses.updated_at, sqlc.arg(at)) WHERE candidate_search_statuses.user_id = sqlc.arg(user_id) AND candidate_search_statuses.version = sqlc.arg(expected_version) RETURNING user_id, version; -- Hides one organization. No row returns when the pair already exists, and -- the caller reads the existing record with its original time. The foreign key -- refuses an unknown organization (SQLSTATE 23503); the caller answers it -- exactly like an organization the candidate cannot see. The trigger refuses -- the 1001st record (candidate_hidden_organizations_limit). The trigger runs -- before the conflict check, so the caller reads the existing record first -- and a repeat at the limit is never refused. -- name: InsertCandidateHiddenOrganization :one INSERT INTO candidate_hidden_organizations ( user_id, organization_id, hidden_at ) VALUES ( sqlc.arg(user_id), sqlc.arg(organization_id), sqlc.arg(at) ) ON CONFLICT (user_id, organization_id) DO NOTHING RETURNING user_id, organization_id, hidden_at; -- Reads one hiding of the candidate with the current name of the -- organization. -- name: GetCandidateHiddenOrganization :one SELECT candidate_hidden_organizations.organization_id, organizations.name AS organization_name, candidate_hidden_organizations.hidden_at FROM candidate_hidden_organizations INNER JOIN organizations ON organizations.id = candidate_hidden_organizations.organization_id WHERE candidate_hidden_organizations.user_id = sqlc.arg(user_id) AND candidate_hidden_organizations.organization_id = sqlc.arg(organization_id); -- Lifts one hiding. No row returns when the organization was not hidden, so -- the caller writes the history event only for a real deletion and answers -- 204 either way. -- name: DeleteCandidateHiddenOrganization :one DELETE FROM candidate_hidden_organizations WHERE user_id = sqlc.arg(user_id) AND organization_id = sqlc.arg(organization_id) RETURNING organization_id; -- One page of the candidate's hidden organizations, newest first with the -- organization identifier as the tie-breaker. The page continues after the -- (hidden_at, organization_id) position of the last record of the previous -- page. -- name: ListCandidateHiddenOrganizations :many SELECT candidate_hidden_organizations.organization_id, organizations.name AS organization_name, candidate_hidden_organizations.hidden_at FROM candidate_hidden_organizations INNER JOIN organizations ON organizations.id = candidate_hidden_organizations.organization_id WHERE candidate_hidden_organizations.user_id = sqlc.arg(user_id) AND ( sqlc.narg(after_hidden_at)::timestamptz IS NULL OR ( candidate_hidden_organizations.hidden_at, candidate_hidden_organizations.organization_id ) < ( sqlc.narg(after_hidden_at)::timestamptz, sqlc.narg(after_organization_id)::uuid ) ) ORDER BY candidate_hidden_organizations.hidden_at DESC, candidate_hidden_organizations.organization_id DESC LIMIT sqlc.arg(row_limit); -- Counts the hiding records of one candidate for the settings read. -- name: CountCandidateHiddenOrganizations :one SELECT count(*) AS total FROM candidate_hidden_organizations WHERE user_id = sqlc.arg(user_id); -- Appends one event to the settings history. The row holds identifiers, card -- field codes, and times, never a name; the caller -- writes it in the transaction of the change it records. -- name: InsertCandidatePrivacyEvent :exec INSERT INTO candidate_privacy_events ( id, user_id, kind, occurred_at, fields, consent_record_id, organization_id ) VALUES ( sqlc.arg(id), sqlc.arg(user_id), sqlc.arg(kind), sqlc.arg(occurred_at), sqlc.narg(fields), sqlc.narg(consent_record_id), sqlc.narg(organization_id) ); -- One page of the candidate's settings history, newest first with the event -- identifier as the tie-breaker. The organization name is resolved here, at -- read time, and is the current name. The page continues after the -- (occurred_at, id) position of the last event of the previous page. -- name: ListCandidatePrivacyEvents :many SELECT candidate_privacy_events.id, candidate_privacy_events.kind, candidate_privacy_events.occurred_at, candidate_privacy_events.fields, candidate_privacy_events.consent_record_id, candidate_privacy_events.organization_id, organizations.name AS organization_name FROM candidate_privacy_events LEFT JOIN organizations ON organizations.id = candidate_privacy_events.organization_id WHERE candidate_privacy_events.user_id = sqlc.arg(user_id) AND ( sqlc.narg(after_occurred_at)::timestamptz IS NULL OR (candidate_privacy_events.occurred_at, candidate_privacy_events.id) < ( sqlc.narg(after_occurred_at)::timestamptz, sqlc.narg(after_id)::uuid ) ) ORDER BY candidate_privacy_events.occurred_at DESC, candidate_privacy_events.id DESC LIMIT sqlc.arg(row_limit); -- Takes the candidate's current unrevoked C2 record for a publication and -- keeps it locked until the transaction ends. The share lock conflicts with -- the revocation stamp (an UPDATE of the row), so a revocation either commits -- before this statement, which then returns no row and the publication is -- refused, or waits until the publication transaction commits and closes the -- card right after. The caller must run it in the transaction that writes the -- publication and must re-read the effective state (ReadCandidatePublication) -- in that transaction to build its response. -- name: LockCurrentC2Record :one SELECT id FROM consent_records WHERE subject_id = sqlc.arg(subject_id) AND purpose = 'c2' AND revoked_at IS NULL ORDER BY granted_at DESC, id DESC LIMIT 1 FOR SHARE; -- Locks the candidate's published row only when it refers to a C2 record -- revoked by this transaction. The setting remains linked to that immutable -- record; ReadCandidatePublication computes the effective closed state and -- keeps the consent-revoked reason after later grants. The returned time is -- the effective close time used by the privacy history event. -- name: LockCandidatePublicationForConsentRevocation :one SELECT greatest(candidate_publications.published_at, consent_records.revoked_at)::timestamptz AS closed_at FROM candidate_publications INNER JOIN consent_records ON consent_records.id = candidate_publications.consent_record_id WHERE candidate_publications.user_id = sqlc.arg(user_id) AND candidate_publications.state = 'published' AND consent_records.id = ANY(sqlc.arg(consent_record_ids)::uuid []) AND consent_records.subject_id = candidate_publications.user_id AND consent_records.purpose = 'c2' AND consent_records.revoked_at IS NOT NULL FOR UPDATE OF candidate_publications; -- The organization-facing read of a candidate's publication: the effective -- view (published and backed by an unrevoked C2) and no hiding of the reading -- organization. No row means the organization reads nothing, whichever of -- the reasons applies, so it cannot tell a hidden candidate from an -- unpublished one. Every organization-facing read of a publication starts -- from this shape. -- name: ReadPublicationVisibleToOrganization :one SELECT effective.user_id, effective.fields, effective.published_at FROM candidate_effective_publications AS effective WHERE effective.user_id = sqlc.arg(user_id) AND NOT exists( SELECT 1 FROM candidate_hidden_organizations AS hidden WHERE hidden.user_id = effective.user_id AND hidden.organization_id = sqlc.arg(organization_id) );