| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
use FluentSupport\App\Services\Notifications\NotificationCategory; |
| 7 |
use FluentSupport\App\Services\Notifications\NotificationFormatter; |
| 8 |
use FluentSupport\App\Services\Notifications\NotificationService; |
| 9 |
use FluentSupport\Framework\Http\Request\Request; |
| 10 |
|
| 11 |
class NotificationController extends Controller |
| 12 |
{ |
| 13 |
public function index(Request $request) |
| 14 |
{ |
| 15 |
$agent = Helper::getAgentByUserId(); |
| 16 |
|
| 17 |
if (!$agent) { |
| 18 |
return $this->sendError([ |
| 19 |
'message' => __('Sorry, You do not have permission. Please add yourself as support agent first', 'fluent-support') |
| 20 |
]); |
| 21 |
} |
| 22 |
|
| 23 |
$notificationService = new NotificationService(); |
| 24 |
$formatter = new NotificationFormatter(); |
| 25 |
|
| 26 |
return array_merge([ |
| 27 |
'notifications' => $formatter->formatPaginated($notificationService->paginateForPerson($agent->id, $this->getFilters($request))), |
| 28 |
'unread_count' => $notificationService->getUnreadCount($agent->id) |
| 29 |
], $this->getNotificationMeta()); |
| 30 |
} |
| 31 |
|
| 32 |
public function unread(Request $request) |
| 33 |
{ |
| 34 |
$agent = Helper::getAgentByUserId(); |
| 35 |
|
| 36 |
if (!$agent) { |
| 37 |
return $this->sendError([ |
| 38 |
'message' => __('Sorry, You do not have permission. Please add yourself as support agent first', 'fluent-support') |
| 39 |
]); |
| 40 |
} |
| 41 |
|
| 42 |
$filters = array_merge($this->getFilters($request), [ |
| 43 |
'status' => 'unread', |
| 44 |
'limit' => $request->getSafe('limit', 'intval') |
| 45 |
]); |
| 46 |
|
| 47 |
$notificationService = new NotificationService(); |
| 48 |
$formatter = new NotificationFormatter(); |
| 49 |
|
| 50 |
return array_merge([ |
| 51 |
'notifications' => $formatter->formatMany($notificationService->getRecentForPerson($agent->id, $filters)), |
| 52 |
'unread_count' => $notificationService->getUnreadCount($agent->id) |
| 53 |
], $this->getNotificationMeta()); |
| 54 |
} |
| 55 |
|
| 56 |
public function unreadCount(Request $request) |
| 57 |
{ |
| 58 |
$agent = Helper::getAgentByUserId(); |
| 59 |
|
| 60 |
if (!$agent) { |
| 61 |
return $this->sendError([ |
| 62 |
'message' => __('Sorry, You do not have permission. Please add yourself as support agent first', 'fluent-support') |
| 63 |
]); |
| 64 |
} |
| 65 |
|
| 66 |
return [ |
| 67 |
'count' => (new NotificationService())->getUnreadCount($agent->id, $this->getFilters($request)) |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
public function markRead(Request $request, $notification_id) |
| 72 |
{ |
| 73 |
$agent = Helper::getAgentByUserId(); |
| 74 |
|
| 75 |
if (!$agent) { |
| 76 |
return $this->sendError([ |
| 77 |
'message' => __('Sorry, You do not have permission. Please add yourself as support agent first', 'fluent-support') |
| 78 |
]); |
| 79 |
} |
| 80 |
|
| 81 |
return [ |
| 82 |
'updated' => (new NotificationService())->markAsRead((int) $notification_id, $agent->id), |
| 83 |
'unread_count' => (new NotificationService())->getUnreadCount($agent->id) |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
public function markAllRead(Request $request) |
| 88 |
{ |
| 89 |
$agent = Helper::getAgentByUserId(); |
| 90 |
|
| 91 |
if (!$agent) { |
| 92 |
return $this->sendError([ |
| 93 |
'message' => __('Sorry, You do not have permission. Please add yourself as support agent first', 'fluent-support') |
| 94 |
]); |
| 95 |
} |
| 96 |
|
| 97 |
return [ |
| 98 |
'updated' => (new NotificationService())->markAllAsRead($agent->id), |
| 99 |
'unread_count' => (new NotificationService())->getUnreadCount($agent->id) |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
protected function getFilters(Request $request) |
| 104 |
{ |
| 105 |
return [ |
| 106 |
'category' => $request->getSafe('category', 'sanitize_text_field'), |
| 107 |
'status' => $request->getSafe('status', 'sanitize_text_field'), |
| 108 |
'ticket_id' => $request->getSafe('ticket_id', 'intval'), |
| 109 |
'per_page' => $request->getSafe('per_page', 'intval') |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
protected function getNotificationMeta() |
| 114 |
{ |
| 115 |
$categories = NotificationCategory::options(); |
| 116 |
|
| 117 |
return [ |
| 118 |
'meta' => [ |
| 119 |
'categories' => $categories |
| 120 |
], |
| 121 |
'notification_categories' => $categories |
| 122 |
]; |
| 123 |
} |
| 124 |
} |
| 125 |
|