Files
lincodex/backend/db/queries/evidence.sql
T

106 lines
4.2 KiB
SQL

-- Skill evidence reads. The projection tables hold no answer, score, item,
-- failure, or anti-cheat data, and none of these statements names the result,
-- attempt, or review tables: evidence is read from its own facts only.
-- The candidate's own facts, hidden ones included, newest event first. Every
-- statement is scoped by the owner; the caller passes the continuation
-- position of the previous page, if any, and fetches one extra row.
-- name: ListCandidateEvidenceFacts :many
SELECT
fact.id,
fact.kind,
fact.source_type,
fact.event_id,
fact.result_id,
fact.attribution,
fact.trust,
fact.grade,
fact.basis,
fact.calibration_status,
fact.occurred_at,
fact.visibility,
COALESCE(
(
SELECT array_agg(skill.skill_code ORDER BY skill.skill_code)
FROM skill_evidence_skills AS skill
WHERE skill.fact_id = fact.id
),
'{}'::text []
)::text [] AS skills
FROM skill_evidence_facts AS fact
WHERE
fact.candidate_id = sqlc.arg(candidate_id)
AND (
sqlc.narg(after_occurred_at)::timestamptz IS NULL
OR (fact.occurred_at, fact.id) < (
sqlc.narg(after_occurred_at)::timestamptz,
sqlc.narg(after_id)::uuid
)
)
ORDER BY fact.occurred_at DESC, fact.id DESC
LIMIT sqlc.arg(row_limit);
-- The evidence that one organization may read about one candidate: one fact
-- for each category of the card, the fact that supports the confirmed grade
-- of that category (the earliest one that passed exactly that grade, so a
-- lower grade or a retake adds nothing). The statement selects from the
-- effective publication view (published and backed by an unrevoked C2) and
-- excludes the candidates that hide the reading organization, so no row means
-- nothing to read, whatever the reason. The card must publish the categories
-- field and only visible measurements qualify. The categories come from the
-- caller as "specialization/grade" pairs taken from the card that was just
-- projected, so the answer never names a skill the card does not show. The columns are
-- an allowlist: no identifier of the fact, the event, the result, or the
-- candidate leaves, the instant is cut to the year, and the basis and the
-- calibration (the same for every candidate) say that the fact is a synthetic
-- demonstration.
-- name: ListOrganizationVisibleEvidence :many
SELECT DISTINCT ON (skill.skill_code)
skill.skill_code AS skill,
fact.grade,
fact.trust,
fact.basis,
fact.calibration_status,
extract(YEAR FROM fact.occurred_at AT TIME ZONE 'UTC')::integer AS observed_year
FROM candidate_effective_publications AS effective
INNER JOIN skill_evidence_facts AS fact
ON effective.user_id = fact.candidate_id
INNER JOIN skill_evidence_skills AS skill
ON fact.id = skill.fact_id
WHERE
effective.user_id = sqlc.arg(candidate_id)
AND 'categories' = any(effective.fields)
AND (skill.skill_code || '/' || fact.grade) = any(sqlc.arg(categories)::text [])
AND fact.kind = 'measurement'
AND fact.visibility = 'visible'
AND NOT EXISTS (
SELECT 1
FROM candidate_hidden_organizations AS hidden
WHERE
hidden.user_id = effective.user_id
AND hidden.organization_id = sqlc.arg(organization_id)
)
ORDER BY skill.skill_code, fact.occurred_at, fact.id;
-- The same selection for the candidate's own card preview, so the candidate
-- sees what an organization would read. It is scoped by the owner and runs
-- after the caller locked the effective C2 and publication; it does not apply
-- an organization or a cohort.
-- name: ListCandidatePreviewEvidence :many
SELECT DISTINCT ON (skill.skill_code)
skill.skill_code AS skill,
fact.grade,
fact.trust,
fact.basis,
fact.calibration_status,
extract(YEAR FROM fact.occurred_at AT TIME ZONE 'UTC')::integer AS observed_year
FROM skill_evidence_facts AS fact
INNER JOIN skill_evidence_skills AS skill
ON fact.id = skill.fact_id
WHERE
fact.candidate_id = sqlc.arg(candidate_id)
AND (skill.skill_code || '/' || fact.grade) = any(sqlc.arg(categories)::text [])
AND fact.kind = 'measurement'
AND fact.visibility = 'visible'
ORDER BY skill.skill_code, fact.occurred_at, fact.id;