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
43 changes: 43 additions & 0 deletions app/Console/Commands/ResendLeadNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Console\Commands;

use App\Models\Lead;
use App\Notifications\NewLeadSubmitted;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Notification;

class ResendLeadNotifications extends Command
{
protected $signature = 'app:resend-lead-notifications {date : The date to resend leads from (Y-m-d)}';

protected $description = 'Re-send lead notifications to the sales email for leads submitted on or after a given date.';

public function handle(): void
{
$date = Carbon::createFromFormat('Y-m-d', $this->argument('date'))->startOfDay();

$leads = Lead::query()
->where('created_at', '>=', $date)
->orderBy('created_at')
->get();

if ($leads->isEmpty()) {
$this->info('No leads found from '.$date->toDateString().' onwards.');

return;
}

$this->info("Found {$leads->count()} lead(s) from {$date->toDateString()} onwards.");

foreach ($leads as $lead) {
Notification::route('mail', 'sales@nativephp.com')
->notify(new NewLeadSubmitted($lead));

$this->line("Sent notification for lead: {$lead->company} ({$lead->email})");
}

$this->info('Done.');
}
}
60 changes: 60 additions & 0 deletions tests/Feature/ResendLeadNotificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Tests\Feature;

use App\Models\Lead;
use App\Notifications\NewLeadSubmitted;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class ResendLeadNotificationsTest extends TestCase
{
use RefreshDatabase;

#[Test]
public function it_resends_notifications_for_leads_from_the_given_date(): void
{
Notification::fake();

$oldLead = Lead::factory()->create(['created_at' => '2025-01-01 12:00:00']);
$matchingLead = Lead::factory()->create(['created_at' => '2025-03-15 09:00:00']);
$newerLead = Lead::factory()->create(['created_at' => '2025-03-16 14:00:00']);

$this->artisan('app:resend-lead-notifications', ['date' => '2025-03-15'])
->expectsOutputToContain('Found 2 lead(s)')
->expectsOutputToContain('Done.')
->assertExitCode(0);

Notification::assertSentOnDemand(
NewLeadSubmitted::class,
function ($notification, $channels, $notifiable) use ($matchingLead) {
return $notifiable->routes['mail'] === 'sales@nativephp.com'
&& $notification->lead->is($matchingLead);
}
);

Notification::assertSentOnDemand(
NewLeadSubmitted::class,
function ($notification, $channels, $notifiable) use ($newerLead) {
return $notifiable->routes['mail'] === 'sales@nativephp.com'
&& $notification->lead->is($newerLead);
}
);

Notification::assertNotSentTo($oldLead, NewLeadSubmitted::class);
}

#[Test]
public function it_shows_message_when_no_leads_are_found(): void
{
Notification::fake();

$this->artisan('app:resend-lead-notifications', ['date' => '2099-01-01'])
->expectsOutputToContain('No leads found')
->assertExitCode(0);

Notification::assertNothingSent();
}
}
Loading