CREATE TYPE "SupplierDocumentKind" AS ENUM ('REGISTRATION', 'TAX_CERTIFICATE', 'COMPLIANCE_CERTIFICATE', 'OTHER'); CREATE TYPE "SupplierDocumentOutcome" AS ENUM ('APPROVED', 'REJECTED'); CREATE TABLE "supplier_documents" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "supplier_id" UUID NOT NULL, "created_by_id" UUID NOT NULL, "code" VARCHAR(32) NOT NULL, "kind" "SupplierDocumentKind" NOT NULL, "title" VARCHAR(160) NOT NULL, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "supplier_documents_pkey" PRIMARY KEY ("id"), CONSTRAINT "supplier_documents_code" CHECK ("code" ~ '^[A-Z0-9][A-Z0-9_-]{0,31}$' AND LENGTH(TRIM("title")) > 0) ); CREATE UNIQUE INDEX "supplier_documents_organization_id_supplier_id_code_key" ON "supplier_documents"("organization_id", "supplier_id", "code"); CREATE UNIQUE INDEX "supplier_documents_id_organization_id_key" ON "supplier_documents"("id", "organization_id"); CREATE INDEX "supplier_documents_supplier_created_idx" ON "supplier_documents"("organization_id", "supplier_id", "created_at", "id"); ALTER TABLE "supplier_documents" ADD CONSTRAINT "supplier_documents_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "supplier_documents" ADD CONSTRAINT "supplier_documents_supplier_id_organization_id_fkey" FOREIGN KEY ("supplier_id", "organization_id") REFERENCES "suppliers"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "supplier_documents" ADD CONSTRAINT "supplier_documents_created_by_id_organization_id_fkey" FOREIGN KEY ("created_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE TABLE "supplier_document_versions" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "document_id" UUID NOT NULL, "uploaded_by_id" UUID NOT NULL, "version" INTEGER NOT NULL, "filename" VARCHAR(120) NOT NULL, "mime_type" VARCHAR(32) NOT NULL, "sha256" CHAR(64) NOT NULL, "size" INTEGER NOT NULL, "content" BYTEA NOT NULL, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "supplier_document_versions_pkey" PRIMARY KEY ("id"), CONSTRAINT "supplier_document_versions_valid" CHECK ( "version" BETWEEN 1 AND 20 AND "size" BETWEEN 1 AND 2097152 AND OCTET_LENGTH("content") = "size" AND SUBSTRING("content" FROM 1 FOR 5) = decode('255044462d', 'hex') AND "mime_type" = 'application/pdf' AND "sha256" ~ '^[a-f0-9]{64}$' AND "filename" ~ '^[A-Za-z0-9][A-Za-z0-9._-]{0,115}[.]pdf$' ) ); CREATE UNIQUE INDEX "supplier_document_versions_document_id_version_key" ON "supplier_document_versions"("document_id", "version"); CREATE UNIQUE INDEX "supplier_document_versions_id_organization_id_key" ON "supplier_document_versions"("id", "organization_id"); CREATE INDEX "supplier_document_versions_organization_id_document_id_vers_idx" ON "supplier_document_versions"("organization_id", "document_id", "version"); ALTER TABLE "supplier_document_versions" ADD CONSTRAINT "supplier_document_versions_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "supplier_document_versions" ADD CONSTRAINT "supplier_document_versions_document_id_organization_id_fkey" FOREIGN KEY ("document_id", "organization_id") REFERENCES "supplier_documents"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "supplier_document_versions" ADD CONSTRAINT "supplier_document_versions_uploaded_by_id_organization_id_fkey" FOREIGN KEY ("uploaded_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE TABLE "supplier_document_decisions" ( "version_id" UUID NOT NULL, "organization_id" UUID NOT NULL, "validated_by_id" UUID NOT NULL, "outcome" "SupplierDocumentOutcome" NOT NULL, "reason" VARCHAR(500), "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "supplier_document_decisions_pkey" PRIMARY KEY ("version_id"), CONSTRAINT "supplier_document_decisions_reason" CHECK ("outcome" <> 'REJECTED' OR ("reason" IS NOT NULL AND LENGTH(TRIM("reason")) > 0)) ); CREATE UNIQUE INDEX "supplier_document_decisions_version_id_organization_id_key" ON "supplier_document_decisions"("version_id", "organization_id"); ALTER TABLE "supplier_document_decisions" ADD CONSTRAINT "supplier_document_decisions_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "supplier_document_decisions" ADD CONSTRAINT "supplier_document_decision_version_scope_fkey" FOREIGN KEY ("version_id", "organization_id") REFERENCES "supplier_document_versions"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "supplier_document_decisions" ADD CONSTRAINT "supplier_document_decision_actor_scope_fkey" FOREIGN KEY ("validated_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; UPDATE "roles" SET "permissions" = array_append(array_append(array_append("permissions", 'supplier_document.read'), 'supplier_document.manage'), 'supplier_document.validate') WHERE "is_system" = true AND "name" = 'Owner' AND NOT ('supplier_document.read' = ANY("permissions")); CREATE FUNCTION check_supplier_document_version() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE previous_version INTEGER; BEGIN PERFORM id FROM supplier_documents WHERE id = NEW.document_id FOR UPDATE; SELECT COALESCE(MAX(version), 0) INTO previous_version FROM supplier_document_versions WHERE document_id = NEW.document_id; IF NEW.version <> previous_version + 1 THEN RAISE EXCEPTION 'Supplier document version must be sequential'; END IF; RETURN NEW; END; $$; CREATE TRIGGER supplier_document_version_sequence BEFORE INSERT ON supplier_document_versions FOR EACH ROW EXECUTE FUNCTION check_supplier_document_version(); CREATE FUNCTION reject_supplier_document_mutation() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'Supplier document records are immutable'; END; $$; CREATE TRIGGER supplier_documents_immutable BEFORE UPDATE OR DELETE ON supplier_documents FOR EACH ROW EXECUTE FUNCTION reject_supplier_document_mutation(); CREATE TRIGGER supplier_document_versions_immutable BEFORE UPDATE OR DELETE ON supplier_document_versions FOR EACH ROW EXECUTE FUNCTION reject_supplier_document_mutation(); CREATE TRIGGER supplier_document_decisions_immutable BEFORE UPDATE OR DELETE ON supplier_document_decisions FOR EACH ROW EXECUTE FUNCTION reject_supplier_document_mutation();