CREATE TYPE "BusinessVerificationKind" AS ENUM ('SUBMITTED', 'APPROVED', 'REJECTED'); CREATE TABLE "business_accounts" ( "id" UUID NOT NULL PRIMARY KEY, "organization_id" UUID NOT NULL, "created_by_id" UUID NOT NULL, "code" VARCHAR(32) NOT NULL, "legal_name" VARCHAR(160) NOT NULL, "trading_name" VARCHAR(160), "registration_number" VARCHAR(64) NOT NULL, "tax_identifier" VARCHAR(64), "country_code" CHAR(2) NOT NULL, "contact_email" VARCHAR(254) NOT NULL, "contact_phone" VARCHAR(24), "billing_line1" VARCHAR(200) NOT NULL, "billing_city" VARCHAR(100) NOT NULL, "billing_region" VARCHAR(100) NOT NULL, "billing_postal_code" VARCHAR(20) NOT NULL, "profile_version" INTEGER NOT NULL DEFAULT 1, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMPTZ(3) NOT NULL, CONSTRAINT "business_accounts_profile_version" CHECK ("profile_version" > 0), CONSTRAINT "business_accounts_code" CHECK ("code" ~ '^[A-Z0-9][A-Z0-9_-]*$'), CONSTRAINT "business_accounts_country" CHECK ("country_code" ~ '^[A-Z]{2}$') ); CREATE UNIQUE INDEX "business_accounts_organization_id_code_key" ON "business_accounts"("organization_id", "code"); CREATE UNIQUE INDEX "business_accounts_organization_id_country_code_registration_key" ON "business_accounts"("organization_id", "country_code", "registration_number"); CREATE UNIQUE INDEX "business_accounts_id_organization_id_key" ON "business_accounts"("id", "organization_id"); CREATE INDEX "business_accounts_organization_id_created_at_id_idx" ON "business_accounts"("organization_id", "created_at", "id"); ALTER TABLE "business_accounts" ADD CONSTRAINT "business_accounts_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "business_accounts" ADD CONSTRAINT "business_accounts_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 "business_verification_events" ( "id" UUID NOT NULL PRIMARY KEY, "organization_id" UUID NOT NULL, "account_id" UUID NOT NULL, "actor_id" UUID NOT NULL, "sequence" INTEGER NOT NULL, "kind" "BusinessVerificationKind" NOT NULL, "profile_snapshot" JSONB, "note" VARCHAR(500), "idempotency_key" UUID NOT NULL, "request_hash" CHAR(64) NOT NULL, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "business_verification_events_sequence" CHECK ("sequence" > 0), CONSTRAINT "business_verification_events_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$'), CONSTRAINT "business_verification_events_shape" CHECK ( ("kind" = 'SUBMITTED' AND "profile_snapshot" IS NOT NULL AND "note" IS NULL) OR ("kind" IN ('APPROVED', 'REJECTED') AND "profile_snapshot" IS NULL AND char_length(btrim("note")) BETWEEN 3 AND 500) ) ); CREATE UNIQUE INDEX "business_verification_events_account_id_sequence_key" ON "business_verification_events"("account_id", "sequence"); CREATE UNIQUE INDEX "business_verification_events_organization_id_idempotency_ke_key" ON "business_verification_events"("organization_id", "idempotency_key"); CREATE INDEX "business_verification_events_organization_id_account_id_seq_idx" ON "business_verification_events"("organization_id", "account_id", "sequence"); ALTER TABLE "business_verification_events" ADD CONSTRAINT "business_verification_events_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE "business_verification_events" ADD CONSTRAINT "business_verification_events_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 "business_verification_events" ADD CONSTRAINT "business_verification_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_business_account() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE latest "BusinessVerificationKind"; BEGIN IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'Business accounts cannot be deleted'; END IF; IF (NEW.id, NEW.organization_id, NEW.created_by_id, NEW.code, NEW.created_at) IS DISTINCT FROM (OLD.id, OLD.organization_id, OLD.created_by_id, OLD.code, OLD.created_at) THEN RAISE EXCEPTION 'Business account identity is immutable'; END IF; SELECT kind INTO latest FROM business_verification_events WHERE account_id = OLD.id ORDER BY sequence DESC LIMIT 1; IF latest IN ('SUBMITTED', 'APPROVED') THEN RAISE EXCEPTION 'Submitted or approved business profiles cannot be edited'; END IF; IF NEW.profile_version <> OLD.profile_version + 1 THEN RAISE EXCEPTION 'Business profile version must advance by one'; END IF; RETURN NEW; END; $$; CREATE TRIGGER business_accounts_guard BEFORE UPDATE OR DELETE ON business_accounts FOR EACH ROW EXECUTE FUNCTION guard_business_account(); CREATE FUNCTION guard_business_verification_event() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE previous business_verification_events%ROWTYPE; DECLARE account business_accounts%ROWTYPE; BEGIN IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Business verification history is immutable'; END IF; SELECT * INTO account FROM business_accounts WHERE id = NEW.account_id AND organization_id = NEW.organization_id; SELECT * INTO previous FROM business_verification_events WHERE account_id = NEW.account_id ORDER BY sequence DESC LIMIT 1; IF NEW.sequence <> COALESCE(previous.sequence, 0) + 1 THEN RAISE EXCEPTION 'Business verification sequence invalid'; END IF; IF NEW.kind = 'SUBMITTED' THEN IF previous.kind IS NOT NULL AND previous.kind <> 'REJECTED' THEN RAISE EXCEPTION 'Business verification submission state invalid'; END IF; IF NEW.profile_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 'Business verification snapshot does not match current profile'; END IF; ELSIF previous.kind IS DISTINCT FROM 'SUBMITTED' OR previous.actor_id = NEW.actor_id THEN RAISE EXCEPTION 'Business verification decision state or reviewer invalid'; END IF; RETURN NEW; END; $$; CREATE TRIGGER business_verification_events_guard BEFORE INSERT OR UPDATE OR DELETE ON business_verification_events FOR EACH ROW EXECUTE FUNCTION guard_business_verification_event(); UPDATE "roles" SET "permissions" = ARRAY(SELECT DISTINCT unnest("permissions" || ARRAY['b2b.read', 'b2b.manage', 'b2b.verify'])) WHERE "is_system" = true AND "name" = 'Owner';