feat: persist candidate questionnaires and enforce company quotas

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 15:47:10 +03:00
1 parent 73c7caa225
commit 3a61a8e8bb
51 files changed
+6280 -38

No files matched your search

@@ -0,0 +1,228 @@
-- +goose Up
-- Candidate questionnaire answers and reviewed assessment catalog bindings.
-- Questionnaire values are private, self-reported data; they never contain
-- a confirmed grade or category.
-- The same small array checks are used by the contract tables below. Null
-- elements are refused because COUNT(DISTINCT ...) excludes them.
-- +goose StatementBegin
CREATE FUNCTION candidate_questionnaire_unique_text_array(input_values text[]) RETURNS boolean
LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $$
SELECT cardinality(input_values) = count(DISTINCT value)
FROM unnest(input_values) AS values_list(value)
$$;
-- +goose StatementEnd
-- +goose StatementBegin
CREATE FUNCTION candidate_questionnaire_unique_lower_text_array(input_values text[]) RETURNS boolean
LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $$
SELECT cardinality(input_values) = count(DISTINCT lower(value))
FROM unnest(input_values) AS values_list(value)
$$;
-- +goose StatementEnd
-- The aggregate row owns the optimistic-concurrency version and work context.
CREATE TABLE candidate_questionnaires (
user_id uuid NOT NULL,
version bigint NOT NULL,
work_formats text[] NOT NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT candidate_questionnaires_pkey PRIMARY KEY (user_id),
CONSTRAINT candidate_questionnaires_user_fkey FOREIGN KEY (user_id)
REFERENCES users (id) ON DELETE CASCADE,
CONSTRAINT candidate_questionnaires_version_positive CHECK (version > 0),
CONSTRAINT candidate_questionnaires_work_formats CHECK (
cardinality(work_formats) BETWEEN 1 AND 3
AND work_formats <@ ARRAY['remote', 'office', 'hybrid']::text[]
AND candidate_questionnaire_unique_text_array(work_formats)
),
CONSTRAINT candidate_questionnaires_times_ordered CHECK (updated_at >= created_at)
);
-- Stores the unique, ordered specialization answers separately from the
-- candidate's assessment result and confirmed category.
CREATE TABLE candidate_questionnaire_specializations (
user_id uuid NOT NULL,
ordinal smallint NOT NULL,
specialization text NOT NULL,
self_assessed_grade text NOT NULL,
task_languages text[] NOT NULL,
stack text[] NOT NULL,
commercial_months integer,
academic_months integer,
CONSTRAINT candidate_questionnaire_specializations_pkey PRIMARY KEY (user_id, ordinal),
CONSTRAINT candidate_questionnaire_specializations_user_fkey FOREIGN KEY (user_id)
REFERENCES candidate_questionnaires (user_id) ON DELETE CASCADE,
CONSTRAINT candidate_questionnaire_specializations_unique_code UNIQUE (user_id, specialization),
CONSTRAINT candidate_questionnaire_specializations_ordinal CHECK (ordinal BETWEEN 0 AND 1),
CONSTRAINT candidate_questionnaire_specializations_code CHECK (
specialization IN (
'backend', 'frontend', 'mobile', 'network_engineer', 'devops_sre',
'data_analyst', 'ds_ml', 'information_security', 'qa'
)
),
CONSTRAINT candidate_questionnaire_specializations_grade CHECK (
self_assessed_grade IN ('intern', 'junior', 'middle', 'senior')
),
CONSTRAINT candidate_questionnaire_specializations_languages CHECK (
cardinality(task_languages) <= 2
AND task_languages <@ ARRAY['python', 'go']::text[]
AND candidate_questionnaire_unique_text_array(task_languages)
),
CONSTRAINT candidate_questionnaire_specializations_stack CHECK (
cardinality(stack) <= 20
AND candidate_questionnaire_unique_lower_text_array(stack)
),
CONSTRAINT candidate_questionnaire_specializations_experience CHECK (
(commercial_months IS NULL AND academic_months IS NULL)
OR (
commercial_months IS NOT NULL
AND academic_months IS NOT NULL
AND commercial_months BETWEEN 0 AND 600
AND academic_months BETWEEN 0 AND 600
)
)
);
-- A catalog binding is an explicit operator-reviewed mapping for one immutable
-- opening. The fingerprint pins it to the exact published family set present
-- when the opening is made. Legacy openings have no row and stay unavailable
-- in the public catalog.
CREATE TABLE assessment_category_catalog_bindings (
opening_id uuid NOT NULL,
specialization text NOT NULL,
grade text NOT NULL,
task_languages text[] NOT NULL,
published_manifest_sha256 bytea NOT NULL,
reviewed_by text NOT NULL,
reviewed_at timestamptz NOT NULL,
CONSTRAINT assessment_category_catalog_bindings_pkey PRIMARY KEY (opening_id),
CONSTRAINT assessment_category_catalog_bindings_opening_fkey FOREIGN KEY (opening_id)
REFERENCES assessment_category_openings (id),
CONSTRAINT assessment_category_catalog_bindings_specialization CHECK (
specialization IN (
'backend', 'frontend', 'mobile', 'network_engineer', 'devops_sre',
'data_analyst', 'ds_ml', 'information_security', 'qa'
)
),
CONSTRAINT assessment_category_catalog_bindings_grade CHECK (
grade IN ('intern', 'junior', 'middle', 'senior')
),
CONSTRAINT assessment_category_catalog_bindings_languages CHECK (
cardinality(task_languages) <= 2
AND task_languages <@ ARRAY['python', 'go']::text[]
AND candidate_questionnaire_unique_text_array(task_languages)
),
CONSTRAINT assessment_category_catalog_bindings_manifest_hash CHECK (
octet_length(published_manifest_sha256) = 32
),
CONSTRAINT assessment_category_catalog_bindings_reviewer CHECK (
char_length(reviewed_by) <= 128
AND reviewed_by ~ '^operator:[a-z0-9][a-z0-9._-]*$'
)
);
-- Openings and published families are already immutable. Refuse a catalog
-- mapping unless it names the current opening and the full reviewed bank still
-- meets that opening's coverage and policy requirements.
-- +goose StatementBegin
CREATE FUNCTION assessment_category_catalog_bindings_validate() RETURNS trigger
LANGUAGE plpgsql AS $$
DECLARE
opening assessment_category_openings%ROWTYPE;
current_policy_id uuid;
published_count integer;
BEGIN
SELECT * INTO opening
FROM assessment_category_openings AS candidate
WHERE candidate.id = NEW.opening_id;
IF NOT FOUND THEN
RAISE EXCEPTION USING
ERRCODE = 'foreign_key_violation',
CONSTRAINT = 'assessment_category_catalog_bindings_opening',
TABLE = 'assessment_category_catalog_bindings',
MESSAGE = 'catalog binding references an unknown opening';
END IF;
IF opening.opened_by <> NEW.reviewed_by THEN
RAISE EXCEPTION USING
ERRCODE = 'check_violation',
CONSTRAINT = 'assessment_category_catalog_bindings_reviewer',
TABLE = 'assessment_category_catalog_bindings',
MESSAGE = 'catalog binding reviewer must match the category opener';
END IF;
IF EXISTS (
SELECT 1
FROM assessment_category_openings AS newer
WHERE newer.category = opening.category
AND (newer.opened_at, newer.id) > (opening.opened_at, opening.id)
) THEN
RAISE EXCEPTION USING
ERRCODE = 'check_violation',
CONSTRAINT = 'assessment_category_catalog_bindings_current_opening',
TABLE = 'assessment_category_catalog_bindings',
MESSAGE = 'catalog binding must reference the current opening';
END IF;
SELECT policy.id INTO current_policy_id
FROM policy_versions AS policy
WHERE policy.policy_key = 'product' AND policy.effective_at <= clock_timestamp()
ORDER BY policy.version DESC
LIMIT 1;
IF current_policy_id IS DISTINCT FROM opening.policy_version_id THEN
RAISE EXCEPTION USING
ERRCODE = 'check_violation',
CONSTRAINT = 'assessment_category_catalog_bindings_policy_current',
TABLE = 'assessment_category_catalog_bindings',
MESSAGE = 'catalog binding opening does not use the current policy';
END IF;
SELECT count(DISTINCT family.family_key)::integer INTO published_count
FROM assessment_families AS family
WHERE family.category = opening.category AND family.lifecycle = 'published';
IF published_count < opening.required_families THEN
RAISE EXCEPTION USING
ERRCODE = 'check_violation',
CONSTRAINT = 'assessment_category_catalog_bindings_coverage',
TABLE = 'assessment_category_catalog_bindings',
MESSAGE = 'catalog binding bank no longer meets its opening coverage';
END IF;
NEW.reviewed_at := clock_timestamp();
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER assessment_category_catalog_bindings_validate
BEFORE INSERT ON assessment_category_catalog_bindings
FOR EACH ROW EXECUTE FUNCTION assessment_category_catalog_bindings_validate();
-- A binding is an immutable review decision. It is replaced only by a new
-- opening and a new binding, never edited in place.
-- +goose StatementBegin
CREATE FUNCTION assessment_category_catalog_bindings_refuse_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'assessment_category_catalog_bindings_immutable',
TABLE = 'assessment_category_catalog_bindings',
MESSAGE = 'catalog bindings are immutable: create a new category opening';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER assessment_category_catalog_bindings_refuse_update_delete
BEFORE UPDATE OR DELETE ON assessment_category_catalog_bindings
FOR EACH ROW EXECUTE FUNCTION assessment_category_catalog_bindings_refuse_change();
CREATE TRIGGER assessment_category_catalog_bindings_refuse_truncate
BEFORE TRUNCATE ON assessment_category_catalog_bindings
FOR EACH STATEMENT EXECUTE FUNCTION assessment_category_catalog_bindings_refuse_change();
-- +goose Down
DROP TABLE assessment_category_catalog_bindings;
DROP FUNCTION assessment_category_catalog_bindings_refuse_change();
DROP FUNCTION assessment_category_catalog_bindings_validate();
DROP TABLE candidate_questionnaire_specializations;
DROP TABLE candidate_questionnaires;
DROP FUNCTION candidate_questionnaire_unique_lower_text_array(text[]);
DROP FUNCTION candidate_questionnaire_unique_text_array(text[]);