Domain message flow
Synced from returns-processing
This page is a copy of docs/docs/ddd/domain-message-flow.md on develop, derived from that repository's code. Edit it there, then re-sync.
One page, end to end: how another context's fact becomes an authorized return, a graded line and an immutable decision. Every box below is a real package in this repo.
The flow in words:
- A fact lands on one of the three consumed topics; the consumer under
its pinned group id decodes the CloudEvents envelope
(
internal/adapters/kafka/cloudevents) and dispatches on the FULL type; unknown types are ignored; a non-CloudEvent message is WARN + skip. - Id claim first. The CloudEvents
idis claimed in the SAME Postgres transaction as the copy write (processed_events); a replayed id is a no-op that still commits the Kafka offset (FetchMessage+CommitMessages— offset only after a durable effect). - The copies answer the decision.
AuthorizeReturnreads only the local copies: the allocations copy supplies the authoritative SKU, the manifested copy proves the line shipped, the current policy supplies the window and the cap. An empty or stale copy rejects (LINE_NOT_MANIFESTED), never a guess and never a live call (ADR 0003). - A refusal persists nothing. A request that fails an invariant returns
the typed domain error (
ErrLineNotManifested,ErrWindowExpired,ErrOpenRMAExists,ErrQuantityExceedsPolicy) mapped to the rejection code — no RMA row, no event. The domain'sRejectpath and the cataloguedReturnAuthorizationRejectedexist for a future async decision path (ADR 0002 keepsREQUESTEDfor it). - Arrival and grading.
OpenReturnReceiptrecords every line as MATCHED or explicitly UNEXPECTED (oneUnexpectedReturnReceivedper unexpected line, which exception-management opens a case on).InspectReturnLinegrades a line exactly once and decides its disposition in the same unit of work: the pureDecide(grade, classification, policy)plus the RESTOCK guardrails. - Everything leaves through the outbox (ADR 0005):
the relay drains due rows, encodes to CloudEvents, deletes on publish,
backs off exponentially, caps attempts (
OUTBOX_MAX_ATTEMPTS) and dead-letters deterministic poison. Each drained row produces TWO messages — the integration event and its analytics twin (sameid, only the dataschema differs, ADR 0007). - The analytics twin feeds the read side:
cmd/returns-projectorfolds five of the seven event types into fact tables;cmd/returns-reportsanswers disposition-mix, value-recovery and return-cycle-time over them.