feat: add SendPaymentFailedReminderJob with grace guard

This commit is contained in:
Ovidiu U
2026-04-23 10:51:13 +01:00
parent c127cc379e
commit 5b17f4cae4
2 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Jobs;
use App\Mail\PaymentFailedDay3Reminder;
use App\Mail\PaymentFailedDay5Reminder;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;
use InvalidArgumentException;
final class SendPaymentFailedReminderJob implements ShouldQueue
{
use Queueable;
public function __construct(
public readonly int $userId,
public readonly int $day,
) {
$this->onQueue('notifications');
}
public function handle(): void
{
$user = User::find($this->userId);
if ($user === null) {
return;
}
if ($user->grace_period_until === null) {
return;
}
$mailable = match ($this->day) {
3 => new PaymentFailedDay3Reminder($user),
5 => new PaymentFailedDay5Reminder($user),
default => throw new InvalidArgumentException("Unsupported reminder day: {$this->day}"),
};
Mail::to($user->email)->send($mailable);
}
}

View File

@@ -0,0 +1,54 @@
<?php
use App\Jobs\SendPaymentFailedReminderJob;
use App\Mail\PaymentFailedDay3Reminder;
use App\Mail\PaymentFailedDay5Reminder;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
uses(RefreshDatabase::class);
it('sends the day-3 mailable when grace_period_until is still set', function (): void {
Mail::fake();
$user = User::factory()->create(['grace_period_until' => now()->addDays(2)]);
(new SendPaymentFailedReminderJob($user->id, 3))->handle();
Mail::assertSent(PaymentFailedDay3Reminder::class, fn ($mail) => $mail->hasTo($user->email));
});
it('sends the day-5 mailable when grace_period_until is still set', function (): void {
Mail::fake();
$user = User::factory()->create(['grace_period_until' => now()->addDay()]);
(new SendPaymentFailedReminderJob($user->id, 5))->handle();
Mail::assertSent(PaymentFailedDay5Reminder::class, fn ($mail) => $mail->hasTo($user->email));
});
it('silently skips the day-3 mailable when grace has been cleared', function (): void {
Mail::fake();
$user = User::factory()->create(['grace_period_until' => null]);
(new SendPaymentFailedReminderJob($user->id, 3))->handle();
Mail::assertNothingSent();
});
it('silently skips the day-5 mailable when grace has been cleared', function (): void {
Mail::fake();
$user = User::factory()->create(['grace_period_until' => null]);
(new SendPaymentFailedReminderJob($user->id, 5))->handle();
Mail::assertNothingSent();
});
it('silently skips when the user no longer exists', function (): void {
Mail::fake();
(new SendPaymentFailedReminderJob(999999, 3))->handle();
Mail::assertNothingSent();
});