manicanldes-backend/prisma/migrations/20261004003000_b2b_custom_c.../migration.sql

110 lines
5.7 KiB
PL/PgSQL

-- CreateEnum
CREATE TYPE "B2bCustomCandleDecisionKind" AS ENUM ('APPROVED', 'REJECTED');
-- CreateTable
CREATE TABLE "b2b_custom_candle_requests" (
"id" UUID NOT NULL,
"organization_id" UUID NOT NULL,
"account_id" UUID NOT NULL,
"requested_by_id" UUID NOT NULL,
"quantity" INTEGER NOT NULL,
"wax_type" VARCHAR(100) NOT NULL,
"fragrance" VARCHAR(160) NOT NULL,
"container" VARCHAR(160) NOT NULL,
"color" VARCHAR(100) NOT NULL,
"branding_requirements" VARCHAR(1000) NOT NULL,
"specifications" VARCHAR(2000) NOT NULL,
"idempotency_key" UUID NOT NULL,
"request_hash" CHAR(64) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "b2b_custom_candle_requests_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "b2b_custom_candle_decisions" (
"id" UUID NOT NULL,
"organization_id" UUID NOT NULL,
"request_id" UUID NOT NULL,
"actor_id" UUID NOT NULL,
"kind" "B2bCustomCandleDecisionKind" NOT NULL,
"note" VARCHAR(1000) NOT NULL,
"idempotency_key" UUID NOT NULL,
"request_hash" CHAR(64) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "b2b_custom_candle_decisions_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "b2b_custom_candle_requests_organization_id_account_id_creat_idx" ON "b2b_custom_candle_requests"("organization_id", "account_id", "created_at", "id");
-- CreateIndex
CREATE UNIQUE INDEX "b2b_custom_candle_requests_id_organization_id_key" ON "b2b_custom_candle_requests"("id", "organization_id");
-- CreateIndex
CREATE UNIQUE INDEX "b2b_custom_candle_requests_organization_id_idempotency_key_key" ON "b2b_custom_candle_requests"("organization_id", "idempotency_key");
-- CreateIndex
CREATE UNIQUE INDEX "b2b_custom_candle_decisions_request_id_key" ON "b2b_custom_candle_decisions"("request_id");
-- CreateIndex
CREATE UNIQUE INDEX "b2b_custom_candle_decisions_request_id_organization_id_key" ON "b2b_custom_candle_decisions"("request_id", "organization_id");
-- CreateIndex
CREATE UNIQUE INDEX "b2b_custom_candle_decisions_organization_id_idempotency_key_key" ON "b2b_custom_candle_decisions"("organization_id", "idempotency_key");
-- AddForeignKey
ALTER TABLE "b2b_custom_candle_requests" ADD CONSTRAINT "b2b_custom_candle_requests_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "b2b_custom_candle_requests" ADD CONSTRAINT "b2b_custom_candle_requests_account_id_organization_id_fkey" FOREIGN KEY ("account_id", "organization_id") REFERENCES "business_accounts"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "b2b_custom_candle_requests" ADD CONSTRAINT "b2b_custom_candle_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;
-- AddForeignKey
ALTER TABLE "b2b_custom_candle_decisions" ADD CONSTRAINT "b2b_custom_candle_decisions_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "b2b_custom_candle_decisions" ADD CONSTRAINT "b2b_custom_candle_decisions_request_id_organization_id_fkey" FOREIGN KEY ("request_id", "organization_id") REFERENCES "b2b_custom_candle_requests"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "b2b_custom_candle_decisions" ADD CONSTRAINT "b2b_custom_candle_decisions_actor_id_organization_id_fkey" FOREIGN KEY ("actor_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE b2b_custom_candle_requests
ADD CONSTRAINT custom_candle_quantity CHECK (quantity BETWEEN 1 AND 1000000),
ADD CONSTRAINT custom_candle_text CHECK (
char_length(btrim(wax_type)) >= 1 AND char_length(btrim(fragrance)) >= 1
AND char_length(btrim(container)) >= 1 AND char_length(btrim(color)) >= 1
AND char_length(btrim(branding_requirements)) >= 1 AND char_length(btrim(specifications)) >= 3),
ADD CONSTRAINT custom_candle_request_hash CHECK (request_hash ~ '^[a-f0-9]{64}$');
ALTER TABLE b2b_custom_candle_decisions
ADD CONSTRAINT custom_candle_decision_note CHECK (char_length(btrim(note)) >= 3),
ADD CONSTRAINT custom_candle_decision_hash CHECK (request_hash ~ '^[a-f0-9]{64}$');
CREATE FUNCTION guard_b2b_custom_candle_immutable() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'Custom candle requests and decisions are immutable';
END;
$$;
CREATE TRIGGER b2b_custom_candle_requests_immutable BEFORE UPDATE OR DELETE ON b2b_custom_candle_requests
FOR EACH ROW EXECUTE FUNCTION guard_b2b_custom_candle_immutable();
CREATE TRIGGER b2b_custom_candle_decisions_immutable BEFORE UPDATE OR DELETE ON b2b_custom_candle_decisions
FOR EACH ROW EXECUTE FUNCTION guard_b2b_custom_candle_immutable();
CREATE FUNCTION guard_b2b_custom_candle_reviewer() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE requester UUID;
BEGIN
SELECT requested_by_id INTO requester FROM b2b_custom_candle_requests
WHERE id = NEW.request_id AND organization_id = NEW.organization_id;
IF requester = NEW.actor_id THEN RAISE EXCEPTION 'Custom candle review requires an independent reviewer'; END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER b2b_custom_candle_decisions_reviewer BEFORE INSERT ON b2b_custom_candle_decisions
FOR EACH ROW EXECUTE FUNCTION guard_b2b_custom_candle_reviewer();
UPDATE roles SET permissions = ARRAY(SELECT DISTINCT unnest(permissions || ARRAY['b2b.custom.read', 'b2b.custom.request', 'b2b.custom.review']))
WHERE is_system = true AND name = 'Owner';