420 lines
11 KiB
SQL
420 lines
11 KiB
SQL
-- name: GetConsentText :one
|
|
SELECT
|
|
purpose,
|
|
version,
|
|
body,
|
|
body_sha256,
|
|
kind,
|
|
published_at
|
|
FROM consent_texts
|
|
WHERE
|
|
purpose = sqlc.arg(purpose)
|
|
AND version = sqlc.arg(version);
|
|
|
|
-- The text of a purpose that applies at a time: the latest publication not
|
|
-- after it.
|
|
-- name: GetCurrentConsentText :one
|
|
SELECT
|
|
purpose,
|
|
version,
|
|
body,
|
|
body_sha256,
|
|
kind,
|
|
published_at
|
|
FROM consent_texts
|
|
WHERE
|
|
purpose = sqlc.arg(purpose)
|
|
AND published_at <= sqlc.arg(at)
|
|
ORDER BY published_at DESC
|
|
LIMIT 1;
|
|
|
|
-- Records a grant of C1, C2, C3, or C5. An unrevoked record of the same
|
|
-- subject, purpose, and text version already exists when no row returns;
|
|
-- the insert then changes nothing and does not abort the transaction.
|
|
-- name: InsertConsentRecord :one
|
|
INSERT INTO consent_records (
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
granted_at
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(subject_id),
|
|
sqlc.arg(purpose),
|
|
sqlc.arg(text_version),
|
|
sqlc.arg(text_hash),
|
|
sqlc.arg(source),
|
|
sqlc.narg(registration_version)::bigint,
|
|
sqlc.arg(granted_at)
|
|
)
|
|
ON CONFLICT (subject_id, purpose, text_version)
|
|
WHERE revoked_at IS NULL AND purpose <> 'c4'
|
|
DO NOTHING
|
|
RETURNING
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at,
|
|
revoked_at,
|
|
revocation_source,
|
|
(
|
|
CASE
|
|
WHEN exists(
|
|
SELECT 1 FROM consent_records AS newer
|
|
WHERE
|
|
newer.subject_id = consent_records.subject_id
|
|
AND newer.purpose = consent_records.purpose
|
|
AND newer.revoked_at IS NULL
|
|
AND (newer.granted_at, newer.id)
|
|
> (consent_records.granted_at, consent_records.id)
|
|
) THEN 'superseded'
|
|
ELSE 'active'
|
|
END
|
|
)::text AS state;
|
|
|
|
-- Records a C4 grant with its scope. The invitation already has a C4 record
|
|
-- when no row returns; the insert then changes nothing and does not abort
|
|
-- the transaction.
|
|
-- name: InsertContactConsentRecord :one
|
|
INSERT INTO consent_records (
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(subject_id),
|
|
'c4',
|
|
sqlc.arg(text_version),
|
|
sqlc.arg(text_hash),
|
|
'invitation_acceptance',
|
|
NULL,
|
|
sqlc.arg(organization_id),
|
|
sqlc.arg(organization_name),
|
|
sqlc.arg(field_set)::text [],
|
|
sqlc.arg(invitation_id),
|
|
sqlc.arg(invitation_terms_version),
|
|
sqlc.arg(granted_at)
|
|
)
|
|
ON CONFLICT (invitation_id) WHERE invitation_id IS NOT NULL
|
|
DO NOTHING
|
|
RETURNING
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at,
|
|
revoked_at,
|
|
revocation_source,
|
|
'active'::text AS state;
|
|
|
|
-- name: GetUnrevokedConsentRecordByVersion :one
|
|
SELECT
|
|
record.id,
|
|
record.subject_id,
|
|
record.purpose,
|
|
record.text_version,
|
|
record.text_hash,
|
|
record.source,
|
|
record.registration_version,
|
|
record.organization_id,
|
|
record.organization_name,
|
|
record.field_set,
|
|
record.invitation_id,
|
|
record.invitation_terms_version,
|
|
record.granted_at,
|
|
record.revoked_at,
|
|
record.revocation_source,
|
|
(
|
|
CASE
|
|
WHEN exists(
|
|
SELECT 1 FROM consent_records AS newer
|
|
WHERE
|
|
newer.subject_id = record.subject_id
|
|
AND newer.purpose = record.purpose
|
|
AND newer.revoked_at IS NULL
|
|
AND (newer.granted_at, newer.id)
|
|
> (record.granted_at, record.id)
|
|
) THEN 'superseded'
|
|
ELSE 'active'
|
|
END
|
|
)::text AS state
|
|
FROM consent_records AS record
|
|
WHERE
|
|
record.subject_id = sqlc.arg(subject_id)
|
|
AND record.purpose = sqlc.arg(purpose)
|
|
AND record.text_version = sqlc.arg(text_version)
|
|
AND record.revoked_at IS NULL;
|
|
|
|
-- name: GetContactConsentRecordByInvitation :one
|
|
SELECT
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at,
|
|
revoked_at,
|
|
revocation_source,
|
|
(
|
|
CASE WHEN revoked_at IS NULL THEN 'active' ELSE 'revoked' END
|
|
)::text AS state
|
|
FROM consent_records
|
|
WHERE
|
|
subject_id = sqlc.arg(subject_id)
|
|
AND invitation_id = sqlc.arg(invitation_id);
|
|
|
|
-- The newest unrevoked record of a purpose other than C4.
|
|
-- name: GetCurrentConsentRecord :one
|
|
SELECT
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at,
|
|
revoked_at,
|
|
revocation_source,
|
|
'active'::text AS state
|
|
FROM consent_records
|
|
WHERE
|
|
subject_id = sqlc.arg(subject_id)
|
|
AND purpose = sqlc.arg(purpose)
|
|
AND purpose <> 'c4'
|
|
AND revoked_at IS NULL
|
|
ORDER BY granted_at DESC, id DESC
|
|
LIMIT 1;
|
|
|
|
-- Locks the active registration C1 while its pending registration is applied.
|
|
-- name: LockRegistrationConsentRecord :one
|
|
SELECT
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at,
|
|
revoked_at,
|
|
revocation_source,
|
|
'active'::text AS state
|
|
FROM consent_records
|
|
WHERE
|
|
subject_id = sqlc.arg(subject_id)
|
|
AND purpose = 'c1'
|
|
AND source = 'registration'
|
|
AND registration_version = sqlc.arg(registration_version)
|
|
AND revoked_at IS NULL
|
|
FOR UPDATE;
|
|
|
|
-- name: ListCurrentContactConsentRecords :many
|
|
SELECT
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at,
|
|
revoked_at,
|
|
revocation_source,
|
|
'active'::text AS state
|
|
FROM consent_records
|
|
WHERE
|
|
subject_id = sqlc.arg(subject_id)
|
|
AND purpose = 'c4'
|
|
AND organization_id = sqlc.arg(organization_id)
|
|
AND revoked_at IS NULL
|
|
ORDER BY granted_at DESC, id DESC;
|
|
|
|
-- Revokes every unrevoked record of a purpose other than C4, superseded ones
|
|
-- included, and returns them. A revocation time before the grant time, from
|
|
-- clock skew between instances, is stamped at the grant time instead.
|
|
-- name: RevokeConsentRecords :many
|
|
UPDATE consent_records
|
|
SET
|
|
revoked_at = greatest(sqlc.arg(revoked_at)::timestamptz, granted_at),
|
|
revocation_source = sqlc.arg(revocation_source)
|
|
WHERE
|
|
subject_id = sqlc.arg(subject_id)
|
|
AND purpose = sqlc.arg(purpose)
|
|
AND purpose <> 'c4'
|
|
AND revoked_at IS NULL
|
|
RETURNING
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at,
|
|
revoked_at,
|
|
revocation_source,
|
|
'revoked'::text AS state;
|
|
|
|
-- Revokes every unrevoked C4 record of the subject for one organization and
|
|
-- returns them, with the same clamp of the revocation time as above.
|
|
-- name: RevokeContactConsentRecords :many
|
|
UPDATE consent_records
|
|
SET
|
|
revoked_at = greatest(sqlc.arg(revoked_at)::timestamptz, granted_at),
|
|
revocation_source = sqlc.arg(revocation_source)
|
|
WHERE
|
|
subject_id = sqlc.arg(subject_id)
|
|
AND purpose = 'c4'
|
|
AND organization_id = sqlc.arg(organization_id)
|
|
AND revoked_at IS NULL
|
|
RETURNING
|
|
id,
|
|
subject_id,
|
|
purpose,
|
|
text_version,
|
|
text_hash,
|
|
source,
|
|
registration_version,
|
|
organization_id,
|
|
organization_name,
|
|
field_set,
|
|
invitation_id,
|
|
invitation_terms_version,
|
|
granted_at,
|
|
revoked_at,
|
|
revocation_source,
|
|
'revoked'::text AS state;
|
|
|
|
-- One page of the subject's ledger, newest grant first, optionally limited
|
|
-- to one state. Every record query is scoped by the subject, so a caller
|
|
-- never reads or revokes another subject's consent. The state is computed,
|
|
-- never stored: revoked once revoked_at is set; superseded when a newer
|
|
-- unrevoked record of the same subject and purpose exists (never for C4,
|
|
-- whose records are scoped by invitation); active otherwise. The page continues after the (granted_at, id) position of
|
|
-- the last record of the previous page.
|
|
-- name: ListConsentRecords :many
|
|
WITH ledger AS (
|
|
SELECT
|
|
record.id,
|
|
record.subject_id,
|
|
record.purpose,
|
|
record.text_version,
|
|
record.text_hash,
|
|
record.source,
|
|
record.registration_version,
|
|
record.organization_id,
|
|
record.organization_name,
|
|
record.field_set,
|
|
record.invitation_id,
|
|
record.invitation_terms_version,
|
|
record.granted_at,
|
|
record.revoked_at,
|
|
record.revocation_source,
|
|
(
|
|
CASE
|
|
WHEN record.revoked_at IS NOT NULL THEN 'revoked'
|
|
WHEN record.purpose <> 'c4' AND exists(
|
|
SELECT 1 FROM consent_records AS newer
|
|
WHERE
|
|
newer.subject_id = record.subject_id
|
|
AND newer.purpose = record.purpose
|
|
AND newer.revoked_at IS NULL
|
|
AND (newer.granted_at, newer.id)
|
|
> (record.granted_at, record.id)
|
|
) THEN 'superseded'
|
|
ELSE 'active'
|
|
END
|
|
)::text AS state
|
|
FROM consent_records AS record
|
|
WHERE record.subject_id = sqlc.arg(subject_id)
|
|
)
|
|
|
|
SELECT
|
|
ledger.id,
|
|
ledger.subject_id,
|
|
ledger.purpose,
|
|
ledger.text_version,
|
|
ledger.text_hash,
|
|
ledger.source,
|
|
ledger.registration_version,
|
|
ledger.organization_id,
|
|
ledger.organization_name,
|
|
ledger.field_set,
|
|
ledger.invitation_id,
|
|
ledger.invitation_terms_version,
|
|
ledger.granted_at,
|
|
ledger.revoked_at,
|
|
ledger.revocation_source,
|
|
ledger.state
|
|
FROM ledger
|
|
WHERE
|
|
(sqlc.narg(state)::text IS NULL OR ledger.state = sqlc.narg(state)::text)
|
|
AND (
|
|
sqlc.narg(after_granted_at)::timestamptz IS NULL
|
|
OR (ledger.granted_at, ledger.id) < (
|
|
sqlc.narg(after_granted_at)::timestamptz,
|
|
sqlc.narg(after_id)::uuid
|
|
)
|
|
)
|
|
ORDER BY ledger.granted_at DESC, ledger.id DESC
|
|
LIMIT sqlc.arg(row_limit);
|