fix(reminders): lifecycle-aware jobs and dispatch skip guard

The reminder pipeline fired regardless of what happened to the booking
after creation: cancelled customers got reminded about bookings that
weren't happening, reschedules kept the old reminder time, and
waitlisted bookings armed jobs too. Pending jobs are now cancelled on
both cancel paths, deleted and re-armed on reschedule, and dispatch
joins bookings.status to skip anything not confirmed. Reschedule also
gained the same availability-window and blocked-day validation as
booking creation, closing a 3am-reschedule hole via the manage URL.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Tomas DvorakandDevin committed 2026-09-18 12:47:07 +02:00
1 parent 0dc26e0c16
commit cdad7875e1
12 files changed
+272 -9

No files matched your search

@@ -168,10 +168,35 @@ func (s *CustomerService) RescheduleBooking(ctx context.Context, reference strin
}
}
// Appointments must land inside bookable hours and off blocked days;
// class bookings keep their session-defined window.
if booking.ClassSessionID == nil {
tenant, err := s.repo.GetTenantByID(ctx, booking.TenantID)
if err != nil {
return err
}
if err := validateAppointmentTarget(ctx, s.repo, tenant, booking.StaffID, booking.LocationID, newStartsAt, newEndsAt); err != nil {
return ErrInvalidReschedule
}
}
if err := s.repo.RescheduleBooking(ctx, booking.ID, newStartsAt, newEndsAt); err != nil {
return err
}
// Re-arm the reminder for the new time; a booking moved inside the
// no-reminder horizon simply gets none.
if err := s.repo.DeletePendingReminderJobsForBooking(ctx, booking.ID); err == nil {
if scheduledFor, ok := reminderSchedule(newStartsAt); ok {
_ = s.repo.CreateReminderJob(ctx, db.ReminderJobParams{
TenantID: booking.TenantID,
BookingID: booking.ID,
Channel: "email",
ScheduledFor: scheduledFor,
})
}
}
// Send reschedule email (async)
go s.sendRescheduleEmail(booking, newStartsAt, newEndsAt)
@@ -195,6 +220,7 @@ func (s *CustomerService) CancelBooking(ctx context.Context, reference string, t
if err := s.repo.UpdateBookingStatus(ctx, booking.ID, "cancelled"); err != nil {
return err
}
_ = s.repo.CancelPendingReminderJobsForBooking(ctx, booking.ID)
// Send cancellation email (async)
go s.sendCancellationEmail(booking)
+6 -6
View File
@@ -290,7 +290,7 @@ func (s *Service) Create(ctx context.Context, request domain.CreateBookingReques
if !startsAt.Add(expectedDuration).Equal(endsAt) {
return domain.CreateBookingResponse{}, fmt.Errorf("%w: appointment duration must match service duration", ErrInvalidBooking)
}
if err := s.validateAppointmentTarget(ctx, tenant, request.StaffID, request.LocationID, startsAt, endsAt); err != nil {
if err := validateAppointmentTarget(ctx, s.repo, tenant, request.StaffID, request.LocationID, startsAt, endsAt); err != nil {
return domain.CreateBookingResponse{}, err
}
for _, booking := range existing {
@@ -383,9 +383,9 @@ func (s *Service) Create(ctx context.Context, request domain.CreateBookingReques
// resources owned by the tenant and lands inside bookable time: the staff and
// location must belong to the tenant, the window must fit an availability rule
// the booking could claim, and it must not overlap a blocked day.
func (s *Service) validateAppointmentTarget(ctx context.Context, tenant db.TenantRecord, staffID, locationID *string, startsAt, endsAt time.Time) error {
func validateAppointmentTarget(ctx context.Context, repo db.Repository, tenant db.TenantRecord, staffID, locationID *string, startsAt, endsAt time.Time) error {
if staffID != nil {
staff, err := s.repo.GetStaffByID(ctx, *staffID)
staff, err := repo.GetStaffByID(ctx, *staffID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return fmt.Errorf("%w: staffId is not available for tenant", ErrInvalidBooking)
@@ -397,7 +397,7 @@ func (s *Service) validateAppointmentTarget(ctx context.Context, tenant db.Tenan
}
}
if locationID != nil {
location, err := s.repo.GetLocationByID(ctx, *locationID)
location, err := repo.GetLocationByID(ctx, *locationID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return fmt.Errorf("%w: locationId is not available for tenant", ErrInvalidBooking)
@@ -409,11 +409,11 @@ func (s *Service) validateAppointmentTarget(ctx context.Context, tenant db.Tenan
}
}
rules, err := s.repo.ListAvailabilityRulesByTenant(ctx, tenant.ID)
rules, err := repo.ListAvailabilityRulesByTenant(ctx, tenant.ID)
if err != nil {
return err
}
blockedDays, err := s.repo.ListBlockedDaysByTenant(ctx, tenant.ID, startsAt.UTC(), endsAt.UTC())
blockedDays, err := repo.ListBlockedDaysByTenant(ctx, tenant.ID, startsAt.UTC(), endsAt.UTC())
if err != nil {
return err
}
@@ -518,3 +518,125 @@ func TestDashboardSummaryComputesKPIsWithTrends(t *testing.T) {
t.Fatalf("utilization kpi = %+v", utilization)
}
}
func bookAppointmentSlot(t *testing.T, repo db.Repository, service *Service) domain.CreateBookingResponse {
t.Helper()
// Target a day ~72h out so the booking lands in the -24h reminder horizon.
target := time.Now().UTC().AddDate(0, 0, 3).Format("2006-01-02")
availability, err := service.Availability(context.Background(), "studio-atelier", AvailabilityFilter{Date: target})
if err != nil {
t.Fatalf("availability: %v", err)
}
var slot domain.TimeSlot
for _, s := range availability.Slots {
if s.Mode == "appointment" {
slot = s
break
}
}
if slot.StartsAt == "" {
t.Fatal("expected appointment slot")
}
resp, err := service.Create(context.Background(), domain.CreateBookingRequest{
TenantSlug: "studio-atelier",
BookingMode: "appointment",
ServiceID: slot.ServiceID,
StaffID: slot.StaffID,
LocationID: slot.LocationID,
CustomerName: "Reminder Test",
CustomerEmail: "[email protected]",
StartsAt: slot.StartsAt,
EndsAt: slot.EndsAt,
})
if err != nil {
t.Fatalf("create booking: %v", err)
}
return resp
}
func pendingJobsFor(t *testing.T, repo db.Repository, bookingID string) []db.ReminderJobRecord {
t.Helper()
jobs, err := repo.ListDueReminderJobs(context.Background(), time.Now().UTC().AddDate(1, 0, 0), 100)
if err != nil {
t.Fatalf("list due jobs: %v", err)
}
var out []db.ReminderJobRecord
for _, job := range jobs {
if job.BookingID == bookingID {
out = append(out, job)
}
}
return out
}
func TestCancelBookingClearsPendingReminder(t *testing.T) {
repo := db.NewMemoryRepository()
service := NewService(repo, nil, "http://localhost:3000")
customerService := NewCustomerService(repo, nil, "http://localhost:3000")
ctx := context.Background()
resp := bookAppointmentSlot(t, repo, service)
if len(pendingJobsFor(t, repo, resp.BookingID)) != 1 {
t.Fatal("expected one pending reminder job after booking")
}
manageURL, _ := url.Parse(resp.ManageURL)
token := manageURL.Query().Get("token")
if err := customerService.CancelBooking(ctx, resp.Reference, token); err != nil {
t.Fatalf("cancel: %v", err)
}
if got := pendingJobsFor(t, repo, resp.BookingID); len(got) != 0 {
t.Fatalf("expected no pending jobs after cancel, got %d", len(got))
}
}
func TestRescheduleRearmsReminder(t *testing.T) {
repo := db.NewMemoryRepository()
service := NewService(repo, nil, "http://localhost:3000")
customerService := NewCustomerService(repo, nil, "http://localhost:3000")
ctx := context.Background()
resp := bookAppointmentSlot(t, repo, service)
booking, _ := repo.GetBookingByID(ctx, resp.BookingID)
newStartsAt := booking.StartsAt.Add(48 * time.Hour)
newEndsAt := booking.EndsAt.Add(48 * time.Hour)
manageURL, _ := url.Parse(resp.ManageURL)
token := manageURL.Query().Get("token")
if err := customerService.RescheduleBooking(ctx, resp.Reference, domain.RescheduleBookingRequest{
NewStartsAt: newStartsAt.UTC().Format(time.RFC3339),
NewEndsAt: newEndsAt.UTC().Format(time.RFC3339),
}, token); err != nil {
t.Fatalf("reschedule: %v", err)
}
jobs := pendingJobsFor(t, repo, resp.BookingID)
if len(jobs) != 1 {
t.Fatalf("expected one pending job after reschedule, got %d", len(jobs))
}
expected := newStartsAt.Add(-24 * time.Hour)
if !jobs[0].ScheduledFor.Equal(expected) {
t.Fatalf("expected reminder at %v, got %v", expected, jobs[0].ScheduledFor)
}
}
func TestRescheduleRejectsTimeOutsideAvailabilityWindow(t *testing.T) {
repo := db.NewMemoryRepository()
service := NewService(repo, nil, "http://localhost:3000")
customerService := NewCustomerService(repo, nil, "http://localhost:3000")
ctx := context.Background()
resp := bookAppointmentSlot(t, repo, service)
manageURL, _ := url.Parse(resp.ManageURL)
token := manageURL.Query().Get("token")
// 03:00 UTC next week is outside every availability rule.
night := time.Now().UTC().AddDate(0, 0, 7).Truncate(24 * time.Hour).Add(3 * time.Hour)
err := customerService.RescheduleBooking(ctx, resp.Reference, domain.RescheduleBookingRequest{
NewStartsAt: night.UTC().Format(time.RFC3339),
NewEndsAt: night.Add(time.Hour).UTC().Format(time.RFC3339),
}, token)
if !errors.Is(err, ErrInvalidReschedule) {
t.Fatalf("expected ErrInvalidReschedule, got %v", err)
}
}