tenantId); if (! $tenant) { Log::warning('Tenant credits low job skipped; tenant missing', [ 'tenant_id' => $this->tenantId, ]); return; } $preferences = app(\App\Services\Packages\TenantNotificationPreferences::class); if (! $preferences->shouldNotify($tenant, 'credits_low')) { $this->logNotification($tenant, [ 'type' => 'credits_low', 'status' => 'skipped', 'context' => [ 'balance' => $this->balance, 'threshold' => $this->threshold, 'reason' => 'opt_out', ], ]); return; } $emails = collect([ $tenant->contact_email, $tenant->user?->email, ])->filter(fn ($email) => is_string($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) ->unique(); if ($emails->isEmpty()) { Log::info('Tenant credits low notification skipped due to missing recipients', [ 'tenant_id' => $tenant->id, ]); $this->logNotification($tenant, [ 'type' => 'credits_low', 'status' => 'skipped', 'context' => [ 'balance' => $this->balance, 'threshold' => $this->threshold, 'reason' => 'no_recipient', ], ]); return; } $context = [ 'balance' => $this->balance, 'threshold' => $this->threshold, ]; $this->dispatchToRecipients( $tenant, $emails, 'credits_low', function (string $email) use ($tenant) { Notification::route('mail', $email)->notify(new TenantCreditsLowNotification( $tenant, $this->balance, $this->threshold, )); }, $context ); } }