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>
72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Notifications\Channels;
|
|
|
|
use App\Services\ApiLogger;
|
|
use Illuminate\Notifications\Notification;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Sends SMS messages via the Vonage SMS API (raw HTTP — no SDK).
|
|
*
|
|
* Notifications targeting this channel must implement `toVonageSms($notifiable)`
|
|
* returning a string body (or `null` to skip).
|
|
*
|
|
* No-ops when VONAGE_KEY/SECRET are unset or when the notifiable user has no
|
|
* phone number on `whatsapp_number` (the same verified column doubles as SMS
|
|
* destination).
|
|
*/
|
|
final class VonageSmsChannel
|
|
{
|
|
public const string NAME = 'vonage-sms';
|
|
|
|
public function __construct(
|
|
private readonly ApiLogger $apiLogger,
|
|
) {}
|
|
|
|
public function send(mixed $notifiable, Notification $notification): void
|
|
{
|
|
$key = config('services.vonage.key');
|
|
$secret = config('services.vonage.secret');
|
|
$from = config('services.vonage.sms_from', 'FuelAlert');
|
|
|
|
if ($key === null || $secret === null) {
|
|
Log::info('VonageSmsChannel: skipped — credentials not configured');
|
|
|
|
return;
|
|
}
|
|
|
|
$to = $notifiable->whatsapp_number ?? null;
|
|
|
|
if ($to === null) {
|
|
return;
|
|
}
|
|
|
|
$body = method_exists($notification, 'toVonageSms')
|
|
? $notification->toVonageSms($notifiable)
|
|
: null;
|
|
|
|
if ($body === null) {
|
|
return;
|
|
}
|
|
|
|
$url = 'https://rest.nexmo.com/sms/json';
|
|
|
|
try {
|
|
$this->apiLogger->send(self::NAME, 'POST', $url, fn () => Http::timeout(10)
|
|
->asForm()
|
|
->post($url, [
|
|
'api_key' => $key,
|
|
'api_secret' => $secret,
|
|
'from' => $from,
|
|
'to' => ltrim($to, '+'),
|
|
'text' => $body,
|
|
]));
|
|
} catch (Throwable $e) {
|
|
Log::error('VonageSmsChannel: send failed', ['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
}
|