Files

112 lines
3.6 KiB
SQL

-- 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);
-- Share-locks the consent C4 record of a grant for the duration of a contact
-- read. The read locks the record before the grant, the order that a C4
-- revocation follows when it stamps the record and then closes its grants. No
-- row means the record is revoked or belongs to another scope, which closes
-- the read.
-- name: LockCurrentContactConsentRecord :one
SELECT consent_records.id
FROM consent_records
WHERE
consent_records.id = sqlc.arg(consent_record_id)
AND consent_records.subject_id = sqlc.arg(candidate_id)
AND consent_records.organization_id = sqlc.arg(organization_id)
AND consent_records.purpose = 'c4'
AND consent_records.revoked_at IS NULL
FOR SHARE;
-- Share-locks one grant for the duration of a contact read and returns its
-- stored state. The caller decides whether the state still allows the read.
-- name: LockContactGrant :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 id = sqlc.arg(id)
FOR SHARE;
-- Closes the active grants of one candidate with one reason. A nil organization
-- closes every organization of the candidate, and a non-nil list of consent
-- records closes only the grants that those records gave. The grant rows are
-- locked in identifier order, so two closures that overlap take their locks in
-- the same order. The revocation time never precedes the grant time, which the
-- grant checks. A grant that is already closed keeps its first stamp.
-- name: CloseActiveContactGrants :execrows
WITH targets AS (
SELECT contact_grants.id
FROM contact_grants
WHERE
contact_grants.candidate_id = sqlc.arg(candidate_id)
AND (
sqlc.narg(organization_id)::uuid IS NULL
OR contact_grants.organization_id = sqlc.narg(organization_id)::uuid
)
AND (
sqlc.narg(consent_record_ids)::uuid [] IS NULL
OR contact_grants.consent_record_id = ANY(sqlc.narg(consent_record_ids)::uuid [])
)
AND contact_grants.revoked_at IS NULL
ORDER BY contact_grants.id
FOR NO KEY UPDATE
)
UPDATE contact_grants
SET
revoked_at = greatest(contact_grants.granted_at, sqlc.arg(revoked_at)::timestamptz),
revocation_reason = sqlc.arg(revocation_reason)
FROM targets
WHERE contact_grants.id = targets.id;