NotificationService.php
1 <?php 2 3 namespace App\Http\Services; 4 5 use App\Models\Dossier; 6 use App\Models\Role; 7 use App\Models\User; 8 use Illuminate\Support\Facades\DB; 9 10 class NotificationService 11 { 12 /** 13 * Create resources 14 * 15 * @return \Illuminate\Http\Response 16 */ 17 public static function searchNotifications(string $searchString, string $sortField, string $sortDirection, array|null $filters) 18 { 19 $notificationsQuery = DB::table('notifications') 20 ->join('users', 'users.id', '=', 'notifications.notifiable_id') 21 ->where('users.id', auth()->user()->id) 22 ->select('notifications.*', 'notifications.created_at as notification_created_at'); 23 24 $notificationsQuery->when($filters['readed'], function ($query) { 25 $query; 26 }, function ($query) { 27 $query->whereNull('notifications.read_at'); 28 }); 29 30 $notificationsQuery 31 ->where(function ($query) use ($searchString) { 32 $query 33 ->where('notifications.data', 'like', '%' . $searchString . '%'); 34 }); 35 36 if ($filters && $filters['created_at_since']) { 37 $notificationsQuery->where('notifications.created_at', '>=', $filters['created_at_since']); 38 } 39 40 if ($filters && $filters['created_at_to']) { 41 $notificationsQuery->where('notifications.created_at', '<=', $filters['created_at_to']); 42 } 43 44 if ($filters && $filters['types']) { 45 $notificationsQuery->whereIn('type', $filters['types']); 46 } 47 48 if ($sortField && $sortDirection) { 49 $notificationsQuery->orderBy($sortField, $sortDirection); 50 } 51 52 return $notificationsQuery; 53 } 54 55 public function notifyManually(array $usersSelected, string $text) 56 { 57 $users = User::whereIn('id', $usersSelected)->get(); 58 59 foreach ($users as $user) { 60 $user->notify(new \App\Notifications\ManualNotification($text)); 61 } 62 } 63 64 public static function generatePendingRectifyNotification(Dossier $dossier) 65 { 66 if ($user = $dossier->principalCertifier) { 67 $user->notify(new \App\Notifications\PendingRectifyNotification($dossier)); 68 } else { 69 $user = Role::where('name', 'technical_manager')->first()->users->first(); 70 $user->notify(new \App\Notifications\PendingRectifyNotification($dossier)); 71 } 72 } 73 }