feat(profile): add the manual candidate profile contract and storage
This commit is contained in:
1 parent
d21fd49a42
commit
e52ffe7bcb
97 files changed
+20442
-63
No files matched your search
@@ -0,0 +1,237 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: candidate.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const advanceCandidateProfileVersion = `-- name: AdvanceCandidateProfileVersion :one
|
||||
UPDATE candidate_profiles
|
||||
SET
|
||||
version = candidate_profiles.version + 1,
|
||||
updated_at = GREATEST(candidate_profiles.updated_at, $1)
|
||||
WHERE
|
||||
candidate_profiles.user_id = $2
|
||||
AND candidate_profiles.version = $3
|
||||
RETURNING user_id, version, created_at, updated_at
|
||||
`
|
||||
|
||||
type AdvanceCandidateProfileVersionParams struct {
|
||||
At pgtype.Timestamptz
|
||||
UserID pgtype.UUID
|
||||
ExpectedVersion int64
|
||||
}
|
||||
|
||||
// Advances the version over the one the caller read, in the same statement.
|
||||
// The row lock it takes serializes concurrent saves of one profile. The update
|
||||
// time never moves back: an application clock behind the stored time (clock
|
||||
// skew between instances) keeps the stored time, which the CHECK
|
||||
// candidate_profiles_updated_after_creation also requires. No row
|
||||
// returns for a stale version or for an account without a profile, which the
|
||||
// caller answers as a version conflict.
|
||||
func (q *Queries) AdvanceCandidateProfileVersion(ctx context.Context, arg AdvanceCandidateProfileVersionParams) (CandidateProfile, error) {
|
||||
row := q.db.QueryRow(ctx, advanceCandidateProfileVersion, arg.At, arg.UserID, arg.ExpectedVersion)
|
||||
var i CandidateProfile
|
||||
err := row.Scan(
|
||||
&i.UserID,
|
||||
&i.Version,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteCandidateProfileClaims = `-- name: DeleteCandidateProfileClaims :exec
|
||||
DELETE FROM candidate_profile_claims
|
||||
WHERE user_id = $1
|
||||
`
|
||||
|
||||
// Removes the claims that a save replaces. The adapter calls it after the
|
||||
// version advanced and before it inserts the new set, in one transaction.
|
||||
func (q *Queries) DeleteCandidateProfileClaims(ctx context.Context, userID pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deleteCandidateProfileClaims, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertCandidateProfile = `-- name: InsertCandidateProfile :one
|
||||
|
||||
INSERT INTO candidate_profiles (
|
||||
user_id,
|
||||
version,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
$1,
|
||||
1,
|
||||
$2,
|
||||
$2
|
||||
)
|
||||
ON CONFLICT (user_id) DO NOTHING
|
||||
RETURNING user_id, version, created_at, updated_at
|
||||
`
|
||||
|
||||
type InsertCandidateProfileParams struct {
|
||||
UserID pgtype.UUID
|
||||
At pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Queries of the manual candidate profile (TASK-066). The profile is personal
|
||||
// data of the account that owns it: every statement is scoped by user_id, and
|
||||
// no statement selects another account's row. None of them names a file, the
|
||||
// ML service, or an FSP account, because the profile needs none.
|
||||
// Stores the first save of a profile at version 1. No row returns when the
|
||||
// account already has a profile, so a first save that lost a race with
|
||||
// another first save from another tab can be told apart and answered as a
|
||||
// version conflict.
|
||||
func (q *Queries) InsertCandidateProfile(ctx context.Context, arg InsertCandidateProfileParams) (CandidateProfile, error) {
|
||||
row := q.db.QueryRow(ctx, insertCandidateProfile, arg.UserID, arg.At)
|
||||
var i CandidateProfile
|
||||
err := row.Scan(
|
||||
&i.UserID,
|
||||
&i.Version,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertCandidateProfileClaim = `-- name: InsertCandidateProfileClaim :exec
|
||||
INSERT INTO candidate_profile_claims (
|
||||
user_id,
|
||||
kind,
|
||||
ordinal,
|
||||
value,
|
||||
status,
|
||||
origin,
|
||||
trust_level,
|
||||
quote,
|
||||
extraction_version,
|
||||
confirmed_at
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
0,
|
||||
$7,
|
||||
$8,
|
||||
$9
|
||||
)
|
||||
`
|
||||
|
||||
type InsertCandidateProfileClaimParams struct {
|
||||
UserID pgtype.UUID
|
||||
Kind string
|
||||
Ordinal int16
|
||||
Value []byte
|
||||
Status string
|
||||
Origin string
|
||||
Quote pgtype.Text
|
||||
ExtractionVersion pgtype.Text
|
||||
ConfirmedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Stores one claim of the new set. Provenance fields are the domain's; the
|
||||
// statement never takes a trust level from the caller, because every claim
|
||||
// has level 0.
|
||||
func (q *Queries) InsertCandidateProfileClaim(ctx context.Context, arg InsertCandidateProfileClaimParams) error {
|
||||
_, err := q.db.Exec(ctx, insertCandidateProfileClaim,
|
||||
arg.UserID,
|
||||
arg.Kind,
|
||||
arg.Ordinal,
|
||||
arg.Value,
|
||||
arg.Status,
|
||||
arg.Origin,
|
||||
arg.Quote,
|
||||
arg.ExtractionVersion,
|
||||
arg.ConfirmedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const readCandidateProfile = `-- name: ReadCandidateProfile :many
|
||||
SELECT
|
||||
candidate_profiles.user_id,
|
||||
candidate_profiles.version,
|
||||
candidate_profiles.created_at,
|
||||
candidate_profiles.updated_at,
|
||||
candidate_profile_claims.kind,
|
||||
candidate_profile_claims.ordinal,
|
||||
candidate_profile_claims.value,
|
||||
candidate_profile_claims.status,
|
||||
candidate_profile_claims.origin,
|
||||
candidate_profile_claims.trust_level,
|
||||
candidate_profile_claims.quote,
|
||||
candidate_profile_claims.extraction_version,
|
||||
candidate_profile_claims.confirmed_at
|
||||
FROM candidate_profiles
|
||||
LEFT JOIN candidate_profile_claims
|
||||
ON candidate_profile_claims.user_id = candidate_profiles.user_id
|
||||
WHERE candidate_profiles.user_id = $1
|
||||
ORDER BY candidate_profile_claims.kind, candidate_profile_claims.ordinal
|
||||
`
|
||||
|
||||
type ReadCandidateProfileRow struct {
|
||||
UserID pgtype.UUID
|
||||
Version int64
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
Kind pgtype.Text
|
||||
Ordinal pgtype.Int2
|
||||
Value []byte
|
||||
Status pgtype.Text
|
||||
Origin pgtype.Text
|
||||
TrustLevel pgtype.Int2
|
||||
Quote pgtype.Text
|
||||
ExtractionVersion pgtype.Text
|
||||
ConfirmedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Reads the profile row of an account together with every claim in one
|
||||
// statement, so the version and the claims come from one snapshot: a save that
|
||||
// commits between two separate reads could pair version N with the claims of
|
||||
// N+1. A profile without a claim returns one row with NULL claim columns, and
|
||||
// no row at all means the account has not saved a profile (contract version
|
||||
// 0). Claims come in a stable order: single fields by kind, list items by
|
||||
// position. Never replace this with two statements.
|
||||
func (q *Queries) ReadCandidateProfile(ctx context.Context, userID pgtype.UUID) ([]ReadCandidateProfileRow, error) {
|
||||
rows, err := q.db.Query(ctx, readCandidateProfile, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ReadCandidateProfileRow
|
||||
for rows.Next() {
|
||||
var i ReadCandidateProfileRow
|
||||
if err := rows.Scan(
|
||||
&i.UserID,
|
||||
&i.Version,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Kind,
|
||||
&i.Ordinal,
|
||||
&i.Value,
|
||||
&i.Status,
|
||||
&i.Origin,
|
||||
&i.TrustLevel,
|
||||
&i.Quote,
|
||||
&i.ExtractionVersion,
|
||||
&i.ConfirmedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -42,6 +42,26 @@ type AssessmentFamily struct {
|
||||
RetiredBy pgtype.Text
|
||||
}
|
||||
|
||||
type CandidateProfile struct {
|
||||
UserID pgtype.UUID
|
||||
Version int64
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type CandidateProfileClaim struct {
|
||||
UserID pgtype.UUID
|
||||
Kind string
|
||||
Ordinal int16
|
||||
Value []byte
|
||||
Status string
|
||||
Origin string
|
||||
TrustLevel int16
|
||||
Quote pgtype.Text
|
||||
ExtractionVersion pgtype.Text
|
||||
ConfirmedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type ConsentRecord struct {
|
||||
ID pgtype.UUID
|
||||
SubjectID pgtype.UUID
|
||||
|
||||
@@ -14,6 +14,14 @@ type Querier interface {
|
||||
// Adds recipients to the day and the month row. The caller holds their
|
||||
// locks from LockMailQuotaRows and has checked both limits.
|
||||
AddMailQuotaUsage(ctx context.Context, arg AddMailQuotaUsageParams) (int64, error)
|
||||
// Advances the version over the one the caller read, in the same statement.
|
||||
// The row lock it takes serializes concurrent saves of one profile. The update
|
||||
// time never moves back: an application clock behind the stored time (clock
|
||||
// skew between instances) keeps the stored time, which the CHECK
|
||||
// candidate_profiles_updated_after_creation also requires. No row
|
||||
// returns for a stale version or for an account without a profile, which the
|
||||
// caller answers as a version conflict.
|
||||
AdvanceCandidateProfileVersion(ctx context.Context, arg AdvanceCandidateProfileVersionParams) (CandidateProfile, error)
|
||||
// Cancels a job that has not finished. A job whose handler transaction is
|
||||
// in progress holds its row lock, so the statement waits for it: after the
|
||||
// handler committed the job is done and nothing is canceled; otherwise the
|
||||
@@ -77,6 +85,9 @@ type Querier interface {
|
||||
// Counts the families of a category that have at least one published
|
||||
// version, however many versions each has.
|
||||
CountPublishedAssessmentFamilies(ctx context.Context, category string) (int32, error)
|
||||
// Removes the claims that a save replaces. The adapter calls it after the
|
||||
// version advanced and before it inserts the new set, in one transaction.
|
||||
DeleteCandidateProfileClaims(ctx context.Context, userID pgtype.UUID) error
|
||||
DeleteMailRequest(ctx context.Context, id pgtype.UUID) (int64, error)
|
||||
DeleteOrganizationRepresentatives(ctx context.Context, organizationID pgtype.UUID) error
|
||||
// Deletes one batch of accounts that stayed unverified since before cutoff,
|
||||
@@ -201,6 +212,19 @@ type Querier interface {
|
||||
// returns no row instead of an error, so a conflict does not abort the
|
||||
// surrounding transaction.
|
||||
InsertAssessmentFamily(ctx context.Context, arg InsertAssessmentFamilyParams) (AssessmentFamily, error)
|
||||
// Queries of the manual candidate profile (TASK-066). The profile is personal
|
||||
// data of the account that owns it: every statement is scoped by user_id, and
|
||||
// no statement selects another account's row. None of them names a file, the
|
||||
// ML service, or an FSP account, because the profile needs none.
|
||||
// Stores the first save of a profile at version 1. No row returns when the
|
||||
// account already has a profile, so a first save that lost a race with
|
||||
// another first save from another tab can be told apart and answered as a
|
||||
// version conflict.
|
||||
InsertCandidateProfile(ctx context.Context, arg InsertCandidateProfileParams) (CandidateProfile, error)
|
||||
// Stores one claim of the new set. Provenance fields are the domain's; the
|
||||
// statement never takes a trust level from the caller, because every claim
|
||||
// has level 0.
|
||||
InsertCandidateProfileClaim(ctx context.Context, arg InsertCandidateProfileClaimParams) error
|
||||
// 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.
|
||||
@@ -342,6 +366,14 @@ type Querier interface {
|
||||
// the publication time set by the guard trigger. No row means the version
|
||||
// was not a draft any more.
|
||||
PublishAssessmentFamily(ctx context.Context, arg PublishAssessmentFamilyParams) (AssessmentFamily, error)
|
||||
// Reads the profile row of an account together with every claim in one
|
||||
// statement, so the version and the claims come from one snapshot: a save that
|
||||
// commits between two separate reads could pair version N with the claims of
|
||||
// N+1. A profile without a claim returns one row with NULL claim columns, and
|
||||
// no row at all means the account has not saved a profile (contract version
|
||||
// 0). Claims come in a stable order: single fields by kind, list items by
|
||||
// position. Never replace this with two statements.
|
||||
ReadCandidateProfile(ctx context.Context, userID pgtype.UUID) ([]ReadCandidateProfileRow, error)
|
||||
// Advances a delivery past one attempt and appends the attempt in one
|
||||
// statement, so both happen or neither does, inside or outside a
|
||||
// transaction. The update applies only while the stored version still
|
||||
|
||||
Reference in new issue
Block a user