// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.31.1 // source: consents.sql package sqlc import ( "context" "github.com/jackc/pgx/v5/pgtype" ) const getConsentText = `-- name: GetConsentText :one SELECT purpose, version, body, body_sha256, kind, published_at FROM consent_texts WHERE purpose = $1 AND version = $2 ` type GetConsentTextParams struct { Purpose string Version string } func (q *Queries) GetConsentText(ctx context.Context, arg GetConsentTextParams) (ConsentText, error) { row := q.db.QueryRow(ctx, getConsentText, arg.Purpose, arg.Version) var i ConsentText err := row.Scan( &i.Purpose, &i.Version, &i.Body, &i.BodySha256, &i.Kind, &i.PublishedAt, ) return i, err } const getContactConsentRecordByInvitation = `-- name: GetContactConsentRecordByInvitation :one SELECT id, subject_id, purpose, text_version, text_hash, source, 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 = $1 AND invitation_id = $2 ` type GetContactConsentRecordByInvitationParams struct { SubjectID pgtype.UUID InvitationID pgtype.UUID } type GetContactConsentRecordByInvitationRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } func (q *Queries) GetContactConsentRecordByInvitation(ctx context.Context, arg GetContactConsentRecordByInvitationParams) (GetContactConsentRecordByInvitationRow, error) { row := q.db.QueryRow(ctx, getContactConsentRecordByInvitation, arg.SubjectID, arg.InvitationID) var i GetContactConsentRecordByInvitationRow err := row.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ) return i, err } const getCurrentConsentRecord = `-- name: GetCurrentConsentRecord :one SELECT id, subject_id, purpose, text_version, text_hash, source, 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 = $1 AND purpose = $2 AND purpose <> 'c4' AND revoked_at IS NULL ORDER BY granted_at DESC, id DESC LIMIT 1 ` type GetCurrentConsentRecordParams struct { SubjectID pgtype.UUID Purpose string } type GetCurrentConsentRecordRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } // The newest unrevoked record of a purpose other than C4. func (q *Queries) GetCurrentConsentRecord(ctx context.Context, arg GetCurrentConsentRecordParams) (GetCurrentConsentRecordRow, error) { row := q.db.QueryRow(ctx, getCurrentConsentRecord, arg.SubjectID, arg.Purpose) var i GetCurrentConsentRecordRow err := row.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ) return i, err } const getCurrentConsentText = `-- name: GetCurrentConsentText :one SELECT purpose, version, body, body_sha256, kind, published_at FROM consent_texts WHERE purpose = $1 AND published_at <= $2 ORDER BY published_at DESC LIMIT 1 ` type GetCurrentConsentTextParams struct { Purpose string At pgtype.Timestamptz } // The text of a purpose that applies at a time: the latest publication not // after it. func (q *Queries) GetCurrentConsentText(ctx context.Context, arg GetCurrentConsentTextParams) (ConsentText, error) { row := q.db.QueryRow(ctx, getCurrentConsentText, arg.Purpose, arg.At) var i ConsentText err := row.Scan( &i.Purpose, &i.Version, &i.Body, &i.BodySha256, &i.Kind, &i.PublishedAt, ) return i, err } const getUnrevokedConsentRecordByVersion = `-- name: GetUnrevokedConsentRecordByVersion :one SELECT record.id, record.subject_id, record.purpose, record.text_version, record.text_hash, record.source, 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 = $1 AND record.purpose = $2 AND record.text_version = $3 AND record.revoked_at IS NULL ` type GetUnrevokedConsentRecordByVersionParams struct { SubjectID pgtype.UUID Purpose string TextVersion string } type GetUnrevokedConsentRecordByVersionRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } func (q *Queries) GetUnrevokedConsentRecordByVersion(ctx context.Context, arg GetUnrevokedConsentRecordByVersionParams) (GetUnrevokedConsentRecordByVersionRow, error) { row := q.db.QueryRow(ctx, getUnrevokedConsentRecordByVersion, arg.SubjectID, arg.Purpose, arg.TextVersion) var i GetUnrevokedConsentRecordByVersionRow err := row.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ) return i, err } const insertConsentRecord = `-- name: InsertConsentRecord :one INSERT INTO consent_records ( id, subject_id, purpose, text_version, text_hash, source, granted_at ) VALUES ( $1, $2, $3, $4, $5, $6, $7 ) 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, 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 ` type InsertConsentRecordParams struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string GrantedAt pgtype.Timestamptz } type InsertConsentRecordRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } // 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. func (q *Queries) InsertConsentRecord(ctx context.Context, arg InsertConsentRecordParams) (InsertConsentRecordRow, error) { row := q.db.QueryRow(ctx, insertConsentRecord, arg.ID, arg.SubjectID, arg.Purpose, arg.TextVersion, arg.TextHash, arg.Source, arg.GrantedAt, ) var i InsertConsentRecordRow err := row.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ) return i, err } const insertContactConsentRecord = `-- name: InsertContactConsentRecord :one INSERT INTO consent_records ( id, subject_id, purpose, text_version, text_hash, source, organization_id, organization_name, field_set, invitation_id, invitation_terms_version, granted_at ) VALUES ( $1, $2, 'c4', $3, $4, 'invitation_acceptance', $5, $6, $7::text [], $8, $9, $10 ) ON CONFLICT (invitation_id) WHERE invitation_id IS NOT NULL DO NOTHING RETURNING id, subject_id, purpose, text_version, text_hash, source, organization_id, organization_name, field_set, invitation_id, invitation_terms_version, granted_at, revoked_at, revocation_source, 'active'::text AS state ` type InsertContactConsentRecordParams struct { ID pgtype.UUID SubjectID pgtype.UUID TextVersion string TextHash []byte OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz } type InsertContactConsentRecordRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } // 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. func (q *Queries) InsertContactConsentRecord(ctx context.Context, arg InsertContactConsentRecordParams) (InsertContactConsentRecordRow, error) { row := q.db.QueryRow(ctx, insertContactConsentRecord, arg.ID, arg.SubjectID, arg.TextVersion, arg.TextHash, arg.OrganizationID, arg.OrganizationName, arg.FieldSet, arg.InvitationID, arg.InvitationTermsVersion, arg.GrantedAt, ) var i InsertContactConsentRecordRow err := row.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ) return i, err } const listConsentRecords = `-- name: ListConsentRecords :many WITH ledger AS ( SELECT record.id, record.subject_id, record.purpose, record.text_version, record.text_hash, record.source, 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 = $5 ) SELECT ledger.id, ledger.subject_id, ledger.purpose, ledger.text_version, ledger.text_hash, ledger.source, 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 ($1::text IS NULL OR ledger.state = $1::text) AND ( $2::timestamptz IS NULL OR (ledger.granted_at, ledger.id) < ( $2::timestamptz, $3::uuid ) ) ORDER BY ledger.granted_at DESC, ledger.id DESC LIMIT $4 ` type ListConsentRecordsParams struct { State pgtype.Text AfterGrantedAt pgtype.Timestamptz AfterID pgtype.UUID RowLimit int32 SubjectID pgtype.UUID } type ListConsentRecordsRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } // 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. func (q *Queries) ListConsentRecords(ctx context.Context, arg ListConsentRecordsParams) ([]ListConsentRecordsRow, error) { rows, err := q.db.Query(ctx, listConsentRecords, arg.State, arg.AfterGrantedAt, arg.AfterID, arg.RowLimit, arg.SubjectID, ) if err != nil { return nil, err } defer rows.Close() var items []ListConsentRecordsRow for rows.Next() { var i ListConsentRecordsRow if err := rows.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil } const listCurrentContactConsentRecords = `-- name: ListCurrentContactConsentRecords :many SELECT id, subject_id, purpose, text_version, text_hash, source, 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 = $1 AND purpose = 'c4' AND organization_id = $2 AND revoked_at IS NULL ORDER BY granted_at DESC, id DESC ` type ListCurrentContactConsentRecordsParams struct { SubjectID pgtype.UUID OrganizationID pgtype.UUID } type ListCurrentContactConsentRecordsRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } func (q *Queries) ListCurrentContactConsentRecords(ctx context.Context, arg ListCurrentContactConsentRecordsParams) ([]ListCurrentContactConsentRecordsRow, error) { rows, err := q.db.Query(ctx, listCurrentContactConsentRecords, arg.SubjectID, arg.OrganizationID) if err != nil { return nil, err } defer rows.Close() var items []ListCurrentContactConsentRecordsRow for rows.Next() { var i ListCurrentContactConsentRecordsRow if err := rows.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil } const revokeConsentRecords = `-- name: RevokeConsentRecords :many UPDATE consent_records SET revoked_at = greatest($1::timestamptz, granted_at), revocation_source = $2 WHERE subject_id = $3 AND purpose = $4 AND purpose <> 'c4' AND revoked_at IS NULL RETURNING id, subject_id, purpose, text_version, text_hash, source, organization_id, organization_name, field_set, invitation_id, invitation_terms_version, granted_at, revoked_at, revocation_source, 'revoked'::text AS state ` type RevokeConsentRecordsParams struct { RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text SubjectID pgtype.UUID Purpose string } type RevokeConsentRecordsRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } // 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. func (q *Queries) RevokeConsentRecords(ctx context.Context, arg RevokeConsentRecordsParams) ([]RevokeConsentRecordsRow, error) { rows, err := q.db.Query(ctx, revokeConsentRecords, arg.RevokedAt, arg.RevocationSource, arg.SubjectID, arg.Purpose, ) if err != nil { return nil, err } defer rows.Close() var items []RevokeConsentRecordsRow for rows.Next() { var i RevokeConsentRecordsRow if err := rows.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil } const revokeContactConsentRecords = `-- name: RevokeContactConsentRecords :many UPDATE consent_records SET revoked_at = greatest($1::timestamptz, granted_at), revocation_source = $2 WHERE subject_id = $3 AND purpose = 'c4' AND organization_id = $4 AND revoked_at IS NULL RETURNING id, subject_id, purpose, text_version, text_hash, source, organization_id, organization_name, field_set, invitation_id, invitation_terms_version, granted_at, revoked_at, revocation_source, 'revoked'::text AS state ` type RevokeContactConsentRecordsParams struct { RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text SubjectID pgtype.UUID OrganizationID pgtype.UUID } type RevokeContactConsentRecordsRow struct { ID pgtype.UUID SubjectID pgtype.UUID Purpose string TextVersion string TextHash []byte Source string OrganizationID pgtype.UUID OrganizationName pgtype.Text FieldSet []string InvitationID pgtype.UUID InvitationTermsVersion pgtype.Int8 GrantedAt pgtype.Timestamptz RevokedAt pgtype.Timestamptz RevocationSource pgtype.Text State string } // Revokes every unrevoked C4 record of the subject for one organization and // returns them, with the same clamp of the revocation time as above. func (q *Queries) RevokeContactConsentRecords(ctx context.Context, arg RevokeContactConsentRecordsParams) ([]RevokeContactConsentRecordsRow, error) { rows, err := q.db.Query(ctx, revokeContactConsentRecords, arg.RevokedAt, arg.RevocationSource, arg.SubjectID, arg.OrganizationID, ) if err != nil { return nil, err } defer rows.Close() var items []RevokeContactConsentRecordsRow for rows.Next() { var i RevokeContactConsentRecordsRow if err := rows.Scan( &i.ID, &i.SubjectID, &i.Purpose, &i.TextVersion, &i.TextHash, &i.Source, &i.OrganizationID, &i.OrganizationName, &i.FieldSet, &i.InvitationID, &i.InvitationTermsVersion, &i.GrantedAt, &i.RevokedAt, &i.RevocationSource, &i.State, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil }