feat: add SendPaymentFailedReminderJob with grace guard
This commit is contained in:
44
app/Jobs/SendPaymentFailedReminderJob.php
Normal file
44
app/Jobs/SendPaymentFailedReminderJob.php
Normal 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);
|
||||
}
|
||||
}
|
||||
54
tests/Feature/Payments/SendPaymentFailedReminderJobTest.php
Normal file
54
tests/Feature/Payments/SendPaymentFailedReminderJobTest.php
Normal 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();
|
||||
});
|
||||
Reference in New Issue
Block a user