Files
lincodex/backend/internal/adapter/secondary/postgres/sqlc/privacy.sql.go
T

716 lines
22 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: privacy.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const countCandidateHiddenOrganizations = `-- name: CountCandidateHiddenOrganizations :one
SELECT count(*) AS total
FROM candidate_hidden_organizations
WHERE user_id = $1
`
// Counts the hiding records of one candidate for the settings read.
func (q *Queries) CountCandidateHiddenOrganizations(ctx context.Context, userID pgtype.UUID) (int64, error) {
row := q.db.QueryRow(ctx, countCandidateHiddenOrganizations, userID)
var total int64
err := row.Scan(&total)
return total, err
}
const deleteCandidateHiddenOrganization = `-- name: DeleteCandidateHiddenOrganization :one
DELETE FROM candidate_hidden_organizations
WHERE user_id = $1 AND organization_id = $2
RETURNING organization_id
`
type DeleteCandidateHiddenOrganizationParams struct {
UserID pgtype.UUID
OrganizationID pgtype.UUID
}
// 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.
func (q *Queries) DeleteCandidateHiddenOrganization(ctx context.Context, arg DeleteCandidateHiddenOrganizationParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, deleteCandidateHiddenOrganization, arg.UserID, arg.OrganizationID)
var organization_id pgtype.UUID
err := row.Scan(&organization_id)
return organization_id, err
}
const getCandidateHiddenOrganization = `-- 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 = $1
AND candidate_hidden_organizations.organization_id = $2
`
type GetCandidateHiddenOrganizationParams struct {
UserID pgtype.UUID
OrganizationID pgtype.UUID
}
type GetCandidateHiddenOrganizationRow struct {
OrganizationID pgtype.UUID
OrganizationName string
HiddenAt pgtype.Timestamptz
}
// Reads one hiding of the candidate with the current name of the
// organization.
func (q *Queries) GetCandidateHiddenOrganization(ctx context.Context, arg GetCandidateHiddenOrganizationParams) (GetCandidateHiddenOrganizationRow, error) {
row := q.db.QueryRow(ctx, getCandidateHiddenOrganization, arg.UserID, arg.OrganizationID)
var i GetCandidateHiddenOrganizationRow
err := row.Scan(&i.OrganizationID, &i.OrganizationName, &i.HiddenAt)
return i, err
}
const insertCandidateHiddenOrganization = `-- name: InsertCandidateHiddenOrganization :one
INSERT INTO candidate_hidden_organizations (
user_id,
organization_id,
hidden_at
) VALUES (
$1,
$2,
$3
)
ON CONFLICT (user_id, organization_id) DO NOTHING
RETURNING user_id, organization_id, hidden_at
`
type InsertCandidateHiddenOrganizationParams struct {
UserID pgtype.UUID
OrganizationID pgtype.UUID
At pgtype.Timestamptz
}
// 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.
func (q *Queries) InsertCandidateHiddenOrganization(ctx context.Context, arg InsertCandidateHiddenOrganizationParams) (CandidateHiddenOrganization, error) {
row := q.db.QueryRow(ctx, insertCandidateHiddenOrganization, arg.UserID, arg.OrganizationID, arg.At)
var i CandidateHiddenOrganization
err := row.Scan(&i.UserID, &i.OrganizationID, &i.HiddenAt)
return i, err
}
const insertCandidatePrivacyEvent = `-- name: InsertCandidatePrivacyEvent :exec
INSERT INTO candidate_privacy_events (
id,
user_id,
kind,
occurred_at,
fields,
consent_record_id,
organization_id
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7
)
`
type InsertCandidatePrivacyEventParams struct {
ID pgtype.UUID
UserID pgtype.UUID
Kind string
OccurredAt pgtype.Timestamptz
Fields []string
ConsentRecordID pgtype.UUID
OrganizationID pgtype.UUID
}
// 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.
func (q *Queries) InsertCandidatePrivacyEvent(ctx context.Context, arg InsertCandidatePrivacyEventParams) error {
_, err := q.db.Exec(ctx, insertCandidatePrivacyEvent,
arg.ID,
arg.UserID,
arg.Kind,
arg.OccurredAt,
arg.Fields,
arg.ConsentRecordID,
arg.OrganizationID,
)
return err
}
const insertCandidatePublication = `-- name: InsertCandidatePublication :one
INSERT INTO candidate_publications (
user_id,
state,
fields,
consent_record_id,
published_at,
closed_at,
version,
created_at,
updated_at
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
1,
$7,
$7
)
ON CONFLICT (user_id) DO NOTHING
RETURNING user_id, version
`
type InsertCandidatePublicationParams struct {
UserID pgtype.UUID
State string
Fields []string
ConsentRecordID pgtype.UUID
PublishedAt pgtype.Timestamptz
ClosedAt pgtype.Timestamptz
At pgtype.Timestamptz
}
type InsertCandidatePublicationRow struct {
UserID pgtype.UUID
Version int64
}
// 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.
func (q *Queries) InsertCandidatePublication(ctx context.Context, arg InsertCandidatePublicationParams) (InsertCandidatePublicationRow, error) {
row := q.db.QueryRow(ctx, insertCandidatePublication,
arg.UserID,
arg.State,
arg.Fields,
arg.ConsentRecordID,
arg.PublishedAt,
arg.ClosedAt,
arg.At,
)
var i InsertCandidatePublicationRow
err := row.Scan(&i.UserID, &i.Version)
return i, err
}
const insertCandidateSearchStatus = `-- name: InsertCandidateSearchStatus :one
INSERT INTO candidate_search_statuses (
user_id,
status,
confirmed_at,
lapses_at,
policy_version_id,
version,
created_at,
updated_at
) VALUES (
$1,
$2,
$3,
$4,
$5,
1,
$6,
$6
)
ON CONFLICT (user_id) DO NOTHING
RETURNING user_id, version
`
type InsertCandidateSearchStatusParams struct {
UserID pgtype.UUID
Status string
ConfirmedAt pgtype.Timestamptz
LapsesAt pgtype.Timestamptz
PolicyVersionID pgtype.UUID
At pgtype.Timestamptz
}
type InsertCandidateSearchStatusRow struct {
UserID pgtype.UUID
Version int64
}
// Stores the first status at version 1; no row returns when one exists.
func (q *Queries) InsertCandidateSearchStatus(ctx context.Context, arg InsertCandidateSearchStatusParams) (InsertCandidateSearchStatusRow, error) {
row := q.db.QueryRow(ctx, insertCandidateSearchStatus,
arg.UserID,
arg.Status,
arg.ConfirmedAt,
arg.LapsesAt,
arg.PolicyVersionID,
arg.At,
)
var i InsertCandidateSearchStatusRow
err := row.Scan(&i.UserID, &i.Version)
return i, err
}
const listCandidateHiddenOrganizations = `-- 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 = $1
AND (
$2::timestamptz IS NULL
OR (
candidate_hidden_organizations.hidden_at,
candidate_hidden_organizations.organization_id
) < (
$2::timestamptz,
$3::uuid
)
)
ORDER BY
candidate_hidden_organizations.hidden_at DESC,
candidate_hidden_organizations.organization_id DESC
LIMIT $4
`
type ListCandidateHiddenOrganizationsParams struct {
UserID pgtype.UUID
AfterHiddenAt pgtype.Timestamptz
AfterOrganizationID pgtype.UUID
RowLimit int32
}
type ListCandidateHiddenOrganizationsRow struct {
OrganizationID pgtype.UUID
OrganizationName string
HiddenAt pgtype.Timestamptz
}
// 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.
func (q *Queries) ListCandidateHiddenOrganizations(ctx context.Context, arg ListCandidateHiddenOrganizationsParams) ([]ListCandidateHiddenOrganizationsRow, error) {
rows, err := q.db.Query(ctx, listCandidateHiddenOrganizations,
arg.UserID,
arg.AfterHiddenAt,
arg.AfterOrganizationID,
arg.RowLimit,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListCandidateHiddenOrganizationsRow
for rows.Next() {
var i ListCandidateHiddenOrganizationsRow
if err := rows.Scan(&i.OrganizationID, &i.OrganizationName, &i.HiddenAt); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listCandidatePrivacyEvents = `-- 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 = $1
AND (
$2::timestamptz IS NULL
OR (candidate_privacy_events.occurred_at, candidate_privacy_events.id) < (
$2::timestamptz,
$3::uuid
)
)
ORDER BY
candidate_privacy_events.occurred_at DESC,
candidate_privacy_events.id DESC
LIMIT $4
`
type ListCandidatePrivacyEventsParams struct {
UserID pgtype.UUID
AfterOccurredAt pgtype.Timestamptz
AfterID pgtype.UUID
RowLimit int32
}
type ListCandidatePrivacyEventsRow struct {
ID pgtype.UUID
Kind string
OccurredAt pgtype.Timestamptz
Fields []string
ConsentRecordID pgtype.UUID
OrganizationID pgtype.UUID
OrganizationName pgtype.Text
}
// 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.
func (q *Queries) ListCandidatePrivacyEvents(ctx context.Context, arg ListCandidatePrivacyEventsParams) ([]ListCandidatePrivacyEventsRow, error) {
rows, err := q.db.Query(ctx, listCandidatePrivacyEvents,
arg.UserID,
arg.AfterOccurredAt,
arg.AfterID,
arg.RowLimit,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListCandidatePrivacyEventsRow
for rows.Next() {
var i ListCandidatePrivacyEventsRow
if err := rows.Scan(
&i.ID,
&i.Kind,
&i.OccurredAt,
&i.Fields,
&i.ConsentRecordID,
&i.OrganizationID,
&i.OrganizationName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockCandidatePublicationForConsentRevocation = `-- 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 = $1
AND candidate_publications.state = 'published'
AND consent_records.id = ANY($2::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
`
type LockCandidatePublicationForConsentRevocationParams struct {
UserID pgtype.UUID
ConsentRecordIds []pgtype.UUID
}
// 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.
func (q *Queries) LockCandidatePublicationForConsentRevocation(ctx context.Context, arg LockCandidatePublicationForConsentRevocationParams) (pgtype.Timestamptz, error) {
row := q.db.QueryRow(ctx, lockCandidatePublicationForConsentRevocation, arg.UserID, arg.ConsentRecordIds)
var closed_at pgtype.Timestamptz
err := row.Scan(&closed_at)
return closed_at, err
}
const lockCurrentC2Record = `-- name: LockCurrentC2Record :one
SELECT id
FROM consent_records
WHERE
subject_id = $1
AND purpose = 'c2'
AND revoked_at IS NULL
ORDER BY granted_at DESC, id DESC
LIMIT 1
FOR SHARE
`
// 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.
func (q *Queries) LockCurrentC2Record(ctx context.Context, subjectID pgtype.UUID) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, lockCurrentC2Record, subjectID)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const readCandidatePublication = `-- 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 = $1
`
type ReadCandidatePublicationRow struct {
UserID pgtype.UUID
State string
Fields []string
ConsentRecordID pgtype.UUID
PublishedAt pgtype.Timestamptz
ClosedAt pgtype.Timestamptz
Version int64
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
ConsentRevokedAt pgtype.Timestamptz
}
// 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.
func (q *Queries) ReadCandidatePublication(ctx context.Context, userID pgtype.UUID) (ReadCandidatePublicationRow, error) {
row := q.db.QueryRow(ctx, readCandidatePublication, userID)
var i ReadCandidatePublicationRow
err := row.Scan(
&i.UserID,
&i.State,
&i.Fields,
&i.ConsentRecordID,
&i.PublishedAt,
&i.ClosedAt,
&i.Version,
&i.CreatedAt,
&i.UpdatedAt,
&i.ConsentRevokedAt,
)
return i, err
}
const readCandidateSearchStatus = `-- 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 = $1
`
// 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.
func (q *Queries) ReadCandidateSearchStatus(ctx context.Context, userID pgtype.UUID) (CandidateSearchStatus, error) {
row := q.db.QueryRow(ctx, readCandidateSearchStatus, userID)
var i CandidateSearchStatus
err := row.Scan(
&i.UserID,
&i.Status,
&i.ConfirmedAt,
&i.LapsesAt,
&i.PolicyVersionID,
&i.Version,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const readPublicationVisibleToOrganization = `-- name: ReadPublicationVisibleToOrganization :one
SELECT
effective.user_id,
effective.fields,
effective.published_at
FROM candidate_effective_publications AS effective
WHERE
effective.user_id = $1
AND NOT exists(
SELECT 1 FROM candidate_hidden_organizations AS hidden
WHERE
hidden.user_id = effective.user_id
AND hidden.organization_id = $2
)
`
type ReadPublicationVisibleToOrganizationParams struct {
UserID pgtype.UUID
OrganizationID pgtype.UUID
}
type ReadPublicationVisibleToOrganizationRow struct {
UserID pgtype.UUID
Fields []string
PublishedAt pgtype.Timestamptz
}
// 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.
func (q *Queries) ReadPublicationVisibleToOrganization(ctx context.Context, arg ReadPublicationVisibleToOrganizationParams) (ReadPublicationVisibleToOrganizationRow, error) {
row := q.db.QueryRow(ctx, readPublicationVisibleToOrganization, arg.UserID, arg.OrganizationID)
var i ReadPublicationVisibleToOrganizationRow
err := row.Scan(&i.UserID, &i.Fields, &i.PublishedAt)
return i, err
}
const updateCandidatePublication = `-- name: UpdateCandidatePublication :one
UPDATE candidate_publications
SET
state = $1,
fields = $2,
consent_record_id = $3,
published_at = $4,
closed_at = $5,
version = candidate_publications.version + 1,
updated_at = greatest(candidate_publications.updated_at, $6)
WHERE
candidate_publications.user_id = $7
AND candidate_publications.version = $8
RETURNING user_id, version
`
type UpdateCandidatePublicationParams struct {
State string
Fields []string
ConsentRecordID pgtype.UUID
PublishedAt pgtype.Timestamptz
ClosedAt pgtype.Timestamptz
At pgtype.Timestamptz
UserID pgtype.UUID
ExpectedVersion int64
}
type UpdateCandidatePublicationRow struct {
UserID pgtype.UUID
Version int64
}
// 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.
func (q *Queries) UpdateCandidatePublication(ctx context.Context, arg UpdateCandidatePublicationParams) (UpdateCandidatePublicationRow, error) {
row := q.db.QueryRow(ctx, updateCandidatePublication,
arg.State,
arg.Fields,
arg.ConsentRecordID,
arg.PublishedAt,
arg.ClosedAt,
arg.At,
arg.UserID,
arg.ExpectedVersion,
)
var i UpdateCandidatePublicationRow
err := row.Scan(&i.UserID, &i.Version)
return i, err
}
const updateCandidateSearchStatus = `-- name: UpdateCandidateSearchStatus :one
UPDATE candidate_search_statuses
SET
status = $1,
confirmed_at = $2,
lapses_at = $3,
policy_version_id = $4,
version = candidate_search_statuses.version + 1,
updated_at = greatest(candidate_search_statuses.updated_at, $5)
WHERE
candidate_search_statuses.user_id = $6
AND candidate_search_statuses.version = $7
RETURNING user_id, version
`
type UpdateCandidateSearchStatusParams struct {
Status string
ConfirmedAt pgtype.Timestamptz
LapsesAt pgtype.Timestamptz
PolicyVersionID pgtype.UUID
At pgtype.Timestamptz
UserID pgtype.UUID
ExpectedVersion int64
}
type UpdateCandidateSearchStatusRow struct {
UserID pgtype.UUID
Version int64
}
// 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.
func (q *Queries) UpdateCandidateSearchStatus(ctx context.Context, arg UpdateCandidateSearchStatusParams) (UpdateCandidateSearchStatusRow, error) {
row := q.db.QueryRow(ctx, updateCandidateSearchStatus,
arg.Status,
arg.ConfirmedAt,
arg.LapsesAt,
arg.PolicyVersionID,
arg.At,
arg.UserID,
arg.ExpectedVersion,
)
var i UpdateCandidateSearchStatusRow
err := row.Scan(&i.UserID, &i.Version)
return i, err
}