Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .ai/decisions.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,26 @@ Rejected as more tooling than the actual risk (single author, single
codebase) justifies right now.

---

## Status-change observer lives inline on `Application`, not a separate `Observer` class
_Recorded: 2026-09-03_

Laravel supports both a dedicated `Observer` class (registered via
`#[ObservedBy]` or in a service provider) and reacting to model events
directly inside the model's own `boot()`. The spec (`v1.md`) only specifies
the *effect* ("via an observer on `Application`"), not which of the two.

**Why:** kept inline in `Application::boot()` rather than split into
`app/Observers/ApplicationObserver.php`. A separate Observer class is easy
to forget exists — it's registered elsewhere, invisible from the model
itself, and its file naturally goes unopened while working on the model.
Inline keeps the status-change side effect visible right next to `status`'s
own logic (`canTransitionTo()`), at the cost of a slightly larger model
class.

**Alternative considered:** a dedicated `ApplicationObserver` class,
Laravel's more common convention for this. Rejected for now on the
forgettability argument above; revisit if `Application` accumulates enough
inline event handling to outweigh that.

---
24 changes: 24 additions & 0 deletions app/Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use App\Enums\ApplicationStatus;
use App\Enums\InteractionType;
use Carbon\CarbonImmutable;
use Database\Factories\ApplicationFactory;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -41,6 +42,29 @@ final class Application extends Model
'notes',
];

protected static function boot(): void
{
parent::boot();

self::updated(function (Application $application) {
if (! $application->wasChanged('status')) {
return;
}

/** @var array<string, string> $previous */
$previous = $application->getPrevious();
$application->interactions()->create([
'type' => InteractionType::STATUS_CHANGE,
'occurred_at' => CarbonImmutable::now(),
'body' => sprintf(
'Status changed from %s to %s.',
$previous['status'],
$application->status->value,
),
]);
});
}

/** @phpstan-return BelongsTo<Company, $this> */
public function company(): BelongsTo
{
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/ApplicationStatusChangeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use App\Models\Application;
use App\Models\Interaction;

test('an interaction is created when the status of an application is updated', function () {
$application = Application::factory()
->lead()
->create();
$application->update(['status' => 'applied']);

/** @var Interaction $interaction */
$interaction = $application->interactions()->first();

expect(Interaction::query()->count())
->toBe(1)
->and($interaction->type->value)
->toBe('status_change');
});