114 lines
3.6 KiB
PHP
114 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Worker;
|
|
|
|
class WorkerNotificationController extends Controller
|
|
{
|
|
/**
|
|
* List all notifications for the worker.
|
|
* GET /api/workers/notifications
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
/** @var Worker $worker */
|
|
$worker = $request->attributes->get('worker');
|
|
|
|
$filter = $request->query('filter', 'all');
|
|
$perPage = (int) $request->query('per_page', 15);
|
|
|
|
if ($filter === 'read') {
|
|
$query = $worker->readNotifications();
|
|
} elseif ($filter === 'unread') {
|
|
$query = $worker->unreadNotifications();
|
|
} else {
|
|
$query = $worker->notifications();
|
|
}
|
|
|
|
$paginator = $query->paginate($perPage);
|
|
|
|
$notifications = collect($paginator->items())->map(function ($notification) {
|
|
return [
|
|
'id' => $notification->id,
|
|
'type' => $notification->type,
|
|
'data' => $notification->data,
|
|
'read_at' => $notification->read_at ? $notification->read_at->toIso8601String() : null,
|
|
'created_at' => $notification->created_at->toIso8601String(),
|
|
'time_ago' => $notification->created_at->diffForHumans(),
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'notifications' => $notifications,
|
|
'pagination' => [
|
|
'total' => $paginator->total(),
|
|
'per_page' => $paginator->perPage(),
|
|
'current_page' => $paginator->currentPage(),
|
|
'last_page' => $paginator->lastPage(),
|
|
]
|
|
]
|
|
], 200);
|
|
}
|
|
|
|
/**
|
|
* Mark a single notification or all unread notifications as read.
|
|
* PUT /api/workers/notifications/{id}/read or PUT /api/workers/notifications/read
|
|
*/
|
|
public function markAsRead(Request $request, $id = null)
|
|
{
|
|
/** @var Worker $worker */
|
|
$worker = $request->attributes->get('worker');
|
|
|
|
if ($id) {
|
|
$notification = $worker->notifications()->where('id', $id)->first();
|
|
if (!$notification) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Notification not found.'
|
|
], 404);
|
|
}
|
|
$notification->markAsRead();
|
|
} else {
|
|
$worker->unreadNotifications->markAsRead();
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Notification(s) marked as read successfully.'
|
|
], 200);
|
|
}
|
|
|
|
/**
|
|
* Delete a single notification or all notifications.
|
|
* DELETE /api/workers/notifications/{id} or DELETE /api/workers/notifications
|
|
*/
|
|
public function destroy(Request $request, $id = null)
|
|
{
|
|
/** @var Worker $worker */
|
|
$worker = $request->attributes->get('worker');
|
|
|
|
if ($id) {
|
|
$notification = $worker->notifications()->where('id', $id)->first();
|
|
if (!$notification) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Notification not found.'
|
|
], 404);
|
|
}
|
|
$notification->delete();
|
|
} else {
|
|
$worker->notifications()->delete();
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Notification(s) deleted successfully.'
|
|
], 200);
|
|
}
|
|
}
|