feat: persist candidate questionnaires and enforce company quotas
This commit is contained in:
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[]);
|
||||
@@ -0,0 +1,106 @@
|
||||
-- +goose Up
|
||||
-- Invitation usage remains keyed by its stable invitation ID for the
|
||||
-- organization's lifetime, so a retry cannot count it again after the
|
||||
-- rolling window. The policy and trust columns record the basis of the
|
||||
-- original reservation.
|
||||
CREATE TABLE company_invitation_quota_usage (
|
||||
organization_id uuid NOT NULL,
|
||||
invitation_id uuid NOT NULL,
|
||||
reserved_at timestamptz NOT NULL,
|
||||
policy_version_id uuid NOT NULL,
|
||||
verification_level text NOT NULL,
|
||||
verification_basis text NOT NULL,
|
||||
verification_changed_at timestamptz NOT NULL,
|
||||
quota_tier text NOT NULL,
|
||||
CONSTRAINT company_invitation_quota_usage_pkey PRIMARY KEY (
|
||||
organization_id, invitation_id
|
||||
),
|
||||
CONSTRAINT company_invitation_quota_usage_organization_id_fkey FOREIGN KEY (
|
||||
organization_id
|
||||
) REFERENCES organizations (id) ON DELETE CASCADE,
|
||||
CONSTRAINT company_invitation_quota_usage_policy_version_id_fkey FOREIGN KEY (
|
||||
policy_version_id
|
||||
) REFERENCES policy_versions (id) ON DELETE RESTRICT,
|
||||
CONSTRAINT company_invitation_quota_usage_verification_evidence CHECK (
|
||||
(
|
||||
verification_level = 'e0'
|
||||
AND verification_basis = 'confirmed_email'
|
||||
) OR (
|
||||
verification_level = 'e1'
|
||||
AND verification_basis = 'corporate_domain'
|
||||
) OR (
|
||||
verification_level = 'e2'
|
||||
AND verification_basis = 'requisites_and_authority'
|
||||
)
|
||||
),
|
||||
CONSTRAINT company_invitation_quota_usage_tier_known CHECK (
|
||||
quota_tier IN ('e0', 'e1', 'e2')
|
||||
),
|
||||
CONSTRAINT company_invitation_quota_usage_tier_matches_verification CHECK (
|
||||
quota_tier = verification_level
|
||||
),
|
||||
CONSTRAINT company_invitation_quota_usage_verification_not_future CHECK (
|
||||
verification_changed_at <= reserved_at
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX company_invitation_quota_usage_window_idx
|
||||
ON company_invitation_quota_usage (organization_id, reserved_at, invitation_id);
|
||||
|
||||
-- Card-view rows are a rolling-window counter, not a full access history.
|
||||
-- Repeats update last_seen_at; expired rows may be deleted in bounded batches
|
||||
-- only after a successful reservation. This is logical window expiry, not a
|
||||
-- promise of deletion at an exact wall-clock time.
|
||||
CREATE TABLE company_card_view_quota_usage (
|
||||
organization_id uuid NOT NULL,
|
||||
candidate_id uuid NOT NULL,
|
||||
last_seen_at timestamptz NOT NULL,
|
||||
policy_version_id uuid NOT NULL,
|
||||
verification_level text NOT NULL,
|
||||
verification_basis text NOT NULL,
|
||||
verification_changed_at timestamptz NOT NULL,
|
||||
quota_tier text NOT NULL,
|
||||
CONSTRAINT company_card_view_quota_usage_pkey PRIMARY KEY (
|
||||
organization_id, candidate_id
|
||||
),
|
||||
CONSTRAINT company_card_view_quota_usage_organization_id_fkey FOREIGN KEY (
|
||||
organization_id
|
||||
) REFERENCES organizations (id) ON DELETE CASCADE,
|
||||
CONSTRAINT company_card_view_quota_usage_candidate_id_fkey FOREIGN KEY (
|
||||
candidate_id
|
||||
) REFERENCES users (id) ON DELETE CASCADE,
|
||||
CONSTRAINT company_card_view_quota_usage_policy_version_id_fkey FOREIGN KEY (
|
||||
policy_version_id
|
||||
) REFERENCES policy_versions (id) ON DELETE RESTRICT,
|
||||
CONSTRAINT company_card_view_quota_usage_verification_evidence CHECK (
|
||||
(
|
||||
verification_level = 'e0'
|
||||
AND verification_basis = 'confirmed_email'
|
||||
) OR (
|
||||
verification_level = 'e1'
|
||||
AND verification_basis = 'corporate_domain'
|
||||
) OR (
|
||||
verification_level = 'e2'
|
||||
AND verification_basis = 'requisites_and_authority'
|
||||
)
|
||||
),
|
||||
CONSTRAINT company_card_view_quota_usage_tier_known CHECK (
|
||||
quota_tier IN ('e0', 'e1', 'e2')
|
||||
),
|
||||
CONSTRAINT company_card_view_quota_usage_tier_matches_verification CHECK (
|
||||
quota_tier = verification_level
|
||||
),
|
||||
CONSTRAINT company_card_view_quota_usage_verification_not_future CHECK (
|
||||
verification_changed_at <= last_seen_at
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX company_card_view_quota_usage_window_idx
|
||||
ON company_card_view_quota_usage (organization_id, last_seen_at, candidate_id);
|
||||
|
||||
CREATE INDEX company_card_view_quota_usage_candidate_idx
|
||||
ON company_card_view_quota_usage (candidate_id);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE company_card_view_quota_usage;
|
||||
DROP TABLE company_invitation_quota_usage;
|
||||
@@ -0,0 +1,130 @@
|
||||
-- Writers lock the organization first, then invitation/card-view state, and
|
||||
-- finally usage rows. The organization lock serializes quota decisions for
|
||||
-- the tenant and makes the subsequent count and insert atomic.
|
||||
|
||||
-- name: LockCompanyQuotaOrganization :one
|
||||
SELECT
|
||||
id,
|
||||
verification_level,
|
||||
verification_basis,
|
||||
verification_changed_at
|
||||
FROM organizations
|
||||
WHERE id = sqlc.arg(organization_id)
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: FindCompanyInvitationQuotaUsage :one
|
||||
SELECT invitation_id
|
||||
FROM company_invitation_quota_usage
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND invitation_id = sqlc.arg(invitation_id)
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: CountCompanyInvitationQuotaWindow :one
|
||||
SELECT
|
||||
count(*)::bigint AS usage_count,
|
||||
min(reserved_at) AS earliest_at
|
||||
FROM company_invitation_quota_usage
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND reserved_at > sqlc.arg(window_start);
|
||||
|
||||
-- name: InsertCompanyInvitationQuotaUsage :exec
|
||||
INSERT INTO company_invitation_quota_usage (
|
||||
organization_id,
|
||||
invitation_id,
|
||||
reserved_at,
|
||||
policy_version_id,
|
||||
verification_level,
|
||||
verification_basis,
|
||||
verification_changed_at,
|
||||
quota_tier
|
||||
) VALUES (
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(invitation_id),
|
||||
sqlc.arg(reserved_at),
|
||||
sqlc.arg(policy_version_id),
|
||||
sqlc.arg(verification_level),
|
||||
sqlc.arg(verification_basis),
|
||||
sqlc.arg(verification_changed_at),
|
||||
sqlc.arg(quota_tier)
|
||||
);
|
||||
|
||||
-- name: FindCompanyCardViewQuotaUsage :one
|
||||
SELECT last_seen_at
|
||||
FROM company_card_view_quota_usage
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND candidate_id = sqlc.arg(candidate_id)
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: RefreshCompanyCardViewQuotaUsage :execrows
|
||||
UPDATE company_card_view_quota_usage
|
||||
SET
|
||||
last_seen_at = greatest(last_seen_at, sqlc.arg(last_seen_at)),
|
||||
policy_version_id = sqlc.arg(policy_version_id),
|
||||
verification_level = sqlc.arg(verification_level),
|
||||
verification_basis = sqlc.arg(verification_basis),
|
||||
verification_changed_at = sqlc.arg(verification_changed_at),
|
||||
quota_tier = sqlc.arg(quota_tier)
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND candidate_id = sqlc.arg(candidate_id)
|
||||
AND last_seen_at > sqlc.arg(window_start);
|
||||
|
||||
-- name: CountCompanyCardViewQuotaWindow :one
|
||||
SELECT
|
||||
count(*)::bigint AS usage_count,
|
||||
min(last_seen_at) AS earliest_at
|
||||
FROM company_card_view_quota_usage
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND last_seen_at > sqlc.arg(window_start);
|
||||
|
||||
-- name: SaveCompanyCardViewQuotaUsage :exec
|
||||
INSERT INTO company_card_view_quota_usage (
|
||||
organization_id,
|
||||
candidate_id,
|
||||
last_seen_at,
|
||||
policy_version_id,
|
||||
verification_level,
|
||||
verification_basis,
|
||||
verification_changed_at,
|
||||
quota_tier
|
||||
) VALUES (
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(candidate_id),
|
||||
sqlc.arg(last_seen_at),
|
||||
sqlc.arg(policy_version_id),
|
||||
sqlc.arg(verification_level),
|
||||
sqlc.arg(verification_basis),
|
||||
sqlc.arg(verification_changed_at),
|
||||
sqlc.arg(quota_tier)
|
||||
)
|
||||
ON CONFLICT (organization_id, candidate_id)
|
||||
DO UPDATE SET
|
||||
last_seen_at = greatest(
|
||||
company_card_view_quota_usage.last_seen_at,
|
||||
EXCLUDED.last_seen_at
|
||||
),
|
||||
policy_version_id = EXCLUDED.policy_version_id,
|
||||
verification_level = EXCLUDED.verification_level,
|
||||
verification_basis = EXCLUDED.verification_basis,
|
||||
verification_changed_at = EXCLUDED.verification_changed_at,
|
||||
quota_tier = EXCLUDED.quota_tier;
|
||||
|
||||
-- name: DeleteExpiredCompanyCardViewQuotaUsage :execrows
|
||||
WITH expired AS (
|
||||
SELECT quota_usage.organization_id, quota_usage.candidate_id
|
||||
FROM company_card_view_quota_usage AS quota_usage
|
||||
WHERE
|
||||
quota_usage.organization_id = sqlc.arg(organization_id)
|
||||
AND quota_usage.last_seen_at <= sqlc.arg(window_start)
|
||||
ORDER BY quota_usage.last_seen_at, quota_usage.candidate_id
|
||||
LIMIT sqlc.arg(row_limit)::integer
|
||||
)
|
||||
DELETE FROM company_card_view_quota_usage AS usage
|
||||
USING expired
|
||||
WHERE
|
||||
usage.organization_id = expired.organization_id
|
||||
AND usage.candidate_id = expired.candidate_id;
|
||||
@@ -0,0 +1,181 @@
|
||||
-- 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';
|
||||
Reference in new issue
Block a user