Fix: update destination limits to 3 and sync with Redis

This commit is contained in:
Hamza-Ayed
2026-06-27 17:49:03 +03:00
parent 5fed555e44
commit 43e3f8c939
13 changed files with 221 additions and 130 deletions

View File

@@ -19,6 +19,37 @@ $destLat = filterRequest('destination_lat') ?? filterRequest('target_latitude')
$destLng = filterRequest('destination_lng') ?? filterRequest('target_longitude');
$destName = filterRequest('destination_name') ?? 'Destination';
function notifySocketServerDestination($userId, $hasDestination, $destLat = '', $destLng = '', $destName = '') {
$url = getenv('LOCATION_SOCKET_URL');
$internalKey = function_exists('getInternalSocketKey') ? getInternalSocketKey() : '';
if (empty($url)) {
error_log("[save_driver_destination] LOCATION_SOCKET_URL env variable not set");
return;
}
$postData = [
'action' => 'update_driver_destination',
'driver_id' => (string)$userId,
'has_destination' => (int)$hasDestination,
'destination_lat' => (string)$destLat,
'destination_lng' => (string)$destLng,
'destination_name' => (string)$destName,
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 400);
if ($internalKey) {
curl_setopt($ch, CURLOPT_HTTPHEADER, ["x-internal-key: $internalKey"]);
}
curl_exec($ch);
curl_close($ch);
}
try {
if ($action === 'get') {
$stmtGet = $con->prepare("
@@ -47,6 +78,10 @@ try {
AND is_active = 1
");
$stmtDeactivate->execute([':did' => $user_id]);
// Sync with Redis on Socket Server
notifySocketServerDestination($user_id, 0);
jsonSuccess(null, "تم إلغاء تفعيل الوجهة الشخصية بنجاح.");
exit;
}
@@ -56,19 +91,34 @@ try {
jsonError("Missing required parameters: destination_lat and destination_lng are required.");
}
// 3. Enforce Limit: Max 2 times daily
// 3. Enforce Limit: Max 3 times daily
// Check local Redis rate limit first
if (isset($redis)) {
$redisKey = "driver:dest_count:" . $user_id;
$redisCount = intval($redis->get($redisKey));
if ($redisCount >= 3) {
jsonError("حسناً كابتن، لقد وصلت للحد الأقصى المسموح به لتحديد الوجهة اليوم (3 مرات في اليوم).");
}
}
// Check MySQL database limit
$stmtCount = $con->prepare("
SELECT COUNT(*)
FROM driver_destinations
WHERE driver_id = :did
AND usage_date = CURDATE()
AND is_active = 1
AND usage_date = CURDATE()
");
$stmtCount->execute([':did' => $user_id]);
$dailyCount = intval($stmtCount->fetchColumn());
if ($dailyCount >= 2) {
jsonError("حسناً كابتن، لقد وصلت للحد الأقصى المسموح به لتحديد الوجهة اليوم (مرتان في اليوم).");
if ($dailyCount >= 3) {
// Sync Redis counter just in case
if (isset($redis)) {
$redisKey = "driver:dest_count:" . $user_id;
$redis->set($redisKey, $dailyCount);
$redis->expire($redisKey, 86400); // 24 hours TTL
}
jsonError("حسناً كابتن، لقد وصلت للحد الأقصى المسموح به لتحديد الوجهة اليوم (3 مرات في اليوم).");
}
// 4. Deactivate previous active destinations for this driver
@@ -93,6 +143,16 @@ try {
':name' => $destName
]);
// Sync with Redis on Socket Server
notifySocketServerDestination($user_id, 1, $destLat, $destLng, $destName);
// Increment local Redis counter
if (isset($redis)) {
$redisKey = "driver:dest_count:" . $user_id;
$redis->incr($redisKey);
$redis->expire($redisKey, 86400); // 24 hours TTL
}
jsonSuccess(null, "تم حفظ وجهتك كابتن بنجاح! سيتم توجيه الطلبات المطابقة لوجهتك.");
} catch (Exception $e) {