manicanldes-backend/prisma/migrations/20261001234900_b2b_sample_r.../migration.sql

107 lines
7.5 KiB
PL/PgSQL

CREATE TYPE "B2bSampleStatus" AS ENUM ('REQUESTED', 'APPROVED', 'REJECTED', 'DISPATCHED');
CREATE TYPE "B2bSampleEventKind" AS ENUM ('APPROVED', 'REJECTED', 'DISPATCHED');
CREATE TABLE "b2b_sample_requests" (
"id" UUID NOT NULL PRIMARY KEY,
"organization_id" UUID NOT NULL,
"account_id" UUID NOT NULL,
"variant_id" UUID NOT NULL,
"requested_by_id" UUID NOT NULL,
"sku" VARCHAR(64) NOT NULL,
"variant_name" VARCHAR(160) NOT NULL,
"quantity" INTEGER NOT NULL,
"purpose" VARCHAR(500) NOT NULL,
"status" "B2bSampleStatus" NOT NULL DEFAULT 'REQUESTED',
"idempotency_key" UUID NOT NULL,
"request_hash" CHAR(64) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ(3) NOT NULL,
CONSTRAINT "b2b_sample_requests_quantity" CHECK ("quantity" BETWEEN 1 AND 100),
CONSTRAINT "b2b_sample_requests_purpose" CHECK (char_length(btrim("purpose")) BETWEEN 3 AND 500),
CONSTRAINT "b2b_sample_requests_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$')
);
CREATE UNIQUE INDEX "b2b_sample_requests_id_organization_id_key" ON "b2b_sample_requests"("id", "organization_id");
CREATE UNIQUE INDEX "b2b_sample_requests_organization_id_idempotency_key_key" ON "b2b_sample_requests"("organization_id", "idempotency_key");
CREATE INDEX "b2b_sample_requests_organization_id_account_id_created_at_i_idx" ON "b2b_sample_requests"("organization_id", "account_id", "created_at", "id");
ALTER TABLE "b2b_sample_requests" ADD CONSTRAINT "b2b_sample_requests_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "b2b_sample_requests" ADD CONSTRAINT "b2b_sample_requests_account_id_organization_id_fkey" FOREIGN KEY ("account_id", "organization_id") REFERENCES "business_accounts"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "b2b_sample_requests" ADD CONSTRAINT "b2b_sample_requests_variant_id_organization_id_fkey" FOREIGN KEY ("variant_id", "organization_id") REFERENCES "product_variants"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "b2b_sample_requests" ADD CONSTRAINT "b2b_sample_requests_requested_by_id_organization_id_fkey" FOREIGN KEY ("requested_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
CREATE TABLE "b2b_sample_events" (
"id" UUID NOT NULL PRIMARY KEY,
"organization_id" UUID NOT NULL,
"request_id" UUID NOT NULL,
"actor_id" UUID NOT NULL,
"sequence" INTEGER NOT NULL,
"kind" "B2bSampleEventKind" NOT NULL,
"note" VARCHAR(500) NOT NULL,
"dispatch_reference" VARCHAR(100),
"idempotency_key" UUID NOT NULL,
"request_hash" CHAR(64) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "b2b_sample_events_sequence" CHECK ("sequence" BETWEEN 1 AND 2),
CONSTRAINT "b2b_sample_events_note" CHECK (char_length(btrim("note")) BETWEEN 3 AND 500),
CONSTRAINT "b2b_sample_events_reference" CHECK (("kind" = 'DISPATCHED' AND char_length(btrim("dispatch_reference")) BETWEEN 3 AND 100) OR ("kind" <> 'DISPATCHED' AND "dispatch_reference" IS NULL)),
CONSTRAINT "b2b_sample_events_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$')
);
CREATE UNIQUE INDEX "b2b_sample_events_request_id_sequence_key" ON "b2b_sample_events"("request_id", "sequence");
CREATE UNIQUE INDEX "b2b_sample_events_organization_id_idempotency_key_key" ON "b2b_sample_events"("organization_id", "idempotency_key");
CREATE INDEX "b2b_sample_events_organization_id_request_id_sequence_idx" ON "b2b_sample_events"("organization_id", "request_id", "sequence");
ALTER TABLE "b2b_sample_events" ADD CONSTRAINT "b2b_sample_events_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "b2b_sample_events" ADD CONSTRAINT "b2b_sample_events_request_id_organization_id_fkey" FOREIGN KEY ("request_id", "organization_id") REFERENCES "b2b_sample_requests"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "b2b_sample_events" ADD CONSTRAINT "b2b_sample_events_actor_id_organization_id_fkey" FOREIGN KEY ("actor_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
CREATE FUNCTION guard_b2b_sample_request() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE latest "B2bSampleEventKind";
BEGIN
IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'Sample requests cannot be deleted'; END IF;
IF TG_OP = 'INSERT' THEN
IF NEW.status <> 'REQUESTED' THEN RAISE EXCEPTION 'Sample requests must start requested'; END IF;
RETURN NEW;
END IF;
IF (NEW.id, NEW.organization_id, NEW.account_id, NEW.variant_id, NEW.requested_by_id,
NEW.sku, NEW.variant_name, NEW.quantity, NEW.purpose, NEW.idempotency_key, NEW.request_hash, NEW.created_at)
IS DISTINCT FROM (OLD.id, OLD.organization_id, OLD.account_id, OLD.variant_id, OLD.requested_by_id,
OLD.sku, OLD.variant_name, OLD.quantity, OLD.purpose, OLD.idempotency_key, OLD.request_hash, OLD.created_at)
THEN RAISE EXCEPTION 'Sample request terms are immutable'; END IF;
SELECT kind INTO latest FROM b2b_sample_events WHERE request_id = NEW.id ORDER BY sequence DESC LIMIT 1;
IF NOT ((OLD.status = 'REQUESTED' AND NEW.status = 'APPROVED' AND latest = 'APPROVED')
OR (OLD.status = 'REQUESTED' AND NEW.status = 'REJECTED' AND latest = 'REJECTED')
OR (OLD.status = 'APPROVED' AND NEW.status = 'DISPATCHED' AND latest = 'DISPATCHED'))
THEN RAISE EXCEPTION 'Sample transition lacks matching event'; END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER b2b_sample_requests_guard BEFORE INSERT OR UPDATE OR DELETE ON b2b_sample_requests FOR EACH ROW EXECUTE FUNCTION guard_b2b_sample_request();
CREATE FUNCTION guard_b2b_sample_event() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE request b2b_sample_requests%ROWTYPE;
BEGIN
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Sample events are immutable'; END IF;
SELECT * INTO request FROM b2b_sample_requests WHERE id = NEW.request_id AND organization_id = NEW.organization_id;
IF NEW.kind IN ('APPROVED', 'REJECTED') THEN
IF NEW.sequence <> 1 OR request.status <> 'REQUESTED' OR NEW.actor_id = request.requested_by_id
THEN RAISE EXCEPTION 'Sample decision requires an independent reviewer and requested sample'; END IF;
ELSIF NEW.kind = 'DISPATCHED' THEN
IF NEW.sequence <> 2 OR request.status <> 'APPROVED'
THEN RAISE EXCEPTION 'Sample dispatch requires an approved request'; END IF;
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER b2b_sample_events_guard BEFORE INSERT OR UPDATE OR DELETE ON b2b_sample_events FOR EACH ROW EXECUTE FUNCTION guard_b2b_sample_event();
CREATE FUNCTION check_b2b_sample_event_status() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE current_status "B2bSampleStatus";
BEGIN
SELECT status INTO current_status FROM b2b_sample_requests WHERE id = NEW.request_id;
IF (NEW.kind = 'APPROVED' AND current_status <> 'APPROVED')
OR (NEW.kind = 'REJECTED' AND current_status <> 'REJECTED')
OR (NEW.kind = 'DISPATCHED' AND current_status <> 'DISPATCHED')
THEN RAISE EXCEPTION 'Sample event must update request status in the same transaction'; END IF;
RETURN NEW;
END;
$$;
CREATE CONSTRAINT TRIGGER b2b_sample_events_status_guard AFTER INSERT ON b2b_sample_events
DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION check_b2b_sample_event_status();
UPDATE "roles" SET "permissions" = ARRAY(SELECT DISTINCT unnest("permissions" || ARRAY['b2b.sample.read', 'b2b.sample.request', 'b2b.sample.review', 'b2b.sample.dispatch']))
WHERE "is_system" = true AND "name" = 'Owner';