| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Notifications; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Notification; |
| 6 |
use FluentSupport\App\Models\NotificationUser; |
| 7 |
use FluentSupport\Framework\Pagination\LengthAwarePaginator; |
| 8 |
use FluentSupport\Framework\Support\Arr; |
| 9 |
|
| 10 |
class NotificationQueryService |
| 11 |
{ |
| 12 |
public function getNotificationsForPerson($personId, array $filters = []) |
| 13 |
{ |
| 14 |
if (!(new NotificationSettings())->canUseNotificationTables(true)) { |
| 15 |
return null; |
| 16 |
} |
| 17 |
|
| 18 |
$query = Notification::whereHas('recipients', function ($recipientQuery) use ($personId, $filters) { |
| 19 |
$recipientQuery->where('user_id', $personId); |
| 20 |
|
| 21 |
$status = Arr::get($filters, 'status'); |
| 22 |
if ($status === 'unread') { |
| 23 |
$recipientQuery->where('is_read', 0); |
| 24 |
} elseif ($status === 'read') { |
| 25 |
$recipientQuery->where('is_read', 1); |
| 26 |
} |
| 27 |
})->with([ |
| 28 |
'actor' => function ($actorQuery) { |
| 29 |
$actorQuery->select(['id', 'first_name', 'last_name', 'email', 'avatar']); |
| 30 |
}, |
| 31 |
'ticket' => function ($ticketQuery) { |
| 32 |
$ticketQuery->select(['id', 'title']); |
| 33 |
}, |
| 34 |
'recipients' => function ($recipientQuery) use ($personId) { |
| 35 |
$recipientQuery->select(['id', 'notification_id', 'user_id', 'is_read', 'read_at']) |
| 36 |
->where('user_id', $personId); |
| 37 |
} |
| 38 |
]); |
| 39 |
|
| 40 |
$this->applyTicketAccessScope($query); |
| 41 |
|
| 42 |
$category = Arr::get($filters, 'category'); |
| 43 |
if ($category && $category !== 'all') { |
| 44 |
$query->byCategory($category); |
| 45 |
} |
| 46 |
|
| 47 |
$ticketId = Arr::get($filters, 'ticket_id'); |
| 48 |
if ($ticketId) { |
| 49 |
$query->where('ticket_id', (int) $ticketId); |
| 50 |
} |
| 51 |
|
| 52 |
return $query->orderBy('created_at', 'DESC'); |
| 53 |
} |
| 54 |
|
| 55 |
public function paginateForPerson($personId, array $filters = []) |
| 56 |
{ |
| 57 |
$perPage = min(max(absint(Arr::get($filters, 'per_page', 15)), 1), 100); |
| 58 |
$query = $this->getNotificationsForPerson($personId, $filters); |
| 59 |
|
| 60 |
if (!$query) { |
| 61 |
return new LengthAwarePaginator([], 0, $perPage); |
| 62 |
} |
| 63 |
|
| 64 |
return $query->paginate($perPage); |
| 65 |
} |
| 66 |
|
| 67 |
public function getRecentForPerson($personId, array $filters = []) |
| 68 |
{ |
| 69 |
$query = $this->getNotificationsForPerson($personId, $filters); |
| 70 |
|
| 71 |
if (!$query) { |
| 72 |
return []; |
| 73 |
} |
| 74 |
|
| 75 |
$limit = absint(Arr::get($filters, 'limit', 8)); |
| 76 |
if (!$limit) { |
| 77 |
$limit = 8; |
| 78 |
} |
| 79 |
|
| 80 |
$limit = min($limit, 20); |
| 81 |
|
| 82 |
return $query->limit($limit)->get(); |
| 83 |
} |
| 84 |
|
| 85 |
public function getUnreadCount($personId, array $filters = []) |
| 86 |
{ |
| 87 |
if (!(new NotificationSettings())->canUseNotificationTables(true)) { |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
$query = NotificationUser::where('user_id', $personId)->unread(); |
| 92 |
|
| 93 |
$category = Arr::get($filters, 'category'); |
| 94 |
$ticketId = Arr::get($filters, 'ticket_id'); |
| 95 |
|
| 96 |
$query->whereHas('notification', function ($notificationQuery) use ($category, $ticketId) { |
| 97 |
$this->applyTicketAccessScope($notificationQuery); |
| 98 |
|
| 99 |
if ($category && $category !== 'all') { |
| 100 |
$notificationQuery->byCategory($category); |
| 101 |
} |
| 102 |
|
| 103 |
if ($ticketId) { |
| 104 |
$notificationQuery->where('ticket_id', (int) $ticketId); |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
return (int) $query->count(); |
| 109 |
} |
| 110 |
|
| 111 |
protected function applyTicketAccessScope($query) |
| 112 |
{ |
| 113 |
$query->whereHas('ticket', function ($ticketQuery) { |
| 114 |
do_action_ref_array('fluent_support/tickets_query_by_permission_ref', [&$ticketQuery, false]); |
| 115 |
do_action_ref_array('fluent_support\main_tickets_query', [&$ticketQuery, []]); |
| 116 |
}); |
| 117 |
} |
| 118 |
} |
| 119 |
|