Files
lincodex/backend/db/queries/assessment_availability.sql

174 lines
7.3 KiB
SQL

-- Returns current reviewed grade/language bindings and the live published
-- family manifest at one caller-supplied database time. The adapter verifies
-- the pinned manifest hash before exposing only family keys.
-- name: ListCurrentAssessmentAvailabilitySelections :many
WITH current_policy AS (
SELECT id, version
FROM policy_versions
WHERE policy_key = 'product' AND effective_at <= sqlc.arg(as_of)::timestamptz
ORDER BY version DESC
LIMIT 1
), current_openings AS (
SELECT DISTINCT ON (category)
id,
category,
policy_version_id,
required_families,
opened_at
FROM assessment_category_openings
ORDER BY category, opened_at DESC, id DESC
)
SELECT
opening.id AS opening_id,
opening.category,
opening.policy_version_id,
opening.required_families,
COALESCE((
SELECT count(DISTINCT family.family_key)::integer
FROM assessment_families AS family
WHERE family.category = opening.category AND family.lifecycle = 'published'
), 0)::integer AS published_count,
policy.id AS effective_policy_id,
policy.version AS effective_policy_version,
binding.specialization,
binding.grade,
binding.task_languages,
binding.published_manifest_sha256,
COALESCE((
SELECT jsonb_agg(
jsonb_build_object(
'family_key', family.family_key,
'version', family.version,
'content_sha256', encode(family.content_sha256, 'hex')
) ORDER BY family.family_key, family.version
)
FROM assessment_families AS family
WHERE family.category = opening.category AND family.lifecycle = 'published'
), '[]'::jsonb)::text AS published_manifest
FROM current_openings AS opening
JOIN assessment_category_catalog_bindings AS binding
ON binding.opening_id = opening.id
LEFT JOIN current_policy AS policy ON true
ORDER BY binding.specialization, binding.grade, binding.task_languages, opening.category;
-- Reads an active real attempt without taking the write lock used by create.
-- The availability GET never expires or otherwise mutates this row.
-- name: GetAssessmentAvailabilityActiveAttempt :one
SELECT id, expires_at
FROM assessment_attempts
WHERE user_id = sqlc.arg(user_id)
AND specialization = sqlc.arg(specialization)
AND kind IN ('root', 'corrective')
AND state IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize')
ORDER BY created_at DESC, id DESC
LIMIT 1;
-- Reads roots within the policy horizon and the last header in each correction
-- chain. It deliberately omits frozen items, answers, and row locks.
-- name: ListAssessmentAvailabilityRootSummaries :many
SELECT
root.target_grade AS root_target_grade,
root.created_at AS root_created_at,
root.updated_at AS root_updated_at,
root.expires_at AS root_expires_at,
root.state AS root_state,
root.outcome AS root_outcome,
root.chain_deadline AS root_chain_deadline,
latest.target_grade AS latest_target_grade,
latest.updated_at AS latest_updated_at,
latest.expires_at AS latest_expires_at,
latest.state AS latest_state,
latest.outcome AS latest_outcome,
latest.chain_deadline AS latest_chain_deadline,
COALESCE(latest_result.result_snapshot->>'not_determined_reason', '')::text AS latest_platform_error
FROM assessment_attempts AS root
JOIN LATERAL (
SELECT attempt.*
FROM assessment_attempts AS attempt
WHERE attempt.user_id = root.user_id
AND attempt.root_attempt_id = root.id
AND attempt.kind IN ('root', 'corrective')
ORDER BY attempt.created_at DESC, attempt.id DESC
LIMIT 1
) AS latest ON true
LEFT JOIN assessment_attempt_results AS root_result
ON root_result.attempt_id = root.id AND root_result.user_id = root.user_id
LEFT JOIN assessment_attempt_results AS latest_result
ON latest_result.attempt_id = latest.id AND latest_result.user_id = latest.user_id
WHERE root.user_id = sqlc.arg(user_id)
AND root.specialization = sqlc.arg(specialization)
AND root.kind = 'root'
AND root.created_at >= sqlc.arg(since)::timestamptz
AND root.state <> 'insufficient_bank'
AND COALESCE(root_result.result_snapshot->>'not_determined_reason', '')
NOT IN ('insufficient_bank', 'sandbox_unavailable')
ORDER BY root.created_at DESC, root.id DESC;
-- Counts eligible root attempts in a policy window and computes the instant
-- at which one additional root slot opens. The microsecond adjustment matches
-- the creation predicate created_at >= since at PostgreSQL timestamp precision.
-- name: GetAssessmentRootQuotaAvailability :one
WITH eligible AS (
SELECT attempt.id, attempt.created_at
FROM assessment_attempts AS attempt
LEFT JOIN assessment_attempt_results AS result
ON result.attempt_id = attempt.id AND result.user_id = attempt.user_id
WHERE attempt.user_id = sqlc.arg(user_id)
AND attempt.kind = 'root'
AND attempt.state <> 'insufficient_bank'
AND attempt.created_at >= sqlc.arg(since)::timestamptz
AND attempt.created_at <= sqlc.arg(as_of)::timestamptz
AND COALESCE(result.result_snapshot->>'not_determined_reason', '')
NOT IN ('insufficient_bank', 'sandbox_unavailable')
), total AS (
SELECT count(*)::bigint AS attempt_count FROM eligible
)
SELECT
total.attempt_count,
CASE WHEN total.attempt_count >= sqlc.arg(max_attempts)::bigint THEN (
SELECT eligible.created_at
+ make_interval(secs => sqlc.arg(window_seconds)::double precision)
+ interval '1 microsecond'
FROM eligible
ORDER BY eligible.created_at, eligible.id
LIMIT 1
OFFSET (total.attempt_count - sqlc.arg(max_attempts)::bigint)
) ELSE NULL::timestamptz END AS available_at
FROM total;
-- Counts family instances disclosed after the non-repeat boundary. The first
-- item is disclosed when the attempt is created; each later item is disclosed
-- when the preceding answer is accepted. This per-item timestamp prevents a
-- long-running attempt from making later items appear older than they are.
-- Counting the first unanswered item after expiry is conservative when the
-- final response was lost, while frozen future items remain excluded.
-- name: ListAssessmentDisclosedFamilyKeys :many
WITH progress AS (
SELECT
attempt.id AS attempt_id,
attempt.created_at,
COALESCE(max(item.ordinal) FILTER (WHERE item.answered_at IS NOT NULL), 0) + 1 AS last_disclosed_ordinal
FROM assessment_attempts AS attempt
JOIN assessment_attempt_items AS item ON item.attempt_id = attempt.id
WHERE attempt.user_id = sqlc.arg(user_id)
AND attempt.kind IN ('root', 'corrective')
AND attempt.expires_at > sqlc.arg(since)::timestamptz
GROUP BY attempt.id
), disclosed AS (
SELECT
item.family_key,
CASE
WHEN item.ordinal = 1 THEN progress.created_at
ELSE previous.answered_at
END AS disclosed_at
FROM progress
JOIN assessment_attempt_items AS item ON item.attempt_id = progress.attempt_id
LEFT JOIN assessment_attempt_items AS previous
ON previous.attempt_id = item.attempt_id AND previous.ordinal = item.ordinal - 1
WHERE item.ordinal <= progress.last_disclosed_ordinal
)
SELECT DISTINCT disclosed.family_key
FROM disclosed
WHERE disclosed.disclosed_at > sqlc.arg(since)::timestamptz
ORDER BY disclosed.family_key;