feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -0,0 +1,168 @@
|
||||
-- +goose Up
|
||||
-- Immutable audit records for employer reads of anonymous cards and, when a
|
||||
-- future grant-backed operation exists, selected contact fields. The table
|
||||
-- contains identifiers and field codes only, never the viewed content.
|
||||
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION candidate_card_access_fields_are_valid(kind text, field_codes text []) RETURNS boolean
|
||||
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
||||
SELECT
|
||||
cardinality(field_codes) BETWEEN 0 AND 6
|
||||
AND array_position(field_codes, NULL) IS NULL
|
||||
AND field_codes = ARRAY(
|
||||
SELECT DISTINCT code COLLATE "C"
|
||||
FROM unnest(field_codes) AS code
|
||||
ORDER BY code COLLATE "C"
|
||||
)
|
||||
AND (
|
||||
(
|
||||
kind = 'card_viewed'
|
||||
AND field_codes <@ ARRAY[
|
||||
'categories', 'experience', 'salary_compatibility',
|
||||
'self_assessment', 'stack', 'work_formats'
|
||||
]::text []
|
||||
)
|
||||
OR (
|
||||
kind = 'contact_read'
|
||||
AND cardinality(field_codes) BETWEEN 1 AND 4
|
||||
AND field_codes <@ ARRAY['email', 'full_name', 'phone', 'telegram']::text []
|
||||
)
|
||||
);
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TABLE candidate_card_access_events (
|
||||
id uuid NOT NULL,
|
||||
candidate_id uuid NOT NULL,
|
||||
organization_id uuid NOT NULL,
|
||||
actor_id uuid,
|
||||
kind text NOT NULL,
|
||||
field_codes text [] NOT NULL,
|
||||
occurred_at timestamptz NOT NULL,
|
||||
CONSTRAINT candidate_card_access_events_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT candidate_card_access_events_candidate_fkey FOREIGN KEY (candidate_id)
|
||||
REFERENCES users (id) ON DELETE CASCADE,
|
||||
CONSTRAINT candidate_card_access_events_organization_fkey FOREIGN KEY (organization_id)
|
||||
REFERENCES organizations (id) ON DELETE RESTRICT,
|
||||
CONSTRAINT candidate_card_access_events_actor_fkey FOREIGN KEY (actor_id)
|
||||
REFERENCES users (id) ON DELETE SET NULL,
|
||||
CONSTRAINT candidate_card_access_events_kind_known CHECK (
|
||||
kind IN ('card_viewed', 'contact_read')
|
||||
),
|
||||
CONSTRAINT candidate_card_access_events_fields_valid CHECK (
|
||||
candidate_card_access_fields_are_valid(kind, field_codes)
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX candidate_card_access_events_candidate_occurred_idx
|
||||
ON candidate_card_access_events (candidate_id, occurred_at DESC, id DESC);
|
||||
|
||||
CREATE INDEX candidate_card_access_events_organization_occurred_idx
|
||||
ON candidate_card_access_events (organization_id, occurred_at DESC, id DESC);
|
||||
|
||||
-- Records cannot be altered after insertion. Deletion is allowed only for
|
||||
-- unverified-account cleanup; confirmed-account history remains evidence.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION candidate_card_access_events_refuse_update() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
-- Actor deletion may anonymize the foreign key while preserving every
|
||||
-- other part of the historical event.
|
||||
IF pg_trigger_depth() > 1
|
||||
AND OLD.actor_id IS NOT NULL
|
||||
AND NEW.actor_id IS NULL
|
||||
AND NEW.id IS NOT DISTINCT FROM OLD.id
|
||||
AND NEW.candidate_id IS NOT DISTINCT FROM OLD.candidate_id
|
||||
AND NEW.organization_id IS NOT DISTINCT FROM OLD.organization_id
|
||||
AND NEW.kind IS NOT DISTINCT FROM OLD.kind
|
||||
AND NEW.field_codes IS NOT DISTINCT FROM OLD.field_codes
|
||||
AND NEW.occurred_at IS NOT DISTINCT FROM OLD.occurred_at THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'candidate_card_access_events_immutable',
|
||||
TABLE = 'candidate_card_access_events',
|
||||
MESSAGE = 'candidate card access events are append-only';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER candidate_card_access_events_refuse_update
|
||||
BEFORE UPDATE ON candidate_card_access_events
|
||||
FOR EACH ROW EXECUTE FUNCTION candidate_card_access_events_refuse_update();
|
||||
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION candidate_card_access_events_refuse_delete() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM users
|
||||
WHERE users.id = OLD.candidate_id AND users.email_verified_at IS NOT NULL
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'candidate_card_access_events_retained',
|
||||
TABLE = 'candidate_card_access_events',
|
||||
MESSAGE = 'candidate card access events of a confirmed account are retained';
|
||||
END IF;
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER candidate_card_access_events_refuse_delete
|
||||
BEFORE DELETE ON candidate_card_access_events
|
||||
FOR EACH ROW EXECUTE FUNCTION candidate_card_access_events_refuse_delete();
|
||||
|
||||
-- Protect a confirmed account from a direct DELETE that would otherwise
|
||||
-- cascade away its access history. The unverified registration cleanup may
|
||||
-- still remove those accounts and their records.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION candidate_card_access_events_protect_account() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF OLD.email_verified_at IS NOT NULL AND EXISTS (
|
||||
SELECT 1 FROM candidate_card_access_events
|
||||
WHERE candidate_card_access_events.candidate_id = OLD.id
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'candidate_card_access_events_retained',
|
||||
TABLE = 'users',
|
||||
MESSAGE = 'confirmed account has retained card access history';
|
||||
END IF;
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER candidate_card_access_events_protect_account
|
||||
BEFORE DELETE ON users
|
||||
FOR EACH ROW EXECUTE FUNCTION candidate_card_access_events_protect_account();
|
||||
|
||||
-- +goose Down
|
||||
-- Do not remove durable access evidence as a side effect of rolling back the
|
||||
-- schema. Operators must export or explicitly resolve retained history first.
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM candidate_card_access_events) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'candidate_card_access_events_retained',
|
||||
TABLE = 'candidate_card_access_events',
|
||||
MESSAGE = 'candidate card access history must be retained before rollback';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
DROP TRIGGER candidate_card_access_events_protect_account ON users;
|
||||
DROP FUNCTION candidate_card_access_events_protect_account();
|
||||
DROP TRIGGER candidate_card_access_events_refuse_delete ON candidate_card_access_events;
|
||||
DROP FUNCTION candidate_card_access_events_refuse_delete();
|
||||
DROP TRIGGER candidate_card_access_events_refuse_update ON candidate_card_access_events;
|
||||
DROP FUNCTION candidate_card_access_events_refuse_update();
|
||||
DROP TABLE candidate_card_access_events;
|
||||
DROP FUNCTION candidate_card_access_fields_are_valid(text, text []);
|
||||
Reference in new issue
Block a user