Edit File: NotificationService.php
<?php namespace App\Services; use App\Jobs\AdminNotify; use App\Jobs\Notify; use App\Jobs\SendEmailJob; use App\Jobs\SendSms; use App\Models\Admin; use App\Models\Delegate; use App\Models\Provider; use App\Models\User; use App\Notifications\NotifyUser; use Illuminate\Support\Facades\Notification; class NotificationService { public function send($request): array { if ($request->user_type === 'all') { $this->sendNotificationToAll($request); } else { $rows = $this->getRows($request->user_type, $request->id); match ($request->type) { 'notify' => $this->sendNotification($rows, $request->user_type, $request), 'email' => $this->sendMail($rows, $request), 'sms' => $this->sendSms($rows->pluck('phone')->toArray(), $request->message), default => $this->sendNotification($rows, $request->user_type, $request), }; } return ['key' => 'success', 'msg' => __('apis.success')]; } protected function sendNotification($rows, $type, $request): void { $type === 'admins' ? dispatch(new AdminNotify($rows, $request)) : Notification::send( $rows, new NotifyUser($request)); // $type === 'admins' ? dispatch(new AdminNotify($rows, $request)) : dispatch(new Notify($rows, $request)); } protected function sendMail($rows, $request): void { dispatch(new SendEmailJob($rows->pluck('email'), $request)); } protected function sendSms($phones, $message): void { dispatch(new SendSms($phones, $message)); } protected function getRows($type, $id = null) { return match ($type) { 'users' => $id ? User::findOrFail($id) : User::all(), 'admins' => Admin::all(), 'delegates' => $id ? Delegate::findOrFail($id) : Delegate::all(), 'providers' => $id ? Provider::findOrFail($id) : Provider::all(), default => collect(), // Return an empty collection if the $type doesn't match any case }; } protected function sendNotificationToAll($request): void { dispatch(new Notify(Provider::all(), $request)); dispatch(new Notify(Delegate::all(), $request)); dispatch(new Notify(User::all(), $request)); dispatch(new AdminNotify(Admin::all(), $request)); } public function all($user, $paginateNum = 10): array { $notifications = $user->notifications()->paginate($paginateNum); return ['key' => 'success', 'notifications' => $notifications, 'msg' => __('apis.success')]; } public function markAsReadNotifications($user, $paginateNum = 20): array { $notifications = $user->unreadNotifications()->paginate($paginateNum); $notifications->getCollection()->each->markAsRead(); return ['key' => 'success', 'notifications' => $notifications, 'msg' => __('apis.success')]; } public function unreadNotificationsCount($user): array { $notificationsCount = $user->unreadNotifications()->count(); return ['key' => 'success', 'count' => $notificationsCount, 'msg' => __('apis.success')]; } public function deleteSelected($user, $request): array { $requestIds = array_column(json_decode($request->data), 'id'); $user->notifications()->whereIn('id', $requestIds)->delete(); return ['msg' => __('apis.deleted'), 'key' => 'success']; } public function deleteAll($user): array { $user->notifications()->delete(); return ['msg' => __('apis.deleted'), 'key' => 'success']; } public function switchNotificationStatus($user): array { $user->update(['is_notify' => !$user->is_notify]); return ['key' => 'success', 'msg' => __('apis.updated'), 'data' => $user->refresh()->is_notify]; } public function deleteOne($user, $id): array { $user->notifications()->whereId($id)->delete(); return ['msg' => __('apis.deleted'), 'key' => 'success']; } }
Back to File Manager