178 lines
6.4 KiB
SQL
178 lines
6.4 KiB
SQL
-- Queries of the manual candidate profile. 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.
|
|
-- name: InsertCandidateProfile :one
|
|
INSERT INTO candidate_profiles (
|
|
user_id,
|
|
version,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
sqlc.arg(user_id),
|
|
1,
|
|
sqlc.arg(at),
|
|
sqlc.arg(at)
|
|
)
|
|
ON CONFLICT (user_id) DO NOTHING
|
|
RETURNING user_id, version, created_at, updated_at;
|
|
|
|
-- 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.
|
|
-- name: AdvanceCandidateProfileVersion :one
|
|
UPDATE candidate_profiles
|
|
SET
|
|
version = candidate_profiles.version + 1,
|
|
updated_at = GREATEST(candidate_profiles.updated_at, sqlc.arg(at))
|
|
WHERE
|
|
candidate_profiles.user_id = sqlc.arg(user_id)
|
|
AND candidate_profiles.version = sqlc.arg(expected_version)
|
|
RETURNING user_id, version, created_at, updated_at;
|
|
|
|
-- 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.
|
|
-- 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,
|
|
COALESCE(
|
|
(
|
|
SELECT jsonb_agg(
|
|
jsonb_build_object(
|
|
'first_index', confirmations.first_index,
|
|
'second_index', confirmations.second_index,
|
|
'first_period', jsonb_build_object(
|
|
'started_on', confirmations.first_started_on,
|
|
'ended_on', confirmations.first_ended_on
|
|
),
|
|
'second_period', jsonb_build_object(
|
|
'started_on', confirmations.second_started_on,
|
|
'ended_on', confirmations.second_ended_on
|
|
)
|
|
) ORDER BY confirmations.first_index, confirmations.second_index
|
|
)::text
|
|
FROM candidate_profile_overlap_confirmations AS confirmations
|
|
WHERE confirmations.user_id = candidate_profiles.user_id
|
|
),
|
|
'[]'::text
|
|
)::text AS parallel_period_acknowledgements
|
|
FROM candidate_profiles
|
|
LEFT JOIN candidate_profile_claims
|
|
ON candidate_profile_claims.user_id = candidate_profiles.user_id
|
|
WHERE candidate_profiles.user_id = sqlc.arg(user_id)
|
|
ORDER BY candidate_profile_claims.kind, candidate_profile_claims.ordinal;
|
|
|
|
-- 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.
|
|
-- name: DeleteCandidateProfileClaims :exec
|
|
DELETE FROM candidate_profile_claims
|
|
WHERE user_id = sqlc.arg(user_id);
|
|
|
|
-- 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.
|
|
-- name: InsertCandidateProfileClaim :exec
|
|
INSERT INTO candidate_profile_claims (
|
|
user_id,
|
|
kind,
|
|
ordinal,
|
|
value,
|
|
status,
|
|
origin,
|
|
trust_level,
|
|
quote,
|
|
extraction_version,
|
|
confirmed_at
|
|
) VALUES (
|
|
sqlc.arg(user_id),
|
|
sqlc.arg(kind),
|
|
sqlc.arg(ordinal),
|
|
sqlc.arg(value),
|
|
sqlc.arg(status),
|
|
sqlc.arg(origin),
|
|
0,
|
|
sqlc.narg(quote),
|
|
sqlc.narg(extraction_version),
|
|
sqlc.narg(confirmed_at)
|
|
);
|
|
|
|
-- Replaces the current explicit decisions with those in the new profile.
|
|
-- The versioned profile row is advanced first in this same transaction.
|
|
-- name: DeleteCandidateProfileOverlapConfirmations :exec
|
|
DELETE FROM candidate_profile_overlap_confirmations
|
|
WHERE user_id = sqlc.arg(user_id);
|
|
|
|
-- name: InsertCandidateProfileOverlapConfirmation :exec
|
|
INSERT INTO candidate_profile_overlap_confirmations (
|
|
user_id,
|
|
first_index,
|
|
second_index,
|
|
first_started_on,
|
|
first_ended_on,
|
|
second_started_on,
|
|
second_ended_on
|
|
) VALUES (
|
|
sqlc.arg(user_id),
|
|
sqlc.arg(first_index),
|
|
sqlc.arg(second_index),
|
|
sqlc.arg(first_started_on),
|
|
sqlc.narg(first_ended_on),
|
|
sqlc.arg(second_started_on),
|
|
sqlc.narg(second_ended_on)
|
|
);
|
|
|
|
-- Every changed version appends exactly one private snapshot. The unique
|
|
-- key prevents a second snapshot for the same profile version.
|
|
-- name: AppendCandidateProfileHistory :exec
|
|
INSERT INTO candidate_profile_history (
|
|
user_id,
|
|
version,
|
|
changed_at,
|
|
snapshot
|
|
) VALUES (
|
|
sqlc.arg(user_id),
|
|
sqlc.arg(version),
|
|
sqlc.arg(changed_at),
|
|
sqlc.arg(snapshot)::jsonb
|
|
);
|
|
|
|
-- Saving a profile advances onboarding only from that exact step. A retry or
|
|
-- a save after a later step is already stored never regresses onboarding.
|
|
-- name: AdvanceCandidateOnboardingAfterProfile :exec
|
|
UPDATE users
|
|
SET
|
|
onboarding_step = 'candidate_questionnaire',
|
|
version = users.version + 1,
|
|
updated_at = GREATEST(users.updated_at, sqlc.arg(at)::timestamptz)
|
|
WHERE
|
|
users.id = sqlc.arg(user_id)
|
|
AND users.scenario = 'candidate'
|
|
AND users.email_verified_at IS NOT NULL
|
|
AND users.onboarding_step = 'candidate_profile';
|