Skip to content

feat(webhooks): booking.reminder event fired with each reminder (#259) - #279

Open
dominik-myszkowski wants to merge 1 commit into
Calnode:mainfrom
dominik-myszkowski:feat/booking-reminder-webhook
Open

dominik-myszkowski wants to merge 1 commit into
Calnode:mainfrom
dominik-myszkowski:feat/booking-reminder-webhook

Conversation

@dominik-myszkowski

Copy link
Copy Markdown

Implements option B from #259: a booking.reminder webhook event, so an external SMS/push sender can stay stateless while Calnode keeps owning scheduling, reschedules and cancellations.

What it does

  • booking.reminder is a new subscribable event (backend allowlist and the event list on the webhooks page). It fires from the existing reminder.send job, once per configured hours_before, with the usual booking payload.
  • It is enqueued before the reminder email, so a failed email that makes the job retry is covered by the delivery key below.
  • No migration and no schema change.

Your three conditions

  1. hours_before in the payload. It is part of the event rather than a selectable booking field, so booking.reminder always carries it in data whatever the webhook's field selection, and no other event does.
  2. Same gate as the reminder email. It runs after the same checks: the booking is still confirmed and the host's notify_reminder is on. If reminder emails are off, no event is sent.
  3. Deterministic delivery key. New webhook.Service.EnqueueOnce(ctx, event, payload, key). The delivery ID is derived from (webhook ID, key) as a UUID-shaped SHA-256 value, and is inserted with INSERT OR IGNORE. A second call with the same key creates neither a delivery nor a webhook.deliver job, even after the first delivery has finished, which is where the jobs table's live-row (type, payload) guard no longer helps. The key is (booking, hours_before, start_at): a retried job collapses, while a reschedule gets its own event for the new time. Enqueue itself is unchanged for every other event. A side benefit is that X-Calnode-Delivery is stable, so receivers can dedupe on it too.

ARCHITECTURE §13 no longer says the event deliberately does not exist, and describes the above instead.

Tests

  • webhook:
    • the same key collapses, including after the delivery succeeded and its job is done;
    • a different key or webhook gets its own delivery, and IDs are UUID-shaped;
    • an empty key is rejected;
    • hours_before is sent even when the webhook selected only id, and is absent on booking.created.
  • worker:
    • the event fires with the expected data next to the email;
    • nothing is sent with notify_reminder = 0 or for a cancelled booking;
    • retry after a failed email gives one delivery and one webhook.deliver job;
    • a second hours_before, a duplicate job for the same time, and a reschedule give exactly three deliveries.
  • handler: a webhook subscribing to booking.reminder is accepted.

Mutation check: with a random instead of a deterministic key, the two dedup tests fail.

gofmt, go vet ./..., go build ./..., go test ./..., go run ./cmd/i18n-coverage and svelte-check pass locally.

I also checked it on a running binary, merged locally with #278 to reach a loopback receiver (the two merge cleanly). The setup was a booking two days out with reminders [72, 24].

  • The 72h reminder fired immediately with "hours_before": 72, and the 24h one stayed pending.
  • Rescheduling delivered a second event with the new start_at and a different delivery ID.
  • After cancelling, a manually re-queued reminder job delivered nothing.

Fires from the reminder.send job at each configured hours_before, after the
same gates as the reminder email (booking confirmed, host notify_reminder on),
and carries hours_before in data regardless of the webhook's field selection.

Enqueued before the email via EnqueueOnce: the delivery ID is derived from
(webhook, booking, hours_before, start_at) and inserted with INSERT OR IGNORE,
so a job retried after a failed email does not deliver twice, while a
reschedule gets its own event. No schema change.

Refs Calnode#259

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one upgrade-window edge case inline.

Approach: sound — the event is raised from the existing reminder.send job and deduped with a deterministic delivery ID, so there is no schema change and no second scheduling path to keep in sync.

Reviewed changes

  • New event wired through: booking.reminder added to the handler allowlist and the admin webhooks page, so it is subscribable through both the API and the UI.
  • hours_before in the payload: a non-selectable BookingPayload field appended by buildData whenever set, so reminder events carry it even when the webhook narrowed its field selection.
  • EnqueueOnce: deterministic UUID-shaped delivery ID from (webhook, key) inserted with INSERT OR IGNORE, skipping the webhook.deliver job on conflict; Enqueue is untouched for the other events.
  • Worker gate + key: sendReminder passes hours_before through and enqueues the event before the email, gated on confirmed / notify_reminder, with a key of (booking, hours_before, start_at).
  • Coverage and docs: webhook, worker and handler tests plus the ARCHITECTURE §13 rewrite.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash | 𝕏

Comment thread internal/worker/worker.go
Status: status,
LocationValue: d.LocationValue,
CreatedAt: created,
HoursBefore: hoursBefore,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder jobs queued by an older version carry only booking_id, so hoursBefore is 0 here and buildData drops hours_before — the event then arrives without the field this PR promises it always carries (the repo still treats that old-format job as a live state in TestRescheduleBooking_updatesReminderJob). The receiver cannot tell a 24h reminder from a 2h one for those in-flight jobs. Consider treating a non-positive hours_before as unidentifiable: skip the webhook (with a log line) or derive it from the event type's configured reminders, rather than emitting an event without it.

Technical details
# Legacy reminder jobs deliver booking.reminder without hours_before

## Affected sites
- `internal/worker/worker.go:403` — `HoursBefore: hoursBefore` forwards the job payload value verbatim; it is 0 for jobs queued before the field existed.
- `internal/webhook/webhook.go:381` — `if bd.core.HoursBefore > 0` omits the key, so the delivered `data` has no `hours_before`.
- `internal/handler/booking_handler.go:2176` and `:2267` — current writers do include it; only pre-existing pending rows are affected.
- `internal/handler/reschedule_test.go:94` — the suite still seeds an "old-format reminder job (no hours_before field)", confirming that shape is considered reachable.

## Required outcome
- A `booking.reminder` delivery always carries `hours_before`, or the worker does not raise the event when it cannot determine which reminder fired (and logs the skip).

## Suggested approach (optional)
- Guard the `enqueueReminderWebhook` call on `p.HoursBefore > 0`, or look the value up from `event_type_reminders` for the booking's event type when it is zero.

## Open questions for the human
- Is upgrading across the version that introduced `hours_before` in the `reminder.send` payload a supported path with in-flight jobs? If not, a comment recording that constraint is enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant