-- R1 intake (docgen#6 F01, P-R1-K05 Fassung 4 M21): the one place where a -- security notice is recorded, unchanged, at its first arrival in R1. Each -- report gets one stable Eingangsreferenz; the Meldereferenz the reporting -- person sees is the same string (F-1). Everything here is insert-only: a -- correction or a later piece of information is a new entry (M13), and what -- became of an entry downstream is a new receipt. CREATE TABLE intake_counters ( tenant_id UUID PRIMARY KEY REFERENCES tenants(id) ON DELETE CASCADE, last INTEGER NOT NULL CHECK (last >= 1) ); -- One row per report: the reference, the time of the first R1 capture, the -- channel it came in by, who reported and the reporter's own answers that -- routing reads (M16: what it is about; M05: immediate danger). The original -- itself is the report's capture entry. CREATE TABLE intake_reports ( tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE, reference TEXT NOT NULL CHECK (reference ~ '^SH-[0-9]{4}-[0-9]{5,}$'), seq INTEGER NOT NULL CHECK (seq >= 1), received_at TIMESTAMPTZ NOT NULL, channel TEXT NOT NULL CHECK (channel IN ('portal')), reporter_sub TEXT NOT NULL CHECK (char_length(reporter_sub) BETWEEN 1 AND 200), about TEXT NOT NULL CHECK (about IN ('product', 'it_ot', 'unknown')), danger TEXT NOT NULL CHECK (danger IN ('yes', 'no', 'unknown')), PRIMARY KEY (tenant_id, reference), UNIQUE (tenant_id, seq) ); -- The capture (the original, unchanged) and every supplement (Nachtrag). CREATE TABLE intake_entries ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, tenant_id UUID NOT NULL, reference TEXT NOT NULL, kind TEXT NOT NULL CHECK (kind IN ('capture', 'supplement')), content JSONB NOT NULL CHECK (jsonb_typeof(content) = 'object'), by_sub TEXT NOT NULL CHECK (char_length(by_sub) BETWEEN 1 AND 200), at TIMESTAMPTZ NOT NULL, FOREIGN KEY (tenant_id, reference) REFERENCES intake_reports(tenant_id, reference) ON DELETE CASCADE ); CREATE INDEX intake_entries_report ON intake_entries (tenant_id, reference, id); CREATE UNIQUE INDEX intake_entries_one_capture ON intake_entries (tenant_id, reference) WHERE kind = 'capture'; -- What became of an entry downstream (werkmeister, S-01/S-02): waiting (no -- receiving process yet), delivered, processed or refused. Processed and -- refused settle the entry. CREATE TABLE intake_receipts ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, entry_id BIGINT NOT NULL REFERENCES intake_entries(id) ON DELETE CASCADE, status TEXT NOT NULL CHECK (status IN ('waiting', 'delivered', 'processed', 'refused')), reason TEXT NOT NULL DEFAULT '' CHECK (char_length(reason) <= 80), handoff_id TEXT NOT NULL DEFAULT '' CHECK (char_length(handoff_id) <= 80), run_id TEXT NOT NULL DEFAULT '' CHECK (char_length(run_id) <= 80), by_client TEXT NOT NULL CHECK (char_length(by_client) BETWEEN 1 AND 200), at TIMESTAMPTZ NOT NULL ); CREATE INDEX intake_receipts_entry ON intake_receipts (entry_id, id); CREATE UNIQUE INDEX intake_receipts_one_settled ON intake_receipts (entry_id) WHERE status IN ('processed', 'refused'); CREATE FUNCTION intake_immutable() RETURNS trigger AS $$ BEGIN RAISE EXCEPTION '%: rows are never changed; insert a new one', TG_TABLE_NAME; END $$ LANGUAGE plpgsql; CREATE TRIGGER intake_reports_immutable BEFORE UPDATE ON intake_reports FOR EACH ROW EXECUTE FUNCTION intake_immutable(); CREATE TRIGGER intake_entries_immutable BEFORE UPDATE ON intake_entries FOR EACH ROW EXECUTE FUNCTION intake_immutable(); CREATE TRIGGER intake_receipts_immutable BEFORE UPDATE ON intake_receipts FOR EACH ROW EXECUTE FUNCTION intake_immutable();