59 lines
2.0 KiB
PHP
59 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* متحكم الإشعارات (Notification Controller)
|
|
*
|
|
* الغرض من الملف:
|
|
* عرض وإدارة التنبيهات التاريخية التي وصلت للمستخدم (سائق أو راكب).
|
|
*
|
|
* كيفية العمل:
|
|
* 1. يجلب قائمة الإشعارات من جداول (notifications) أو (notificationCaptain).
|
|
* 2. يسمح بتحديد الإشعارات كـ "مقروءة".
|
|
*/
|
|
class NotificationController extends Controller
|
|
{
|
|
/** GET /v2/notifications */
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$userId = $request->input('_jwt_user_id');
|
|
$userType = $request->input('_jwt_user_type');
|
|
$page = (int) $request->input('page', 1);
|
|
$limit = min((int) $request->input('limit', 20), 50);
|
|
|
|
if ($userType === 'driver') {
|
|
$notifications = DB::connection('primary')->table('notificationCaptain')
|
|
->where('driverID', $userId)
|
|
->orderBy('dateCreated', 'desc')
|
|
->skip(($page - 1) * $limit)->take($limit)
|
|
->get();
|
|
} else {
|
|
$notifications = DB::connection('primary')->table('notifications')
|
|
->where('passenger_id', $userId)
|
|
->orderBy('created_at', 'desc')
|
|
->skip(($page - 1) * $limit)->take($limit)
|
|
->get();
|
|
}
|
|
|
|
return response()->json(['status' => 'success', 'data' => $notifications]);
|
|
}
|
|
|
|
/** PUT /v2/notifications/{id}/read */
|
|
public function markRead(Request $request, int $id): JsonResponse
|
|
{
|
|
$userType = $request->input('_jwt_user_type');
|
|
$table = $userType === 'driver' ? 'notificationCaptain' : 'notifications';
|
|
|
|
DB::connection('primary')->table($table)
|
|
->where('id', $id)
|
|
->update(['isShown' => 'true']);
|
|
|
|
return response()->json(['status' => 'success']);
|
|
}
|
|
}
|