Refactor push notification logic in afterSave method to streamline subscription checks and improve logging #160

Merged
jothi merged 1 commits from ranjith-dev into master 2026-01-30 06:51:41 +00:00

View File

@@ -129,17 +129,19 @@ class EditRfqTransporterBid extends EditRecord
// ]); // ]);
// } // }
// } // }
foreach ($users as $user) { foreach ($users as $user) {
$subscriptions = $user->pushSubscriptions()->get(); $count = $user->pushSubscriptions()->count();
Log::info('Checking push subscription for user', [ Log::info('Checking push subscription for user', [
'user_id' => $user->id, 'user_id' => $user->id,
'name' => $user->name, 'name' => $user->name,
'subscription_count' => $subscriptions->count(), 'subscription_count' => $count,
]); ]);
if ($subscriptions->isEmpty()) { if ($count == 0) {
Log::warning('User has NO push subscription', [ Log::warning('User has NO push subscription', [
'user_id' => $user->id, 'user_id' => $user->id,
]); ]);
@@ -150,24 +152,13 @@ class EditRfqTransporterBid extends EditRecord
'user_id' => $user->id, 'user_id' => $user->id,
]); ]);
foreach ($subscriptions as $subscription) { // ✅ THIS IS ALL YOU NEED
try { $user->notify(new PushAlertNotification(
\Notification::route('webpush', $subscription)
->notify(new PushAlertNotification(
'Rank Updated', 'Rank Updated',
$body $body
)); ));
} catch (\Throwable $e) { }
Log::warning('Removing invalid push subscription', [
'user_id' => $user->id,
'endpoint' => $subscription->endpoint,
'error' => $e->getMessage(),
]);
$subscription->delete();
}
}
}
} }