182 lines
6.4 KiB
SQL
182 lines
6.4 KiB
SQL
-- Creates the first saved questionnaire. A missing row is version zero;
|
|
-- concurrent first saves race on the primary key and exactly one returns a row.
|
|
-- name: InsertCandidateQuestionnaire :one
|
|
INSERT INTO candidate_questionnaires (
|
|
user_id,
|
|
version,
|
|
work_formats,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
sqlc.arg(user_id),
|
|
1,
|
|
sqlc.arg(work_formats),
|
|
sqlc.arg(at)::timestamptz,
|
|
sqlc.arg(at)::timestamptz
|
|
)
|
|
ON CONFLICT (user_id) DO NOTHING
|
|
RETURNING user_id, version, work_formats, created_at, updated_at;
|
|
|
|
-- Advances an existing questionnaire only at the version read by the caller.
|
|
-- The row lock serializes saves for one user; a stale writer returns no row.
|
|
-- name: AdvanceCandidateQuestionnaireVersion :one
|
|
UPDATE candidate_questionnaires
|
|
SET
|
|
version = candidate_questionnaires.version + 1,
|
|
work_formats = sqlc.arg(work_formats),
|
|
updated_at = GREATEST(candidate_questionnaires.updated_at, sqlc.arg(at)::timestamptz)
|
|
WHERE
|
|
candidate_questionnaires.user_id = sqlc.arg(user_id)
|
|
AND candidate_questionnaires.version = sqlc.arg(expected_version)
|
|
RETURNING user_id, version, work_formats, created_at, updated_at;
|
|
|
|
-- Reads the aggregate and its ordered specializations in one statement so a
|
|
-- response never pairs a version with another version's answers.
|
|
-- name: ReadCandidateQuestionnaire :one
|
|
SELECT
|
|
questionnaire.user_id,
|
|
questionnaire.version,
|
|
questionnaire.work_formats,
|
|
questionnaire.created_at,
|
|
questionnaire.updated_at,
|
|
COALESCE((
|
|
SELECT jsonb_agg(
|
|
jsonb_build_object(
|
|
'ordinal', specialization.ordinal,
|
|
'specialization', specialization.specialization,
|
|
'self_assessed_grade', specialization.self_assessed_grade,
|
|
'task_languages', specialization.task_languages,
|
|
'stack', specialization.stack,
|
|
'commercial_months', specialization.commercial_months,
|
|
'academic_months', specialization.academic_months
|
|
) ORDER BY specialization.ordinal
|
|
)
|
|
FROM candidate_questionnaire_specializations AS specialization
|
|
WHERE specialization.user_id = questionnaire.user_id
|
|
), '[]'::jsonb)::text AS specializations
|
|
FROM candidate_questionnaires AS questionnaire
|
|
WHERE questionnaire.user_id = sqlc.arg(user_id);
|
|
|
|
-- Replaces all specialization answers after the version row advanced in the
|
|
-- same transaction.
|
|
-- name: DeleteCandidateQuestionnaireSpecializations :exec
|
|
DELETE FROM candidate_questionnaire_specializations
|
|
WHERE user_id = sqlc.arg(user_id);
|
|
|
|
-- name: InsertCandidateQuestionnaireSpecialization :exec
|
|
INSERT INTO candidate_questionnaire_specializations (
|
|
user_id,
|
|
ordinal,
|
|
specialization,
|
|
self_assessed_grade,
|
|
task_languages,
|
|
stack,
|
|
commercial_months,
|
|
academic_months
|
|
) VALUES (
|
|
sqlc.arg(user_id),
|
|
sqlc.arg(ordinal),
|
|
sqlc.arg(specialization),
|
|
sqlc.arg(self_assessed_grade),
|
|
sqlc.arg(task_languages),
|
|
sqlc.arg(stack),
|
|
sqlc.narg(commercial_months),
|
|
sqlc.narg(academic_months)
|
|
);
|
|
|
|
-- Stores an explicit mapping against a newly-created immutable opening. The
|
|
-- insert trigger checks the opening, declared reviewer, current policy, and
|
|
-- live coverage; the adapter supplies the fingerprint of the published set.
|
|
-- name: InsertAssessmentCategoryCatalogBinding :exec
|
|
INSERT INTO assessment_category_catalog_bindings (
|
|
opening_id,
|
|
specialization,
|
|
grade,
|
|
task_languages,
|
|
published_manifest_sha256,
|
|
reviewed_by,
|
|
reviewed_at
|
|
) VALUES (
|
|
sqlc.arg(opening_id),
|
|
sqlc.arg(specialization),
|
|
sqlc.arg(grade),
|
|
sqlc.arg(task_languages),
|
|
sqlc.arg(published_manifest_sha256),
|
|
sqlc.arg(reviewed_by),
|
|
clock_timestamp()
|
|
);
|
|
|
|
-- Lists the exact published bank manifest while the category advisory lock is
|
|
-- held by OpenCategory. Published families have passed the independent-review
|
|
-- constraint before this read.
|
|
-- name: ReadPublishedAssessmentCategoryManifest :many
|
|
SELECT family_key, version, content_sha256
|
|
FROM assessment_families
|
|
WHERE category = sqlc.arg(category) AND lifecycle = 'published'
|
|
ORDER BY family_key, version;
|
|
|
|
-- Produces current catalog availability in one PostgreSQL statement snapshot.
|
|
-- The adapter compares the ordered live family manifest with the pinned
|
|
-- fingerprint; stale bindings are omitted fail-closed.
|
|
-- name: ListCurrentSpecializationAvailability :many
|
|
WITH current_policy AS (
|
|
SELECT id
|
|
FROM policy_versions
|
|
WHERE policy_key = 'product' AND effective_at <= statement_timestamp()
|
|
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,
|
|
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 opening.category;
|
|
|
|
-- A questionnaire save advances onboarding only from its own step.
|
|
-- name: AdvanceCandidateOnboardingAfterQuestionnaire :exec
|
|
UPDATE users
|
|
SET
|
|
onboarding_step = 'candidate_assessment',
|
|
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_questionnaire';
|