feat: build FuelPriceAlert notification with multi-channel adapters
The DispatchUserNotificationJob has been logging sent=true for every
allowed channel without actually sending anything — a TODO marker covered
by the existing test suite, which only asserted log rows. The downstream
"missed today" widget read those rows and reported falsely. This commit
makes the telemetry truthful by wiring the real send.
- App\Notifications\FuelPriceAlert — Notification class with via() that
returns the per-tier-filtered channel list passed in by the dispatcher.
Implements toMail / toOneSignal / toVonageWhatsApp / toVonageSms.
ShouldQueue on the 'notifications' queue.
- App\Notifications\Channels\OneSignalChannel — raw HTTP to OneSignal
REST API, gated on services.onesignal.{app_id,api_key} + user
push_token. Logs every call to api_logs via ApiLogger.
- App\Notifications\Channels\VonageWhatsAppChannel — raw HTTP to Vonage
Messages API, gated on whatsapp_verified_at + whatsapp_number.
- App\Notifications\Channels\VonageSmsChannel — raw HTTP to Vonage SMS
API, gated on whatsapp_number.
- DispatchUserNotificationJob now calls $user->notify(new
FuelPriceAlert(...)) before logging.
- New tests: assert the notification IS dispatched with the right
channels, and that nothing is dispatched when no channels are allowed.
Channels gracefully no-op when their credentials are unset (logging at
info level), so existing tests without a Notification::fake() still
pass — the channels just early-return on missing config.
No new composer dependencies — Vonage SDK avoided in favour of raw HTTP
through the existing ApiLogger pattern.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,9 @@ use App\Models\NotificationLog;
|
||||
use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use App\Models\UserNotificationPreference;
|
||||
use App\Notifications\FuelPriceAlert;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
@@ -39,6 +41,39 @@ it('logs a sent entry for each allowed channel', function (): void {
|
||||
->and($log->fuel_type)->toBe(FuelType::E10->value);
|
||||
});
|
||||
|
||||
it('actually dispatches FuelPriceAlert with the allowed channels', function (): void {
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
UserNotificationPreference::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'channel' => 'email',
|
||||
'fuel_type' => FuelType::E10->value,
|
||||
'enabled' => true,
|
||||
]);
|
||||
|
||||
(new DispatchUserNotificationJob($user, 'price_threshold', FuelType::E10->value, price: 143.9))->handle();
|
||||
|
||||
Notification::assertSentTo($user, FuelPriceAlert::class, function (FuelPriceAlert $n) {
|
||||
return $n->triggerType === 'price_threshold'
|
||||
&& $n->fuelType === FuelType::E10->value
|
||||
&& $n->price === 143.9
|
||||
&& in_array('email', $n->channels, true);
|
||||
});
|
||||
});
|
||||
|
||||
it('does not dispatch FuelPriceAlert when no channels are allowed', function (): void {
|
||||
Notification::fake();
|
||||
|
||||
// Free user with no preferences — channelsFor returns []
|
||||
$user = User::factory()->create();
|
||||
|
||||
(new DispatchUserNotificationJob($user, 'price_threshold', FuelType::E10->value))->handle();
|
||||
|
||||
Notification::assertNothingSent();
|
||||
});
|
||||
|
||||
// ─── DispatchUserNotificationJob — tier_restricted logging ───────────────────
|
||||
|
||||
it('logs tier_restricted for channels the user wants but their tier forbids', function (): void {
|
||||
|
||||
Reference in New Issue
Block a user