167 lines
5.9 KiB
PHP
167 lines
5.9 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->attributes->get('_jwt_user_id');
|
|
$userType = $request->attributes->get('_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', 'message' => $notifications]);
|
|
}
|
|
|
|
/** PUT /v2/notifications/{id}/read */
|
|
public function markRead(Request $request, int $id): JsonResponse
|
|
{
|
|
$userType = $request->attributes->get('_jwt_user_type');
|
|
$table = $userType === 'driver' ? 'notificationCaptain' : 'notifications';
|
|
|
|
$userId = $request->attributes->get('_jwt_user_id');
|
|
$userField = $userType === 'driver' ? 'driverID' : 'passenger_id';
|
|
|
|
DB::connection('primary')->table($table)
|
|
->where('id', $id)
|
|
->where($userField, $userId)
|
|
->update(['isShown' => 'true']);
|
|
|
|
return response()->json(['status' => 'success']);
|
|
}
|
|
|
|
/** POST /v2/notifications/update (For V1 Compatibility) */
|
|
public function updateNotification(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$id = $request->input('id');
|
|
if (!$id) {
|
|
return response()->json(['status' => 'failure', 'message' => 'Missing notification ID']);
|
|
}
|
|
|
|
$isShown = $request->input('isShown', 'true');
|
|
$userId = $request->attributes->get('_jwt_user_id');
|
|
$userType = $request->attributes->get('_jwt_user_type');
|
|
|
|
$table = $userType === 'driver' ? 'notificationCaptain' : 'notifications';
|
|
$userField = $userType === 'driver' ? 'driverID' : 'passenger_id';
|
|
|
|
$affected = DB::connection('primary')->table($table)
|
|
->where('id', $id)
|
|
->where($userField, $userId)
|
|
->update(['isShown' => $isShown]);
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'affected' => $affected
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
\Log::error('NotificationController Update Error: ' . $e->getMessage());
|
|
return response()->json([
|
|
'status' => 'failure',
|
|
'message' => 'Internal server error occurred'
|
|
], 500);
|
|
}
|
|
}
|
|
/** POST /v2/notifications/token */
|
|
public function updateToken(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'token' => 'required|string',
|
|
'fingerPrint' => 'required|string',
|
|
]);
|
|
|
|
$userId = $request->attributes->get('_jwt_user_id') ?? $request->input('passengerID');
|
|
$userType = $request->attributes->get('_jwt_user_type') ?? 'passenger';
|
|
|
|
if (!$userId) {
|
|
return response()->json(['status' => 'failure', 'message' => 'User ID missing'], 400);
|
|
}
|
|
|
|
if ($userType === 'driver') {
|
|
DB::connection('primary')->table('driverToken')
|
|
->updateOrInsert(
|
|
['captain_id' => $userId],
|
|
[
|
|
'token' => $request->input('token'),
|
|
'fingerPrint' => $request->input('fingerPrint'),
|
|
]
|
|
);
|
|
} else {
|
|
DB::connection('primary')->table('tokens')
|
|
->updateOrInsert(
|
|
['passengerID' => $userId],
|
|
[
|
|
'token' => $request->input('token'),
|
|
'fingerPrint' => $request->input('fingerPrint'),
|
|
]
|
|
);
|
|
}
|
|
|
|
return response()->json(['status' => 'success']);
|
|
}
|
|
|
|
/** GET /v2/notifications/token */
|
|
public function getToken(Request $request): JsonResponse
|
|
{
|
|
$userId = $request->attributes->get('_jwt_user_id') ?? $request->input('passengerID');
|
|
$userType = $request->attributes->get('_jwt_user_type') ?? 'passenger';
|
|
|
|
if (!$userId) {
|
|
return response()->json(['status' => 'failure', 'message' => 'User ID missing'], 400);
|
|
}
|
|
|
|
if ($userType === 'driver') {
|
|
$data = DB::connection('primary')->table('driverToken')
|
|
->where('captain_id', $userId)
|
|
->first();
|
|
} else {
|
|
$data = DB::connection('primary')->table('tokens')
|
|
->where('passengerID', $userId)
|
|
->first();
|
|
}
|
|
|
|
if (!$data) {
|
|
return response()->json(['status' => 'failure', 'message' => 'No token found'], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => [
|
|
'token' => $data->token,
|
|
'fingerPrint' => $data->fingerPrint ?? null,
|
|
]
|
|
]);
|
|
}
|
|
}
|