386 lines
16 KiB
PL/PgSQL
386 lines
16 KiB
PL/PgSQL
-- +goose Up
|
|
-- Account-owned support snapshots store only typed object references. The
|
|
-- referenced records stay in their own bounded authorization scopes.
|
|
CREATE SEQUENCE support_case_number_seq
|
|
AS bigint
|
|
MINVALUE 1
|
|
MAXVALUE 9999999999
|
|
NO CYCLE;
|
|
|
|
CREATE TABLE support_cases (
|
|
id uuid NOT NULL,
|
|
case_number text NOT NULL,
|
|
owner_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
|
case_type text NOT NULL,
|
|
status text NOT NULL,
|
|
description text NOT NULL,
|
|
object_type text,
|
|
object_id uuid,
|
|
assessment_specialization text,
|
|
assessment_grade text,
|
|
complaint_reason text,
|
|
rubric_version integer,
|
|
rubric_opening_id uuid,
|
|
rubric_policy_version_id uuid,
|
|
target_business_days integer NOT NULL,
|
|
target_due_at timestamptz NOT NULL,
|
|
target_answered_at timestamptz,
|
|
target_policy_version integer NOT NULL,
|
|
target_calendar_version integer NOT NULL,
|
|
decision_outcome text,
|
|
decision_reason text,
|
|
decision_at timestamptz,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
CONSTRAINT support_cases_pkey PRIMARY KEY (id),
|
|
CONSTRAINT support_cases_number_key UNIQUE (case_number),
|
|
CONSTRAINT support_cases_number_format CHECK (
|
|
case_number ~ '^SC-[0-9]{4}-[0-9]{6,10}$'
|
|
AND substring(case_number FROM 4 FOR 4)::integer =
|
|
EXTRACT(YEAR FROM created_at AT TIME ZONE 'UTC')::integer
|
|
),
|
|
CONSTRAINT support_cases_case_type CHECK (
|
|
case_type IN ('support', 'complaint', 'grade_appeal', 'anticheat_appeal', 'manual_assessment')
|
|
),
|
|
CONSTRAINT support_cases_status CHECK (
|
|
status IN ('submitted', 'in_review', 'waiting_for_user', 'waiting_expert', 'resolved', 'closed')
|
|
),
|
|
CONSTRAINT support_cases_description CHECK (
|
|
char_length(description) BETWEEN 1 AND 4000
|
|
),
|
|
CONSTRAINT support_cases_object_pair CHECK (
|
|
(object_type IS NULL) = (object_id IS NULL)
|
|
),
|
|
CONSTRAINT support_cases_object_type CHECK (
|
|
object_type IS NULL OR object_type IN ('assessment_attempt', 'organization', 'invitation', 'support_case')
|
|
),
|
|
CONSTRAINT support_cases_assessment_target_pair CHECK (
|
|
(assessment_specialization IS NULL) = (assessment_grade IS NULL)
|
|
),
|
|
CONSTRAINT support_cases_assessment_specialization CHECK (
|
|
assessment_specialization IS NULL OR assessment_specialization IN (
|
|
'backend', 'frontend', 'mobile', 'network_engineer', 'devops_sre',
|
|
'data_analyst', 'ds_ml', 'information_security', 'qa'
|
|
)
|
|
),
|
|
CONSTRAINT support_cases_assessment_grade CHECK (
|
|
assessment_grade IS NULL OR assessment_grade IN ('intern', 'junior', 'middle', 'senior')
|
|
),
|
|
CONSTRAINT support_cases_complaint_reason CHECK (
|
|
complaint_reason IS NULL OR complaint_reason IN (
|
|
'organization_conduct', 'offer_content', 'salary_terms_violation', 'data_disclosure', 'other'
|
|
)
|
|
),
|
|
CONSTRAINT support_cases_rubric_version CHECK (rubric_version IS NULL OR rubric_version > 0),
|
|
CONSTRAINT support_cases_rubric_opening_fkey FOREIGN KEY (rubric_opening_id)
|
|
REFERENCES assessment_category_openings (id),
|
|
CONSTRAINT support_cases_rubric_policy_version_fkey FOREIGN KEY (rubric_policy_version_id)
|
|
REFERENCES policy_versions (id),
|
|
CONSTRAINT support_cases_rubric_pin_shape CHECK (
|
|
(rubric_version IS NULL AND rubric_opening_id IS NULL AND rubric_policy_version_id IS NULL)
|
|
OR (
|
|
case_type = 'manual_assessment'
|
|
AND rubric_version IS NOT NULL
|
|
AND rubric_opening_id IS NOT NULL
|
|
AND rubric_policy_version_id IS NOT NULL
|
|
)
|
|
),
|
|
CONSTRAINT support_cases_target_values CHECK (
|
|
target_business_days BETWEEN 1 AND 60
|
|
AND target_policy_version > 0
|
|
AND target_calendar_version > 0
|
|
AND target_due_at >= created_at
|
|
AND (target_answered_at IS NULL OR target_answered_at >= created_at)
|
|
),
|
|
CONSTRAINT support_cases_decision_shape CHECK (
|
|
(decision_outcome IS NULL AND decision_reason IS NULL AND decision_at IS NULL)
|
|
OR (
|
|
decision_outcome IS NOT NULL
|
|
AND decision_outcome IN ('upheld', 'partially_upheld', 'not_upheld', 'answered', 'assessed')
|
|
AND decision_reason IS NOT NULL
|
|
AND char_length(decision_reason) BETWEEN 1 AND 4000
|
|
AND decision_at IS NOT NULL
|
|
)
|
|
),
|
|
CONSTRAINT support_cases_timestamps CHECK (
|
|
updated_at >= created_at
|
|
AND (target_answered_at IS NULL OR target_answered_at <= updated_at)
|
|
AND (decision_at IS NULL OR (decision_at >= created_at AND decision_at <= updated_at))
|
|
),
|
|
CONSTRAINT support_cases_fields_match_type CHECK (
|
|
(
|
|
case_type = 'support'
|
|
AND (complaint_reason IS NULL)
|
|
AND rubric_version IS NULL
|
|
AND assessment_specialization IS NULL
|
|
)
|
|
OR (
|
|
case_type = 'complaint'
|
|
AND object_type IS NOT NULL
|
|
AND object_type IN ('organization', 'invitation')
|
|
AND complaint_reason IS NOT NULL
|
|
AND assessment_specialization IS NULL
|
|
AND rubric_version IS NULL
|
|
AND (complaint_reason NOT IN ('offer_content', 'salary_terms_violation') OR object_type = 'invitation')
|
|
)
|
|
OR (
|
|
case_type = 'grade_appeal'
|
|
AND object_type IS NOT NULL
|
|
AND object_type IN ('assessment_attempt', 'support_case')
|
|
AND complaint_reason IS NULL
|
|
AND assessment_specialization IS NULL
|
|
AND rubric_version IS NULL
|
|
)
|
|
OR (
|
|
case_type = 'anticheat_appeal'
|
|
AND object_type IS NOT NULL
|
|
AND object_type = 'assessment_attempt'
|
|
AND complaint_reason IS NULL
|
|
AND assessment_specialization IS NULL
|
|
AND rubric_version IS NULL
|
|
)
|
|
OR (
|
|
case_type = 'manual_assessment'
|
|
AND assessment_specialization IS NOT NULL
|
|
AND complaint_reason IS NULL
|
|
AND (object_type IS NULL OR object_type = 'assessment_attempt')
|
|
)
|
|
),
|
|
CONSTRAINT support_cases_decision_matches_type CHECK (
|
|
decision_outcome IS NULL
|
|
OR (case_type = 'support' AND decision_outcome = 'answered')
|
|
OR (case_type = 'manual_assessment' AND decision_outcome = 'assessed')
|
|
OR (
|
|
case_type IN ('complaint', 'grade_appeal', 'anticheat_appeal')
|
|
AND decision_outcome IN ('upheld', 'partially_upheld', 'not_upheld')
|
|
)
|
|
),
|
|
CONSTRAINT support_cases_status_decision CHECK (
|
|
(status = 'resolved') = (decision_outcome IS NOT NULL)
|
|
)
|
|
);
|
|
|
|
-- A stored rubric pin must name the immutable current opening for the exact
|
|
-- target and the grading-policy version used by automated assessment.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION support_cases_rubric_pin_guard() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
opening_policy_version_id uuid;
|
|
pinned_version integer;
|
|
BEGIN
|
|
IF NEW.rubric_version IS NULL THEN
|
|
RETURN NEW;
|
|
END IF;
|
|
|
|
SELECT opening.policy_version_id, policy.version
|
|
INTO opening_policy_version_id, pinned_version
|
|
FROM assessment_category_openings AS opening
|
|
JOIN assessment_category_catalog_bindings AS binding
|
|
ON binding.opening_id = opening.id
|
|
JOIN policy_versions AS policy
|
|
ON policy.id = opening.policy_version_id
|
|
WHERE opening.id = NEW.rubric_opening_id
|
|
AND opening.policy_version_id = NEW.rubric_policy_version_id
|
|
AND binding.specialization = NEW.assessment_specialization
|
|
AND binding.grade = NEW.assessment_grade
|
|
AND policy.policy_key = 'product'
|
|
AND NOT 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)
|
|
)
|
|
AND opening.policy_version_id = (
|
|
SELECT current_policy.id
|
|
FROM policy_versions AS current_policy
|
|
WHERE current_policy.policy_key = 'product'
|
|
AND current_policy.effective_at <= statement_timestamp()
|
|
ORDER BY current_policy.version DESC
|
|
LIMIT 1
|
|
);
|
|
|
|
IF NOT FOUND OR opening_policy_version_id IS DISTINCT FROM NEW.rubric_policy_version_id
|
|
OR pinned_version IS DISTINCT FROM NEW.rubric_version THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'check_violation', CONSTRAINT = 'support_cases_rubric_pin_consistency',
|
|
MESSAGE = 'support case rubric pin does not match its current reviewed opening';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER support_cases_rubric_pin_guard
|
|
BEFORE INSERT OR UPDATE OF rubric_version, rubric_opening_id, rubric_policy_version_id,
|
|
assessment_specialization, assessment_grade, created_at
|
|
ON support_cases
|
|
FOR EACH ROW EXECUTE FUNCTION support_cases_rubric_pin_guard();
|
|
|
|
-- Rubric evidence is fixed at creation even when case status and history later
|
|
-- change.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION support_cases_rubric_pin_immutable() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF (NEW.rubric_version, NEW.rubric_opening_id, NEW.rubric_policy_version_id)
|
|
IS DISTINCT FROM (OLD.rubric_version, OLD.rubric_opening_id, OLD.rubric_policy_version_id) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation', CONSTRAINT = 'support_cases_rubric_pin_immutable',
|
|
MESSAGE = 'support case rubric pin is immutable';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER support_cases_rubric_pin_immutable
|
|
BEFORE UPDATE ON support_cases
|
|
FOR EACH ROW EXECUTE FUNCTION support_cases_rubric_pin_immutable();
|
|
|
|
CREATE INDEX support_cases_owner_created_idx
|
|
ON support_cases (owner_id, created_at DESC, id DESC);
|
|
|
|
-- An appeal is once per owner, appeal type, and target regardless of outcome.
|
|
CREATE UNIQUE INDEX support_cases_one_appeal_per_target
|
|
ON support_cases (owner_id, case_type, object_type, object_id)
|
|
WHERE case_type IN ('grade_appeal', 'anticheat_appeal');
|
|
|
|
CREATE UNIQUE INDEX support_cases_one_open_manual_assessment
|
|
ON support_cases (owner_id, assessment_specialization)
|
|
WHERE case_type = 'manual_assessment' AND status NOT IN ('resolved', 'closed');
|
|
|
|
CREATE UNIQUE INDEX support_cases_one_open_complaint_per_object
|
|
ON support_cases (owner_id, object_type, object_id)
|
|
WHERE case_type = 'complaint' AND status NOT IN ('resolved', 'closed');
|
|
|
|
-- Events are the append-only public history. No staff identity, private note,
|
|
-- evidence blob, or copied object data is present in this table.
|
|
CREATE TABLE support_case_events (
|
|
case_id uuid NOT NULL REFERENCES support_cases (id) ON DELETE CASCADE,
|
|
sequence integer NOT NULL,
|
|
event_kind text NOT NULL,
|
|
actor_kind text NOT NULL,
|
|
occurred_at timestamptz NOT NULL,
|
|
status text,
|
|
message text,
|
|
CONSTRAINT support_case_events_pkey PRIMARY KEY (case_id, sequence),
|
|
CONSTRAINT support_case_events_sequence CHECK (sequence BETWEEN 1 AND 200),
|
|
CONSTRAINT support_case_events_kind CHECK (
|
|
event_kind IN ('created', 'status_changed', 'reply', 'decision')
|
|
),
|
|
CONSTRAINT support_case_events_actor CHECK (actor_kind IN ('user', 'support', 'system')),
|
|
CONSTRAINT support_case_events_status CHECK (
|
|
status IS NULL OR status IN ('submitted', 'in_review', 'waiting_for_user', 'waiting_expert', 'resolved', 'closed')
|
|
),
|
|
CONSTRAINT support_case_events_message CHECK (
|
|
message IS NULL OR (
|
|
char_length(message) BETWEEN 1 AND 4000
|
|
)
|
|
),
|
|
CONSTRAINT support_case_events_shape CHECK (
|
|
(event_kind = 'created' AND actor_kind = 'user' AND status = 'submitted' AND message IS NULL)
|
|
OR (event_kind = 'status_changed' AND actor_kind IN ('support', 'system') AND status IS NOT NULL)
|
|
OR (event_kind = 'reply' AND actor_kind IN ('user', 'support') AND status IS NULL AND message IS NOT NULL)
|
|
OR (event_kind = 'decision' AND actor_kind = 'support' AND status = 'resolved' AND message IS NOT NULL)
|
|
),
|
|
CONSTRAINT support_case_events_close_reason CHECK (
|
|
status <> 'closed' OR (event_kind = 'status_changed' AND message IS NOT NULL)
|
|
)
|
|
);
|
|
|
|
-- Prevent rewriting event history while allowing account deletion cascades.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION support_case_events_refuse_change() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF TG_OP = 'DELETE' AND pg_trigger_depth() > 1 THEN
|
|
RETURN OLD;
|
|
END IF;
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'support_case_events_immutable',
|
|
MESSAGE = 'support case events are immutable';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER support_case_events_refuse_update_delete
|
|
BEFORE UPDATE OR DELETE ON support_case_events
|
|
FOR EACH ROW EXECUTE FUNCTION support_case_events_refuse_change();
|
|
|
|
-- The create result lives exactly as long as its generic scoped idempotency
|
|
-- record. Its original snapshot and late flag cannot be rewritten on replay.
|
|
CREATE TABLE support_case_create_outcomes (
|
|
scope_hash bytea NOT NULL REFERENCES idempotency_records (scope_hash) ON DELETE CASCADE,
|
|
outcome_kind text NOT NULL,
|
|
case_snapshot jsonb,
|
|
target_was_late boolean,
|
|
created_at timestamptz NOT NULL,
|
|
CONSTRAINT support_case_create_outcomes_pkey PRIMARY KEY (scope_hash),
|
|
CONSTRAINT support_case_create_outcomes_scope_hash CHECK (octet_length(scope_hash) = 32),
|
|
CONSTRAINT support_case_create_outcomes_kind CHECK (
|
|
outcome_kind IN ('created', 'not_found', 'state_conflict')
|
|
),
|
|
CONSTRAINT support_case_create_outcomes_snapshot CHECK (
|
|
(
|
|
outcome_kind = 'created'
|
|
AND case_snapshot IS NOT NULL
|
|
AND jsonb_typeof(case_snapshot) = 'object'
|
|
AND octet_length(case_snapshot::text) <= 1048576
|
|
AND target_was_late IS NOT NULL
|
|
) OR (
|
|
outcome_kind IN ('not_found', 'state_conflict')
|
|
AND case_snapshot IS NULL
|
|
AND target_was_late IS NULL
|
|
)
|
|
)
|
|
);
|
|
|
|
-- A live idempotent result cannot change between the original response and a
|
|
-- replay. Expiry cleanup may still delete it through its parent record.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION support_case_create_outcomes_refuse_update() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'support_case_create_outcomes_immutable',
|
|
MESSAGE = 'support case create outcomes are immutable';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER support_case_create_outcomes_refuse_update
|
|
BEFORE UPDATE ON support_case_create_outcomes
|
|
FOR EACH ROW EXECUTE FUNCTION support_case_create_outcomes_refuse_update();
|
|
|
|
-- Request events retain the maximum supported 24-hour window, not merely the
|
|
-- current policy's window. Rows are pruned in small batches by each request.
|
|
CREATE TABLE support_case_request_events (
|
|
id bigint GENERATED ALWAYS AS IDENTITY,
|
|
owner_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
|
requested_at timestamptz NOT NULL,
|
|
CONSTRAINT support_case_request_events_pkey PRIMARY KEY (id)
|
|
);
|
|
|
|
CREATE INDEX support_case_request_events_owner_time_idx
|
|
ON support_case_request_events (owner_id, requested_at, id);
|
|
|
|
-- +goose Down
|
|
DROP TABLE support_case_request_events;
|
|
DROP TRIGGER support_cases_rubric_pin_immutable ON support_cases;
|
|
DROP FUNCTION support_cases_rubric_pin_immutable();
|
|
DROP TRIGGER support_cases_rubric_pin_guard ON support_cases;
|
|
DROP FUNCTION support_cases_rubric_pin_guard();
|
|
DROP TRIGGER support_case_create_outcomes_refuse_update ON support_case_create_outcomes;
|
|
DROP FUNCTION support_case_create_outcomes_refuse_update();
|
|
DROP TABLE support_case_create_outcomes;
|
|
DROP TRIGGER support_case_events_refuse_update_delete ON support_case_events;
|
|
DROP FUNCTION support_case_events_refuse_change();
|
|
DROP TABLE support_case_events;
|
|
DROP INDEX support_cases_one_open_complaint_per_object;
|
|
DROP INDEX support_cases_one_open_manual_assessment;
|
|
DROP INDEX support_cases_one_appeal_per_target;
|
|
DROP INDEX support_cases_owner_created_idx;
|
|
DROP TABLE support_cases;
|
|
DROP SEQUENCE support_case_number_seq;
|