CREATE TYPE "B2bPriceListStatus" AS ENUM ('DRAFT', 'ACTIVE', 'ARCHIVED'); CREATE TABLE "b2b_price_lists" ( "id" UUID NOT NULL PRIMARY KEY, "organization_id" UUID NOT NULL, "account_id" UUID NOT NULL, "created_by_id" UUID NOT NULL, "name" VARCHAR(100) NOT NULL, "currency" CHAR(3) NOT NULL, "status" "B2bPriceListStatus" NOT NULL DEFAULT 'DRAFT', "active_slot" BOOLEAN, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMPTZ(3) NOT NULL, CONSTRAINT "b2b_price_lists_currency" CHECK ("currency" IN ('INR', 'USD', 'EUR', 'GBP')), CONSTRAINT "b2b_price_lists_active_slot" CHECK ( ("status" = 'ACTIVE' AND "active_slot" IS TRUE) OR ("status" <> 'ACTIVE' AND "active_slot" IS NULL) ) ); CREATE UNIQUE INDEX "b2b_price_lists_id_organization_id_key" ON "b2b_price_lists"("id", "organization_id"); CREATE UNIQUE INDEX "b2b_price_lists_organization_id_account_id_currency_active__key" ON "b2b_price_lists"("organization_id", "account_id", "currency", "active_slot"); CREATE INDEX "b2b_price_lists_organization_id_account_id_status_created_a_idx" ON "b2b_price_lists"("organization_id", "account_id", "status", "created_at", "id"); ALTER TABLE "b2b_price_lists" ADD CONSTRAINT "b2b_price_lists_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "b2b_price_lists" ADD CONSTRAINT "b2b_price_lists_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_price_lists" ADD CONSTRAINT "b2b_price_lists_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 "b2b_price_list_items" ( "id" UUID NOT NULL PRIMARY KEY, "organization_id" UUID NOT NULL, "price_list_id" UUID NOT NULL, "variant_id" UUID NOT NULL, "unit_price" DECIMAL(12,2) NOT NULL, "min_quantity" INTEGER NOT NULL, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMPTZ(3) NOT NULL, CONSTRAINT "b2b_price_list_items_price" CHECK ("unit_price" > 0), CONSTRAINT "b2b_price_list_items_min_quantity" CHECK ("min_quantity" BETWEEN 1 AND 1000000) ); CREATE UNIQUE INDEX "b2b_price_list_items_price_list_id_variant_id_key" ON "b2b_price_list_items"("price_list_id", "variant_id"); CREATE INDEX "b2b_price_list_items_organization_id_price_list_id_id_idx" ON "b2b_price_list_items"("organization_id", "price_list_id", "id"); ALTER TABLE "b2b_price_list_items" ADD CONSTRAINT "b2b_price_list_items_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "b2b_price_list_items" ADD CONSTRAINT "b2b_price_list_items_price_list_id_organization_id_fkey" FOREIGN KEY ("price_list_id", "organization_id") REFERENCES "b2b_price_lists"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "b2b_price_list_items" ADD CONSTRAINT "b2b_price_list_items_variant_id_organization_id_fkey" FOREIGN KEY ("variant_id", "organization_id") REFERENCES "product_variants"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; CREATE FUNCTION guard_b2b_price_list() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE verified BOOLEAN; BEGIN IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'B2B price lists cannot be deleted'; END IF; IF TG_OP = 'INSERT' THEN RETURN NEW; END IF; IF (NEW.id, NEW.organization_id, NEW.account_id, NEW.created_by_id, NEW.name, NEW.currency, NEW.created_at) IS DISTINCT FROM (OLD.id, OLD.organization_id, OLD.account_id, OLD.created_by_id, OLD.name, OLD.currency, OLD.created_at) THEN RAISE EXCEPTION 'B2B price list identity and terms are immutable'; END IF; IF OLD.status = 'DRAFT' AND NEW.status = 'ACTIVE' THEN SELECT kind = 'APPROVED' INTO verified FROM business_verification_events WHERE account_id = NEW.account_id ORDER BY sequence DESC LIMIT 1; IF COALESCE(verified, FALSE) IS FALSE THEN RAISE EXCEPTION 'Business account is not verified'; END IF; IF NOT EXISTS (SELECT 1 FROM b2b_price_list_items WHERE price_list_id = NEW.id) THEN RAISE EXCEPTION 'B2B price list has no items'; END IF; IF EXISTS ( SELECT 1 FROM b2b_price_list_items i JOIN product_variants v ON v.id = i.variant_id JOIN products p ON p.id = v.product_id WHERE i.price_list_id = NEW.id AND (NOT v.active OR p.status <> 'PUBLISHED' OR v.currency <> NEW.currency) ) THEN RAISE EXCEPTION 'B2B price list contains ineligible variants'; END IF; ELSIF NOT (OLD.status = 'ACTIVE' AND NEW.status = 'ARCHIVED') THEN RAISE EXCEPTION 'B2B price list transition invalid'; END IF; RETURN NEW; END; $$; CREATE TRIGGER b2b_price_lists_guard BEFORE UPDATE OR DELETE ON b2b_price_lists FOR EACH ROW EXECUTE FUNCTION guard_b2b_price_list(); CREATE FUNCTION guard_b2b_price_list_item() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE list_status "B2bPriceListStatus"; DECLARE list_currency CHAR(3); BEGIN IF TG_OP = 'DELETE' THEN SELECT status INTO list_status FROM b2b_price_lists WHERE id = OLD.price_list_id; IF list_status <> 'DRAFT' THEN RAISE EXCEPTION 'Active or archived price list items are immutable'; END IF; RETURN OLD; END IF; SELECT status, currency INTO list_status, list_currency FROM b2b_price_lists WHERE id = NEW.price_list_id AND organization_id = NEW.organization_id; IF list_status <> 'DRAFT' THEN RAISE EXCEPTION 'Active or archived price list items are immutable'; END IF; IF TG_OP = 'UPDATE' AND (NEW.id, NEW.organization_id, NEW.price_list_id, NEW.variant_id, NEW.created_at) IS DISTINCT FROM (OLD.id, OLD.organization_id, OLD.price_list_id, OLD.variant_id, OLD.created_at) THEN RAISE EXCEPTION 'B2B price list item identity is immutable'; END IF; IF NOT EXISTS ( SELECT 1 FROM product_variants v JOIN products p ON p.id = v.product_id WHERE v.id = NEW.variant_id AND v.organization_id = NEW.organization_id AND v.active AND p.status = 'PUBLISHED' AND v.currency = list_currency ) THEN RAISE EXCEPTION 'Variant is ineligible for B2B price list'; END IF; RETURN NEW; END; $$; CREATE TRIGGER b2b_price_list_items_guard BEFORE INSERT OR UPDATE OR DELETE ON b2b_price_list_items FOR EACH ROW EXECUTE FUNCTION guard_b2b_price_list_item(); UPDATE "roles" SET "permissions" = ARRAY(SELECT DISTINCT unnest("permissions" || ARRAY['b2b.price.read', 'b2b.price.manage'])) WHERE "is_system" = true AND "name" = 'Owner';