115 lines
7.4 KiB
PL/PgSQL
115 lines
7.4 KiB
PL/PgSQL
CREATE TYPE "B2bCreditStatus" AS ENUM ('PROPOSED', 'ACTIVE', 'REJECTED', 'SUSPENDED');
|
|
CREATE TYPE "B2bCreditEventKind" AS ENUM ('APPROVED', 'REJECTED', 'SUSPENDED');
|
|
|
|
CREATE TABLE "b2b_credit_policies" (
|
|
"id" UUID NOT NULL PRIMARY KEY,
|
|
"organization_id" UUID NOT NULL,
|
|
"account_id" UUID NOT NULL,
|
|
"proposed_by_id" UUID NOT NULL,
|
|
"currency" CHAR(3) NOT NULL,
|
|
"credit_limit" DECIMAL(16,2) NOT NULL,
|
|
"net_days" INTEGER NOT NULL,
|
|
"rationale" VARCHAR(500) NOT NULL,
|
|
"status" "B2bCreditStatus" NOT NULL DEFAULT 'PROPOSED',
|
|
"active_slot" BOOLEAN,
|
|
"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_credit_policies_terms" CHECK (
|
|
"currency" IN ('INR', 'USD', 'EUR', 'GBP') AND "credit_limit" > 0
|
|
AND "net_days" BETWEEN 1 AND 180 AND char_length(btrim("rationale")) BETWEEN 3 AND 500
|
|
),
|
|
CONSTRAINT "b2b_credit_policies_slot" CHECK (
|
|
("status" = 'ACTIVE' AND "active_slot" IS TRUE)
|
|
OR ("status" <> 'ACTIVE' AND "active_slot" IS NULL)
|
|
),
|
|
CONSTRAINT "b2b_credit_policies_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$')
|
|
);
|
|
CREATE UNIQUE INDEX "b2b_credit_policies_id_organization_id_key" ON "b2b_credit_policies"("id", "organization_id");
|
|
CREATE UNIQUE INDEX "b2b_credit_policies_organization_id_idempotency_key_key" ON "b2b_credit_policies"("organization_id", "idempotency_key");
|
|
CREATE UNIQUE INDEX "b2b_credit_policies_organization_id_account_id_currency_act_key" ON "b2b_credit_policies"("organization_id", "account_id", "currency", "active_slot");
|
|
CREATE INDEX "b2b_credit_policies_organization_id_account_id_created_at_i_idx" ON "b2b_credit_policies"("organization_id", "account_id", "created_at", "id");
|
|
ALTER TABLE "b2b_credit_policies" ADD CONSTRAINT "b2b_credit_policies_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "b2b_credit_policies" ADD CONSTRAINT "b2b_credit_policies_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_credit_policies" ADD CONSTRAINT "b2b_credit_policies_proposed_by_id_organization_id_fkey" FOREIGN KEY ("proposed_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
CREATE TABLE "b2b_credit_events" (
|
|
"id" UUID NOT NULL PRIMARY KEY,
|
|
"organization_id" UUID NOT NULL,
|
|
"policy_id" UUID NOT NULL,
|
|
"actor_id" UUID NOT NULL,
|
|
"sequence" INTEGER NOT NULL,
|
|
"kind" "B2bCreditEventKind" 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_credit_events_sequence" CHECK ("sequence" BETWEEN 1 AND 2),
|
|
CONSTRAINT "b2b_credit_events_note" CHECK (char_length(btrim("note")) BETWEEN 3 AND 500),
|
|
CONSTRAINT "b2b_credit_events_hash" CHECK ("request_hash" ~ '^[a-f0-9]{64}$')
|
|
);
|
|
CREATE UNIQUE INDEX "b2b_credit_events_policy_id_sequence_key" ON "b2b_credit_events"("policy_id", "sequence");
|
|
CREATE UNIQUE INDEX "b2b_credit_events_organization_id_idempotency_key_key" ON "b2b_credit_events"("organization_id", "idempotency_key");
|
|
CREATE INDEX "b2b_credit_events_organization_id_policy_id_sequence_idx" ON "b2b_credit_events"("organization_id", "policy_id", "sequence");
|
|
ALTER TABLE "b2b_credit_events" ADD CONSTRAINT "b2b_credit_events_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "b2b_credit_events" ADD CONSTRAINT "b2b_credit_events_policy_id_organization_id_fkey" FOREIGN KEY ("policy_id", "organization_id") REFERENCES "b2b_credit_policies"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
ALTER TABLE "b2b_credit_events" ADD CONSTRAINT "b2b_credit_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_credit_policy() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
DECLARE latest "B2bCreditEventKind";
|
|
BEGIN
|
|
IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'Credit policy proposals cannot be deleted'; END IF;
|
|
IF TG_OP = 'INSERT' THEN
|
|
IF NEW.status <> 'PROPOSED' OR NEW.active_slot IS NOT NULL THEN RAISE EXCEPTION 'Credit policies must start proposed'; END IF;
|
|
RETURN NEW;
|
|
END IF;
|
|
IF (NEW.id, NEW.organization_id, NEW.account_id, NEW.proposed_by_id, NEW.currency,
|
|
NEW.credit_limit, NEW.net_days, NEW.rationale, NEW.idempotency_key, NEW.request_hash, NEW.created_at)
|
|
IS DISTINCT FROM (OLD.id, OLD.organization_id, OLD.account_id, OLD.proposed_by_id, OLD.currency,
|
|
OLD.credit_limit, OLD.net_days, OLD.rationale, OLD.idempotency_key, OLD.request_hash, OLD.created_at)
|
|
THEN RAISE EXCEPTION 'Credit policy proposal terms are immutable'; END IF;
|
|
SELECT kind INTO latest FROM b2b_credit_events WHERE policy_id = NEW.id ORDER BY sequence DESC LIMIT 1;
|
|
IF NOT ((OLD.status = 'PROPOSED' AND NEW.status = 'ACTIVE' AND latest = 'APPROVED')
|
|
OR (OLD.status = 'PROPOSED' AND NEW.status = 'REJECTED' AND latest = 'REJECTED')
|
|
OR (OLD.status = 'ACTIVE' AND NEW.status = 'SUSPENDED' AND latest = 'SUSPENDED'))
|
|
THEN RAISE EXCEPTION 'Credit policy transition lacks matching decision event'; END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE TRIGGER b2b_credit_policies_guard BEFORE INSERT OR UPDATE OR DELETE ON b2b_credit_policies FOR EACH ROW EXECUTE FUNCTION guard_b2b_credit_policy();
|
|
|
|
CREATE FUNCTION guard_b2b_credit_event() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
DECLARE policy b2b_credit_policies%ROWTYPE;
|
|
BEGIN
|
|
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Credit policy decisions are immutable'; END IF;
|
|
SELECT * INTO policy FROM b2b_credit_policies WHERE id = NEW.policy_id AND organization_id = NEW.organization_id;
|
|
IF NEW.kind IN ('APPROVED', 'REJECTED') THEN
|
|
IF NEW.sequence <> 1 OR policy.status <> 'PROPOSED' OR NEW.actor_id = policy.proposed_by_id
|
|
THEN RAISE EXCEPTION 'Credit decision requires an independent reviewer and proposed policy'; END IF;
|
|
ELSIF NEW.kind = 'SUSPENDED' THEN
|
|
IF NEW.sequence <> 2 OR policy.status <> 'ACTIVE'
|
|
THEN RAISE EXCEPTION 'Credit suspension requires an active policy'; END IF;
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE TRIGGER b2b_credit_events_guard BEFORE INSERT OR UPDATE OR DELETE ON b2b_credit_events FOR EACH ROW EXECUTE FUNCTION guard_b2b_credit_event();
|
|
|
|
CREATE FUNCTION check_b2b_credit_event_status() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
DECLARE current_status "B2bCreditStatus";
|
|
BEGIN
|
|
SELECT status INTO current_status FROM b2b_credit_policies WHERE id = NEW.policy_id;
|
|
IF (NEW.kind = 'APPROVED' AND current_status <> 'ACTIVE')
|
|
OR (NEW.kind = 'REJECTED' AND current_status <> 'REJECTED')
|
|
OR (NEW.kind = 'SUSPENDED' AND current_status <> 'SUSPENDED')
|
|
THEN RAISE EXCEPTION 'Credit decision must update policy status in the same transaction'; END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
CREATE CONSTRAINT TRIGGER b2b_credit_events_status_guard AFTER INSERT ON b2b_credit_events
|
|
DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION check_b2b_credit_event_status();
|
|
|
|
UPDATE "roles" SET "permissions" = ARRAY(SELECT DISTINCT unnest("permissions" || ARRAY['b2b.credit.read', 'b2b.credit.propose', 'b2b.credit.review']))
|
|
WHERE "is_system" = true AND "name" = 'Owner';
|