Course › Day 4 · Hosts and guests › Scene 21 of 49 · 35 min
Arrival message sent at 3 a.m.
Message templates, send_at built in the property’s timezone, DST tests, a scheduler with chunkById(), the review request after check-out
On this page
#supportWednesday 07:10
Guest email, Queenstown chaletYour “see you tomorrow” message with the key box code woke me up at 3:04 in the morning. Is that really necessary?
Mona · supportThe template says “1 day before check-in, at 15:00”. Our first host outside Europe, first complaint.
What hosts asked for
Hosts send the same messages to every guest: a thank-you when the booking is confirmed, arrival instructions the day before, a goodbye after check-out. Automatic messages are the feature hosts pay a PMS for. Each template has a trigger, a number of days before or after it, and a local time to send.
The one hosts care about most is the review request: “How was your stay?” the day after check-out at 10:00, local time. Reviews decide a listing’s rank on Airbnb, so hosts want every guest asked, once, at a decent hour, and never a guest whose stay didn’t happen. It needs nothing special: it is a template with the trigger after_check_out, one day after, at 10:00. This scene gets the time right; scene 22 makes sure it is sent once and is cancelled with the booking.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('message_templates', function (Blueprint $table) {
$table->id();
$table->foreignId('account_id')->constrained()->cascadeOnDelete();
$table->string('name');
$table->string('trigger'); // booking_confirmed, before_check_in, after_check_out
$table->smallInteger('days_offset')->default(0); // -1 = the day before
$table->time('send_time')->nullable(); // local time at the property; null = right away
$table->string('subject');
$table->text('body');
$table->boolean('active')->default(true);
$table->timestamps();
});
Schema::create('scheduled_messages', function (Blueprint $table) {
$table->id();
$table->foreignId('booking_id')->constrained()->cascadeOnDelete();
$table->foreignId('message_template_id')->constrained()->cascadeOnDelete();
$table->timestamp('send_at'); // UTC, like every timestamp
$table->string('status')->default('pending'); // pending, sent, cancelled
$table->timestamp('sent_at')->nullable();
$table->timestamps();
$table->unique(['booking_id', 'message_template_id']); // one of each template per booking (scene 22)
$table->index(['status', 'send_at']); // "what is due?" every minute
});
}
};
<?php
namespace App\Enums;
enum MessageTrigger: string
{
case BookingConfirmed = 'booking_confirmed';
case BeforeCheckIn = 'before_check_in';
case AfterCheckOut = 'after_check_out';
}
<?php
namespace App\Enums;
enum ScheduledMessageStatus: string
{
case Pending = 'pending';
case Sent = 'sent';
case Cancelled = 'cancelled';
}
The two models. A template belongs to an account:
<?php
namespace App\Models;
use App\Enums\MessageTrigger;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class MessageTemplate extends Model
{
protected $fillable = ['name', 'trigger', 'days_offset', 'send_time', 'subject', 'body', 'active'];
protected function casts(): array
{
return ['trigger' => MessageTrigger::class, 'active' => 'boolean'];
}
public function account(): BelongsTo
{
return $this->belongsTo(Account::class);
}
}
A scheduled message belongs to a booking and a template, and knows when it is due:
<?php
namespace App\Models;
use App\Enums\ScheduledMessageStatus;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ScheduledMessage extends Model
{
protected function casts(): array
{
return [
'status' => ScheduledMessageStatus::class,
'send_at' => 'datetime',
'sent_at' => 'datetime',
];
}
public function booking(): BelongsTo
{
return $this->belongsTo(Booking::class);
}
public function template(): BelongsTo
{
return $this->belongsTo(MessageTemplate::class, 'message_template_id');
}
/** Pending, and its time has come: ScheduledMessage::query()->due() */
public function scopeDue(Builder $query): void
{
$query->where('status', ScheduledMessageStatus::Pending)->where('send_at', '<=', now());
}
}
An account has its templates:
36 unchanged lines
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsToMany;use Illuminate\Database\Eloquent\Relations\HasMany;use Illuminate\Database\Eloquent\Relations\HasManyThrough; class Account extends Model{ use HasFactory; protected $fillable = ['name']; public function users(): BelongsToMany { return $this->belongsToMany(User::class, 'memberships') ->using(Membership::class) ->withPivot('role') ->withTimestamps(); } public function properties(): HasMany { return $this->hasMany(Property::class); } public function channelConnections(): HasMany { return $this->hasMany(ChannelConnection::class); } /** Bookings of all this account's properties: bookings → properties.account_id */ public function bookings(): HasManyThrough { return $this->hasManyThrough(Booking::class, Property::class); } public function messageTemplates(): HasMany { return $this->hasMany(MessageTemplate::class); }}
And a booking has its scheduled messages:
57 unchanged lines
<?php namespace App\Models; use App\Bookings\Refunds\NoAutomaticRefund;use App\Bookings\Refunds\RefundPolicy;use App\Bookings\States\BookingState;use App\Bookings\States\CancelledState;use App\Bookings\States\ConfirmedState;use App\Bookings\States\ExpiredState;use App\Bookings\States\PendingState;use App\Bookings\Stay;use App\Casts\MoneyCast;use App\Casts\StayCast;use App\Enums\BookingStatus;use App\Enums\CancellationPolicy;use App\Enums\Channel;use Illuminate\Database\Eloquent\Builder;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsTo;use Illuminate\Database\Eloquent\Relations\HasMany; class Booking extends Model{ use HasFactory; protected $fillable = ['guest_name', 'guest_email', 'check_in', 'check_out', 'guests']; protected function casts(): array { return [ 'check_in' => 'date:Y-m-d', 'check_out' => 'date:Y-m-d', 'status' => BookingStatus::class, 'expires_at' => 'datetime', 'confirmed_at' => 'datetime', 'cancelled_at' => 'datetime', 'total' => MoneyCast::class . ':total_cents', // Money from total_cents + currency 'stay' => StayCast::class, // Stay from check_in + check_out 'cancellation_policy' => CancellationPolicy::class, // a copy of the property's policy, made at booking time 'channel' => Channel::class, // null for bookings made on Nordic Stays ]; } public function property(): BelongsTo { return $this->belongsTo(Property::class); } public function payments(): HasMany { return $this->hasMany(Payment::class); } public function lineItems(): HasMany { return $this->hasMany(BookingLineItem::class); } public function scheduledMessages(): HasMany { return $this->hasMany(ScheduledMessage::class); } /** Bookings that share at least one night with $stay: $property->bookings()->overlapping($stay). */ public function scopeOverlapping(Builder $query, Stay $stay): void {22 unchanged lines
$query->whereIn('status', BookingStatus::blocking()) ->where('check_in', '<', $stay->checkOut->toDateString()) // starts before the new stay ends ->where('check_out', '>', $stay->checkIn->toDateString()); // ends after the new stay starts } /** What this booking may do now. The status column stays the single source of truth. */ public function state(): BookingState { return match ($this->status) { BookingStatus::Pending => new PendingState(), BookingStatus::Confirmed => new ConfirmedState(), BookingStatus::Expired => new ExpiredState(), BookingStatus::Cancelled => new CancelledState(), }; // a new status without a state class fails here, loudly } /** The rules this booking's guest agreed to. Never null: bookings without a policy refund nothing automatically. */ public function refundPolicy(): RefundPolicy { return $this->cancellation_policy?->refundPolicy() ?? new NoAutomaticRefund(); }}
The broken code
$sendAt = $booking->check_in->subDay()->setTimeFromTimeString($template->send_time); // 15:00... in UTC
check_in is a date at midnight UTC (scene 2), so this builds 15:00 UTC. In Copenhagen that’s 17:00 in summer, close enough that nobody noticed. In Queenstown, New Zealand, UTC+12 in July, it is 03:00.
A local time only means something together with a place. "15:00" for this property means 15:00 on the clock in Queenstown, whatever that is in UTC on that particular date, daylight saving included.
How the fix works, in one picture
Before
The send time was built in UTC, which is 3 a.m. in New Zealand.
After
The local time is computed in the property’s timezone, then stored in UTC; DST is tested.
The fix: build the moment in the property’s timezone
The rule is the same as scene 2’s checkInAt(): take the date, add the local time, interpret both in the property’s timezone, and convert the result to UTC for storage. The database never stores local times; the property’s timezone is applied once, here.
<?php
namespace App\Messaging;
use App\Enums\MessageTrigger;
use App\Models\Booking;
use App\Models\MessageTemplate;
use Carbon\CarbonImmutable;
final class SendTime
{
public function for(MessageTemplate $template, Booking $booking): CarbonImmutable
{
if ($template->trigger === MessageTrigger::BookingConfirmed || $template->send_time === null) {
return now();
}
$anchor = $template->trigger === MessageTrigger::BeforeCheckIn ? $booking->stay->checkIn : $booking->stay->checkOut;
$date = $anchor->addDays($template->days_offset)->toDateString(); // a calendar date: no timezone yet
return CarbonImmutable::parse("{$date} {$template->send_time}", $booking->property->timezone) // 15:00 in Queenstown
->utc(); // stored as 03:00 UTC
}
}
One edge: on the night clocks jump forward, a local time like 02:30 doesn’t exist. Carbon moves it forward by the gap (to 03:30), which is what a guest would expect. That’s also why templates should send at daytime hours.
Scheduling when a booking is confirmed
One row per active template. insertOrIgnore() and the unique index mean that confirming twice (scene 9) can’t schedule anything twice.
<?php
namespace App\Messaging;
use App\Models\Booking;
use App\Models\MessageTemplate;
use Illuminate\Support\Facades\DB;
final readonly class ScheduleGuestMessages
{
public function __construct(private SendTime $sendTime)
{
}
public function handle(Booking $booking): void
{
$templates = MessageTemplate::query()
->where('account_id', $booking->property->account_id)
->where('active', true)
->get();
DB::table('scheduled_messages')->insertOrIgnore($templates->map(fn (MessageTemplate $template): array => [
'booking_id' => $booking->id,
'message_template_id' => $template->id,
'send_at' => $this->sendTime->for($template, $booking),
'status' => 'pending',
'created_at' => now(),
'updated_at' => now(),
])->all());
}
}
The confirmation facade (scene 6) calls it right after the state change, inside its transaction:
<?php namespace App\Bookings; use App\Channels\CalendarSync;use App\Enums\PaymentStatus;use App\Messaging\ScheduleGuestMessages;use App\Models\Booking;use App\Models\Payment;use App\Notifications\BookingConfirmed;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Notification; final class BookingConfirmation{ public function __construct(private CalendarSync $calendar) { public function __construct( private CalendarSync $calendar, private ScheduleGuestMessages $scheduleMessages, ) { } public function confirm(Booking $booking, Payment $payment): void8 unchanged lines
{ DB::transaction(function () use ($booking, $payment): void { $booking = Booking::whereKey($booking->id)->lockForUpdate()->firstOrFail(); // fresh, and locked if (! $booking->state()->confirm($booking)) { // step 1: the state decides return; // already confirmed: nothing else to do } $payment->status = PaymentStatus::Succeeded; // step 2 $payment->save(); $this->scheduleMessages->handle($booking); // the guest's messages, at their local times Notification::route('mail', $booking->guest_email) // step 3, after the commit ->notify((new BookingConfirmed($booking))->afterCommit()); $this->calendar->bookingChanged($booking); // step 4: the coordinator decides what follows }); }}
A booking also arrives through a second door: the channel import (scene 15). Without this line an Airbnb guest would never get an arrival message, and nobody would notice until one asked for the address. insertOrIgnore() and the unique key make it safe to call again when the channel sends an update:
<?php namespace App\Channels; use App\Enums\BookingStatus;use App\Messaging\ScheduleGuestMessages;use App\Models\Booking;use App\Models\ChannelListing;use App\Models\Property;use Illuminate\Support\Facades\DB; final class ImportChannelReservation{ public function __construct(private CalendarSync $calendar) { public function __construct( private CalendarSync $calendar, private ScheduleGuestMessages $scheduleMessages, ) { } /** @return bool true when the reservation was applied, false when it was older than what we have */34 unchanged lines
public function handle(ChannelReservation $reservation): bool { $listing = ChannelListing::query() ->where('channel', $reservation->channel) ->where('external_listing_id', $reservation->externalListingId) ->firstOrFail(); return DB::transaction(function () use ($reservation, $listing): bool { Property::whereKey($listing->property_id)->lockForUpdate()->firstOrFail(); // one change per property at a time $booking = Booking::query() ->where('channel', $reservation->channel) ->where('external_id', $reservation->externalId) ->lockForUpdate() ->first() ?? new Booking(); if ($booking->exists && $booking->external_version >= $reservation->version) { return false; // older or the same: already applied something newer } $booking->forceFill([ 'property_id' => $listing->property_id, 'channel' => $reservation->channel, 'external_id' => $reservation->externalId, 'external_version' => $reservation->version, 'status' => $reservation->status, // the channel decides; our own State rules (scene 9) guard our actions 'guest_name' => $reservation->guestName, 'guest_email' => $reservation->guestEmail, 'guests' => $reservation->guests, 'stay' => $reservation->stay, 'total' => $reservation->total, 'confirmed_at' => $booking->confirmed_at ?? now(), 'cancelled_at' => $reservation->status === BookingStatus::Cancelled ? ($booking->cancelled_at ?? now()) : null, ])->save(); $this->calendar->bookingChanged($booking); // clashes are found here, and the channels told if ($booking->status === BookingStatus::Confirmed) { $this->scheduleMessages->handle($booking); // the same messages as a booking made on our site } return true; }); }}
Sending: a scheduler that looks every minute
Every minute, one command finds the messages that are due and queues one job each. chunkById() reads them 500 at a time, so a backlog after an outage doesn’t load ten thousand models at once.
<?php
namespace App\Console\Commands;
use App\Jobs\SendScheduledMessage;
use App\Models\ScheduledMessage;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
class SendDueMessages extends Command
{
protected $signature = 'messages:send-due';
protected $description = 'Queue every scheduled guest message whose time has come';
public function handle(): int
{
ScheduledMessage::query()->due()->chunkById(500, function (Collection $messages): void {
$messages->each(fn (ScheduledMessage $message) => SendScheduledMessage::dispatch($message));
});
return self::SUCCESS;
}
}
13 unchanged lines
<?php use App\Jobs\SyncIcalFeed;use App\Models\IcalFeed;use Illuminate\Support\Facades\Schedule; // The server runs the scheduler once a minute: * * * * * php artisan schedule:run// Locally: php artisan schedule:work Schedule::command('bookings:expire-holds')->everyMinute()->withoutOverlapping();Schedule::command('payments:reconcile-refunds')->dailyAt('03:10')->withoutOverlapping(); Schedule::call(function (): void { IcalFeed::query()->lazyById()->each(fn (IcalFeed $feed) => SyncIcalFeed::dispatch($feed));})->everyFifteenMinutes()->name('sync-ical-feeds')->withoutOverlapping();Schedule::command('channels:poll')->everyTenMinutes()->withoutOverlapping();Schedule::command('messages:send-due')->everyMinute()->withoutOverlapping()->onOneServer();
onOneServer() matters once there are several app servers: the scheduler runs on each of them, and only one may queue the messages.
<?php
namespace App\Jobs;
use App\Enums\ScheduledMessageStatus;
use App\Models\ScheduledMessage;
use App\Notifications\GuestMessage;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Notification;
class SendScheduledMessage implements ShouldQueue
{
use Queueable;
public int $tries = 3;
public function __construct(public ScheduledMessage $message)
{
}
public function handle(): void
{
$booking = $this->message->booking;
Notification::route('mail', $booking->guest_email)->notifyNow(new GuestMessage($this->message));
$this->message->forceFill(['status' => ScheduledMessageStatus::Sent, 'sent_at' => now()])->save();
}
}
The message itself is a mail notification built from the template’s subject and body. Scene 24 fills in its {guest_name} placeholders; scene 25 lets it go by SMS and WhatsApp too.
<?php
namespace App\Notifications;
use App\Models\ScheduledMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class GuestMessage extends Notification
{
use Queueable;
public function __construct(public ScheduledMessage $message)
{
}
/** @return list<string> */
public function via(object $notifiable): array
{
return ['mail'];
}
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage())
->subject($this->message->template->subject)
->markdown('mail.guest', ['body' => $this->message->template->body]); // the host's text, as written
}
}
Its view uses Laravel’s Markdown mail layout:
<x-mail::message>
{!! $body !!}
The Nordic Stays team
</x-mail::message>
Why a table and a scheduler, not delayed jobs
Laravel can dispatch a job with ->delay($sendAt), so why not queue every message at confirmation time? Because messages change after they’re planned. A cancellation must cancel them, new dates must move them (scene 23), and a host editing a template expects future messages to follow. A delayed job can’t be found and changed once it’s in the queue; a row in a table can, with one UPDATE. The table is also what support looks at when a guest asks "did you send it?".
Laravel inside: chunkById()
Like scene 11’s lazyById(), but it hands your closure one batch at a time instead of one row. Paging by id (WHERE id > ?) instead of by offset matters here: the job may update rows while the loop runs, and an offset would skip some.
public function chunkById($count, callable $callback, $column = null, $alias = null)
{
$lastId = null;
do {
$clone = clone $this;
$results = $clone->forPageAfterId($count, $lastId, $column)->get(); // WHERE id > $lastId ORDER BY id LIMIT 500
$countResults = $results->count();
if ($countResults === 0) {
break;
}
if ($callback($results) === false) { // return false to stop early
return false;
}
$lastId = $results->last()->{$alias ?? 'id'};
} while ($countResults === $count);
return true;
}
TestsSend times in two timezones across daylight saving, only due messages queued, and channel bookings scheduled too
<?php
use App\Enums\MessageTrigger;
use App\Messaging\SendTime;
use App\Models\Booking;
use App\Models\MessageTemplate;
use App\Models\Property;
it('sends "the day before at 15:00" at 15:00 on the property’s clock', function (string $timezone, string $checkIn, string $utc) {
$property = Property::factory()->create(['timezone' => $timezone]);
$booking = Booking::factory()->for($property)->forStay($checkIn, 3)->create();
$template = MessageTemplate::forceCreate([
'account_id' => $property->account_id, 'name' => 'Arrival', 'trigger' => MessageTrigger::BeforeCheckIn,
'days_offset' => -1, 'send_time' => '15:00', 'subject' => 'Tomorrow', 'body' => 'See you tomorrow',
]);
expect(app(SendTime::class)->for($template, $booking)->toDateTimeString())->toBe($utc);
})->with([
'Copenhagen, winter (UTC+1)' => ['Europe/Copenhagen', '2027-03-28', '2027-03-27 14:00:00'],
'Copenhagen, summer (UTC+2)' => ['Europe/Copenhagen', '2027-03-29', '2027-03-28 13:00:00'], // clocks moved that night
'Queenstown, winter (UTC+12)' => ['Pacific/Auckland', '2027-07-10', '2027-07-09 03:00:00'], // the incident
'Queenstown, summer (UTC+13)' => ['Pacific/Auckland', '2027-09-28', '2027-09-27 02:00:00'],
]);
<?php
use App\Jobs\SendScheduledMessage;
use App\Models\Booking;
use App\Models\MessageTemplate;
use App\Models\ScheduledMessage;
use Illuminate\Support\Facades\Queue;
it('queues the due messages and nothing else', function () {
Queue::fake();
$booking = Booking::factory()->create();
$template = MessageTemplate::forceCreate(['account_id' => $booking->property->account_id, 'name' => 'Hi', 'trigger' => 'booking_confirmed', 'subject' => 'Hi', 'body' => 'Hi']);
$due = ScheduledMessage::forceCreate(['booking_id' => $booking->id, 'message_template_id' => $template->id, 'send_at' => now()->subMinute()]);
$later = MessageTemplate::forceCreate(['account_id' => $booking->property->account_id, 'name' => 'Review request', 'trigger' => 'after_check_out', 'days_offset' => 1, 'send_time' => '10:00', 'subject' => 'How was your stay?', 'body' => 'How was your stay at {property_name}? We would love to hear.']);
ScheduledMessage::forceCreate(['booking_id' => $booking->id, 'message_template_id' => $later->id, 'send_at' => now()->addDay()]);
$this->artisan('messages:send-due')->assertSuccessful();
Queue::assertPushedTimes(SendScheduledMessage::class, 1);
Queue::assertPushed(SendScheduledMessage::class, fn (SendScheduledMessage $job): bool => $job->message->is($due));
});
<?php
use App\Channels\BookingCom\BookingComReservationMapper;
use App\Channels\ImportChannelReservation;
use App\Enums\Channel;
use App\Enums\MessageTrigger;
use App\Models\Booking;
use App\Models\ChannelListing;
use App\Models\MessageTemplate;
use App\Models\Property;
use App\Models\ScheduledMessage;
it('schedules the guest messages for a booking that arrives from a channel', function () {
$property = Property::factory()->create();
ChannelListing::forceCreate(['property_id' => $property->id, 'channel' => Channel::BookingCom, 'external_listing_id' => '1290031']);
MessageTemplate::forceCreate([
'account_id' => $property->account_id, 'name' => 'Arrival', 'trigger' => MessageTrigger::BeforeCheckIn,
'days_offset' => -1, 'send_time' => '15:00', 'subject' => 'Tomorrow', 'body' => 'See you tomorrow',
]);
app(ImportChannelReservation::class)->handle((new BookingComReservationMapper())->reservation(fixture('booking_com/reservation-new.json')));
expect(ScheduledMessage::where('booking_id', Booking::sole()->id)->count())->toBe(1); // red without the call in the import
});
Remember
- "15:00, one day before" is a local time: compute
send_atin the property’s timezone, store it in UTC. - Test across a daylight-saving change with a frozen clock.
Messages leave at the right local time. Thursday, a guest gets her door code twice, four seconds apart.