38 — TestimonialRequestEvent Model & Delivery Event Stream (Blueprint §5.6)
Cursor-ready plan for request-event logging, provider event ingestion, timeline semantics, deduplication, and Requests Log integration.
38 — TestimonialRequestEvent Model & Delivery Event Stream (Blueprint §5.6)
Section titled “38 — TestimonialRequestEvent Model & Delivery Event Stream (Blueprint §5.6)”Source: 02-Implementation-Blueprint.md — §5.6 New model: TestimonialRequestEvent.
This document is a build spec only. No code changes are implied until a task references this file.
Related: 07 (delivery pipeline), 13 (requests log screen), 32 (operational workflow), 34 (daily analytics rollups), 20 (schema).
0) Goal (one sentence)
Section titled “0) Goal (one sentence)”Implement TestimonialRequestEvent as a reliable, append-only timeline of messaging events so delivery observability, troubleshooting, and analytics are accurate and auditable.
1) Blueprint model recap
Section titled “1) Blueprint model recap”Blueprint §5.6 fields:
id String @id @default(uuid())shopId String @map("shop_id")requestId String @map("request_id")eventType String @map("event_type")// queued | sent | delivered | opened | clicked | failedeventAt DateTime @default(now()) @map("event_at")providerMessageId String? @map("provider_message_id")payload Json?
Index:
@@index([requestId, eventAt])
2) Scope boundaries
Section titled “2) Scope boundaries”In scope
Section titled “In scope”- event model + migration
- event write helper contract
- provider webhook event ingestion mapping
- event dedupe rules
- requests log timeline usage
Out of scope
Section titled “Out of scope”- cross-provider normalization for every vendor-specific edge case
- external event bus integration
3) Event taxonomy (canonical)
Section titled “3) Event taxonomy (canonical)”Base events from blueprint:
queuedsentdeliveredopenedclickedfailed
Optional internal events (if needed):
scheduled(if you want pre-queue observability)submitted(mirrors request status transition after form submit)retrying
Keep a central enum/constants module for event types used by sender, webhook callbacks, and UI.
4) Event write contract
Section titled “4) Event write contract”Create a shared helper:
appendRequestEvent({ shopId, requestId, eventType, eventAt?, providerMessageId?, payload? })
Rules:
- request must belong to
shopId. - event rows are append-only.
- if
eventAtomitted, default now. - payload should be compact and sanitized (no secrets).
Use this helper everywhere to avoid inconsistent event writes.
5) Event sources
Section titled “5) Event sources”5.1 Internal system-generated events
Section titled “5.1 Internal system-generated events”queuedwhen worker picks due requestsentwhen provider accepts sendfailedwhen provider call errorsclickedwhen token page is opened (if using event stream in addition toclickedAt)
5.2 Provider callback events
Section titled “5.2 Provider callback events”Webhook/callback handlers map provider statuses into canonical events:
- provider delivered ->
delivered - provider open ->
opened - provider click ->
clicked(if tracked at provider level) - provider bounce/reject ->
failed
Store original provider payload in payload with redaction as needed.
6) Deduplication and idempotency
Section titled “6) Deduplication and idempotency”Provider callbacks can replay. Add dedupe logic:
6.1 Preferred approach
Section titled “6.1 Preferred approach”Before insert, check recent duplicate by:
requestIdeventTypeproviderMessageId(when present)- near-identical
eventAtwindow
6.2 Optional hard guarantee
Section titled “6.2 Optional hard guarantee”Add unique constraint on a synthetic hash column (future) if duplicate volume becomes a problem.
6.3 Replay-safe behavior
Section titled “6.3 Replay-safe behavior”Duplicate event should be no-op and still return success response to provider.
7) Timeline semantics
Section titled “7) Timeline semantics”Requests log (13) and support tooling should treat events as timeline, not single status source.
7.1 Latest event
Section titled “7.1 Latest event”- last event by
eventAtcan be used for “Last event timestamp”.
7.2 Status reconciliation
Section titled “7.2 Status reconciliation”TestimonialRequest.statusremains primary operational status.- event stream provides detailed audit and provider-level context.
8) Payload policy
Section titled “8) Payload policy”payload Json guidelines:
- include provider response identifiers and normalized metadata
- exclude secrets/API keys
- truncate oversized payloads if needed
- redact personal data not needed for debugging
Keep payload size manageable to avoid DB bloat.
9) Query patterns
Section titled “9) Query patterns”9.1 Request detail timeline
Section titled “9.1 Request detail timeline”where: { requestId, shopId } ordered eventAt desc.
9.2 Rollup/analytics
Section titled “9.2 Rollup/analytics”Use grouped counts by eventType + day (UTC), or rely on TestimonialDailyAnalytics rollup jobs.
9.3 Operational debugging
Section titled “9.3 Operational debugging”Filter by:
eventType=failed- date range
- campaign/request ids
10) Integration points
Section titled “10) Integration points”07sender worker writes queued/sent/failed.06token page can write clicked event.- provider callback route writes delivered/opened/clicked.
13requests log displays last event + event timeline.34rollup job consumes events (optional for clicked/opened counts).
11) Acceptance criteria
Section titled “11) Acceptance criteria”- Every send attempt writes at least one event row.
- Provider callback events map to canonical event types.
- Duplicate callback replay does not create duplicate timeline entries.
- Requests log can show accurate last-event timestamp.
- Event payloads are present but sanitized.
12) Suggested implementation order (for Cursor)
Section titled “12) Suggested implementation order (for Cursor)”- Add Prisma model and migration for
TestimonialRequestEvent. - Implement shared append helper.
- Integrate helper into sender worker.
- Implement provider callback mapping and dedupe.
- Add timeline view in requests log.
- Add event-based analytics rollup hooks if needed.
13) References
Section titled “13) References”02-Implementation-Blueprint.md— §5.607-email-sms-request-delivery-pipeline.md13-requests-log-blueprint-screen-10.md32-email-sms-operational-detail-blueprint-section-4.md34-testimonial-daily-analytics-model-blueprint-section-5-11.md
14) Note on numbering
Section titled “14) Note on numbering”This folder already includes 05 through 37 plans. This file is 38-....