142 lines
9.5 KiB
PL/PgSQL
142 lines
9.5 KiB
PL/PgSQL
CREATE TYPE "B2bQuoteResponseKind" AS ENUM ('ACCEPTED', 'DECLINED', 'WITHDRAWN');
|
|
|
|
CREATE TABLE "b2b_quotes" (
|
|
"id" UUID NOT NULL PRIMARY KEY,
|
|
"organization_id" UUID NOT NULL,
|
|
"account_id" UUID NOT NULL,
|
|
"price_list_id" UUID NOT NULL,
|
|
"created_by_id" UUID NOT NULL,
|
|
"number" VARCHAR(24) NOT NULL,
|
|
"account_snapshot" JSONB NOT NULL,
|
|
"price_list_name" VARCHAR(100) NOT NULL,
|
|
"currency" CHAR(3) NOT NULL,
|
|
"merchandise_total" DECIMAL(18,2) NOT NULL,
|
|
"customer_reference" VARCHAR(100),
|
|
"expires_at" TIMESTAMPTZ(3) NOT NULL,
|
|
"idempotency_key" UUID NOT NULL,
|
|
"request_hash" CHAR(64) NOT NULL,
|
|
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
CONSTRAINT "b2b_quotes_number_format" CHECK ("number" ~ '^BQ-[A-F0-9]{20}$'),
|
|
CONSTRAINT "b2b_quotes_total" CHECK ("merchandise_total" > 0),
|
|
CONSTRAINT "b2b_quotes_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$')
|
|
);
|
|
CREATE UNIQUE INDEX "b2b_quotes_id_organization_id_key" ON "b2b_quotes"("id", "organization_id");
|
|
CREATE UNIQUE INDEX "b2b_quotes_organization_id_number_key" ON "b2b_quotes"("organization_id", "number");
|
|
CREATE UNIQUE INDEX "b2b_quotes_organization_id_idempotency_key_key" ON "b2b_quotes"("organization_id", "idempotency_key");
|
|
CREATE INDEX "b2b_quotes_organization_id_account_id_created_at_id_idx" ON "b2b_quotes"("organization_id", "account_id", "created_at", "id");
|
|
ALTER TABLE "b2b_quotes" ADD CONSTRAINT "b2b_quotes_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "b2b_quotes" ADD CONSTRAINT "b2b_quotes_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_quotes" ADD CONSTRAINT "b2b_quotes_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_quotes" ADD CONSTRAINT "b2b_quotes_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_quote_lines" (
|
|
"id" UUID NOT NULL PRIMARY KEY,
|
|
"organization_id" UUID NOT NULL,
|
|
"quote_id" UUID NOT NULL,
|
|
"variant_id" UUID NOT NULL,
|
|
"sku" VARCHAR(64) NOT NULL,
|
|
"name" VARCHAR(160) NOT NULL,
|
|
"quantity" INTEGER NOT NULL,
|
|
"min_quantity" INTEGER NOT NULL,
|
|
"unit_price" DECIMAL(12,2) NOT NULL,
|
|
"line_total" DECIMAL(18,2) NOT NULL,
|
|
CONSTRAINT "b2b_quote_lines_quantity" CHECK ("quantity" BETWEEN 1 AND 1000000 AND "quantity" >= "min_quantity"),
|
|
CONSTRAINT "b2b_quote_lines_money" CHECK ("unit_price" > 0 AND "line_total" = "unit_price" * "quantity")
|
|
);
|
|
CREATE UNIQUE INDEX "b2b_quote_lines_quote_id_variant_id_key" ON "b2b_quote_lines"("quote_id", "variant_id");
|
|
CREATE INDEX "b2b_quote_lines_organization_id_quote_id_id_idx" ON "b2b_quote_lines"("organization_id", "quote_id", "id");
|
|
ALTER TABLE "b2b_quote_lines" ADD CONSTRAINT "b2b_quote_lines_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "b2b_quote_lines" ADD CONSTRAINT "b2b_quote_lines_quote_id_organization_id_fkey" FOREIGN KEY ("quote_id", "organization_id") REFERENCES "b2b_quotes"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "b2b_quote_lines" ADD CONSTRAINT "b2b_quote_lines_variant_id_organization_id_fkey" FOREIGN KEY ("variant_id", "organization_id") REFERENCES "product_variants"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
CREATE TABLE "b2b_quote_responses" (
|
|
"id" UUID NOT NULL PRIMARY KEY,
|
|
"organization_id" UUID NOT NULL,
|
|
"quote_id" UUID NOT NULL,
|
|
"actor_id" UUID NOT NULL,
|
|
"kind" "B2bQuoteResponseKind" NOT NULL,
|
|
"note" VARCHAR(500) NOT NULL,
|
|
"idempotency_key" UUID NOT NULL,
|
|
"request_hash" CHAR(64) NOT NULL,
|
|
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
CONSTRAINT "b2b_quote_responses_note" CHECK (char_length(btrim("note")) BETWEEN 3 AND 500),
|
|
CONSTRAINT "b2b_quote_responses_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$')
|
|
);
|
|
CREATE UNIQUE INDEX "b2b_quote_responses_quote_id_organization_id_key" ON "b2b_quote_responses"("quote_id", "organization_id");
|
|
CREATE UNIQUE INDEX "b2b_quote_responses_organization_id_idempotency_key_key" ON "b2b_quote_responses"("organization_id", "idempotency_key");
|
|
CREATE INDEX "b2b_quote_responses_organization_id_quote_id_idx" ON "b2b_quote_responses"("organization_id", "quote_id");
|
|
ALTER TABLE "b2b_quote_responses" ADD CONSTRAINT "b2b_quote_responses_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "b2b_quote_responses" ADD CONSTRAINT "b2b_quote_responses_quote_id_organization_id_fkey" FOREIGN KEY ("quote_id", "organization_id") REFERENCES "b2b_quotes"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "b2b_quote_responses" ADD CONSTRAINT "b2b_quote_responses_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_quote() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
DECLARE account business_accounts%ROWTYPE;
|
|
DECLARE list b2b_price_lists%ROWTYPE;
|
|
BEGIN
|
|
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Issued B2B quotations are immutable'; END IF;
|
|
SELECT * INTO account FROM business_accounts WHERE id = NEW.account_id AND organization_id = NEW.organization_id;
|
|
SELECT * INTO list FROM b2b_price_lists WHERE id = NEW.price_list_id AND organization_id = NEW.organization_id;
|
|
IF list.status IS DISTINCT FROM 'ACTIVE' OR list.account_id IS DISTINCT FROM NEW.account_id
|
|
OR list.currency IS DISTINCT FROM NEW.currency OR list.name IS DISTINCT FROM NEW.price_list_name
|
|
THEN RAISE EXCEPTION 'Quotation price list does not match the active account terms'; END IF;
|
|
IF NOT EXISTS (SELECT 1 FROM business_verification_events
|
|
WHERE account_id = NEW.account_id AND kind = 'APPROVED' AND sequence =
|
|
(SELECT MAX(sequence) FROM business_verification_events WHERE account_id = NEW.account_id))
|
|
THEN RAISE EXCEPTION 'Quotation business account is not verified'; END IF;
|
|
IF NEW.expires_at <= CURRENT_TIMESTAMP OR NEW.expires_at > CURRENT_TIMESTAMP + INTERVAL '90 days'
|
|
THEN RAISE EXCEPTION 'Quotation expiry must be within ninety days'; END IF;
|
|
IF NEW.account_snapshot IS DISTINCT FROM jsonb_build_object(
|
|
'legalName', account.legal_name, 'tradingName', account.trading_name,
|
|
'registrationNumber', account.registration_number, 'taxIdentifier', account.tax_identifier,
|
|
'countryCode', account.country_code, 'contactEmail', account.contact_email,
|
|
'contactPhone', account.contact_phone, 'billingLine1', account.billing_line1,
|
|
'billingCity', account.billing_city, 'billingRegion', account.billing_region,
|
|
'billingPostalCode', account.billing_postal_code, 'profileVersion', account.profile_version
|
|
) THEN RAISE EXCEPTION 'Quotation account snapshot is not current'; END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE TRIGGER b2b_quotes_guard BEFORE INSERT OR UPDATE OR DELETE ON b2b_quotes FOR EACH ROW EXECUTE FUNCTION guard_b2b_quote();
|
|
|
|
CREATE FUNCTION guard_b2b_quote_line() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
DECLARE quote b2b_quotes%ROWTYPE;
|
|
DECLARE item b2b_price_list_items%ROWTYPE;
|
|
DECLARE variant product_variants%ROWTYPE;
|
|
BEGIN
|
|
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Issued B2B quotation lines are immutable'; END IF;
|
|
SELECT * INTO quote FROM b2b_quotes WHERE id = NEW.quote_id AND organization_id = NEW.organization_id;
|
|
SELECT * INTO item FROM b2b_price_list_items WHERE price_list_id = quote.price_list_id AND variant_id = NEW.variant_id;
|
|
SELECT * INTO variant FROM product_variants WHERE id = NEW.variant_id AND organization_id = NEW.organization_id;
|
|
IF item.id IS NULL OR item.unit_price <> NEW.unit_price OR item.min_quantity <> NEW.min_quantity
|
|
OR variant.sku IS DISTINCT FROM NEW.sku OR variant.name IS DISTINCT FROM NEW.name
|
|
THEN RAISE EXCEPTION 'Quotation line does not match its price list item and variant snapshot'; END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE TRIGGER b2b_quote_lines_guard BEFORE INSERT OR UPDATE OR DELETE ON b2b_quote_lines FOR EACH ROW EXECUTE FUNCTION guard_b2b_quote_line();
|
|
|
|
CREATE FUNCTION check_b2b_quote_total() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF (SELECT COUNT(*) FROM b2b_quote_lines WHERE quote_id = NEW.id) = 0 OR
|
|
(SELECT SUM(line_total) FROM b2b_quote_lines WHERE quote_id = NEW.id) <> NEW.merchandise_total
|
|
THEN RAISE EXCEPTION 'Quotation total must equal its line snapshots'; END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE CONSTRAINT TRIGGER b2b_quotes_total_guard AFTER INSERT ON b2b_quotes
|
|
DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION check_b2b_quote_total();
|
|
|
|
CREATE FUNCTION guard_b2b_quote_response() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
DECLARE deadline TIMESTAMPTZ;
|
|
BEGIN
|
|
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Quotation responses are immutable'; END IF;
|
|
SELECT expires_at INTO deadline FROM b2b_quotes WHERE id = NEW.quote_id AND organization_id = NEW.organization_id;
|
|
IF deadline <= CURRENT_TIMESTAMP THEN RAISE EXCEPTION 'Quotation has expired'; END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE TRIGGER b2b_quote_responses_guard BEFORE INSERT OR UPDATE OR DELETE ON b2b_quote_responses FOR EACH ROW EXECUTE FUNCTION guard_b2b_quote_response();
|
|
|
|
UPDATE "roles" SET "permissions" = ARRAY(SELECT DISTINCT unnest("permissions" || ARRAY['b2b.quote.read', 'b2b.quote.manage']))
|
|
WHERE "is_system" = true AND "name" = 'Owner';
|