diff --git a/src/lib/domain.ts b/src/lib/domain.ts index 535e9f5..2a444b1 100644 --- a/src/lib/domain.ts +++ b/src/lib/domain.ts @@ -80,11 +80,15 @@ export type DecisionInput = { readonly idempotencyKey: string; }; -/** Kinds whose whole point is a specific parcel. */ +/** + * Kinds whose whole point is a specific parcel. A proof of receipt is a + * per-parcel claim, so a missing target is a typed rejection — never an event + * that bumps the case revision while touching nothing. + */ const PARCEL_SCOPED_KINDS: Readonly> = { "courier.progress": true, "customer.dispute": true, - "support.proof": false, + "support.proof": true, "support.dispute_cleared": true, }; @@ -378,7 +382,10 @@ export function ingestEvent(handle: DatabaseSync, envelope: EventEnvelope): Inge const caseRow = getCase(handle, envelope.caseId); if (caseRow === null) return { error: "case_not_found", caseId: envelope.caseId }; - if (PARCEL_SCOPED_KINDS[envelope.kind] && envelope.parcelId === null) { + // Normalise once: unvalidated domain callers can pass `undefined`, and that + // must be the same typed rejection as an explicit `null`, not a bind error. + const parcelTarget = envelope.parcelId ?? null; + if (PARCEL_SCOPED_KINDS[envelope.kind] && parcelTarget === null) { return { error: "parcel_required", kind: envelope.kind }; } if (envelope.kind === "courier.progress" && envelope.providerRevision === null) { @@ -386,10 +393,10 @@ export function ingestEvent(handle: DatabaseSync, envelope: EventEnvelope): Inge } let parcel: ParcelRow | null = null; - if (envelope.parcelId !== null) { - parcel = getParcel(handle, envelope.parcelId); + if (parcelTarget !== null) { + parcel = getParcel(handle, parcelTarget); if (parcel === null || parcel.case_id !== envelope.caseId) { - return { error: "parcel_not_found", parcelId: envelope.parcelId }; + return { error: "parcel_not_found", parcelId: parcelTarget }; } }