Update codebase
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../connect.php';
|
||||
require_once __DIR__ . '/referral_code_helper.php';
|
||||
|
||||
// Use JWT token variables provided by connect.php
|
||||
if (!$user_id || $role != 'driver') {
|
||||
jsonError("Invalid parameters or unauthorized token");
|
||||
}
|
||||
|
||||
// 1. Get the driver's referral code
|
||||
$stmtCode = $con->prepare("SELECT referral_code FROM user_referral_codes WHERE user_id = ? AND user_type = 'driver'");
|
||||
$stmtCode->execute([$user_id]);
|
||||
// 1. Get the driver's referral code — ونُنشئه إن غاب.
|
||||
// الردّ بـ null كان يترك شاشة الدعوة تدور بلا كود إلى الأبد، لأن لا
|
||||
// شيء في التطبيق ينادي get_unified_code.php الذي يولّده.
|
||||
$referralCode = ensureReferralCode($con, (string) $user_id, 'driver');
|
||||
|
||||
if ($stmtCode->rowCount() == 0) {
|
||||
// If no code exists, return empty stats
|
||||
if ($referralCode === null) {
|
||||
printSuccess([
|
||||
"referral_code" => null,
|
||||
"total_invited_drivers" => 0,
|
||||
@@ -21,8 +22,6 @@ if ($stmtCode->rowCount() == 0) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$referralCode = $stmtCode->fetchColumn();
|
||||
|
||||
// 2. Fetch all referrals made by this code
|
||||
$stmtRefs = $con->prepare("
|
||||
SELECT id, invited_user_id, invited_user_type, status, trip_count, is_reward_claimed, created_at
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../connect.php';
|
||||
require_once __DIR__ . '/referral_code_helper.php';
|
||||
|
||||
// Use JWT token variables provided by connect.php
|
||||
if (!$user_id || $role != 'passenger') {
|
||||
@@ -8,12 +9,10 @@ if (!$user_id || $role != 'passenger') {
|
||||
|
||||
$country_code = $_POST['country_code'] ?? 'Jordan';
|
||||
|
||||
// 1. Get the passenger's referral code
|
||||
$stmtCode = $con->prepare("SELECT referral_code FROM user_referral_codes WHERE user_id = ? AND user_type = 'passenger'");
|
||||
$stmtCode->execute([$user_id]);
|
||||
// 1. Get the passenger's referral code — ونُنشئه إن غاب (نفس علّة السائق).
|
||||
$referralCode = ensureReferralCode($con, (string) $user_id, 'passenger');
|
||||
|
||||
if ($stmtCode->rowCount() == 0) {
|
||||
// If no code exists, return empty stats
|
||||
if ($referralCode === null) {
|
||||
printSuccess([
|
||||
"referral_code" => null,
|
||||
"total_invited_drivers" => 0,
|
||||
@@ -23,8 +22,6 @@ if ($stmtCode->rowCount() == 0) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$referralCode = $stmtCode->fetchColumn();
|
||||
|
||||
// 2. Fetch all referrals made by this code
|
||||
$stmtRefs = $con->prepare("
|
||||
SELECT id, invited_user_id, invited_user_type, status, trip_count, is_reward_claimed, created_at
|
||||
|
||||
@@ -1,42 +1,19 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../connect.php';
|
||||
|
||||
function generateUnifiedCode($con) {
|
||||
while (true) {
|
||||
$letters = substr(str_shuffle("ABCDEFGHJKLMNPQRSTUVWXYZ"), 0, 2);
|
||||
$numbers = substr(str_shuffle("23456789"), 0, 4);
|
||||
$code = $letters . $numbers;
|
||||
|
||||
$stmt = $con->prepare("SELECT COUNT(*) FROM user_referral_codes WHERE referral_code = ?");
|
||||
$stmt->execute([$code]);
|
||||
|
||||
if ($stmt->fetchColumn() == 0) {
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
}
|
||||
// منطق التوليد انتقل إلى helper مشترك حتى تولّده نقاط القراءة أيضاً —
|
||||
// هذه النقطة لم تكن مُستدعاة من أي شاشة، فبقي الكود بلا توليد.
|
||||
require_once __DIR__ . '/referral_code_helper.php';
|
||||
|
||||
// Ensure the JWT values exist
|
||||
if (!$user_id || !in_array($role, ['driver', 'passenger'])) {
|
||||
jsonError("Invalid or missing user information in token");
|
||||
}
|
||||
|
||||
$stmt = $con->prepare("SELECT referral_code FROM user_referral_codes WHERE user_id = ? AND user_type = ?");
|
||||
$stmt->execute([$user_id, $role]);
|
||||
$code = ensureReferralCode($con, (string) $user_id, (string) $role);
|
||||
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
printSuccess(["referral_code" => $row['referral_code']]);
|
||||
} else {
|
||||
$newCode = generateUnifiedCode($con);
|
||||
$insertStmt = $con->prepare("INSERT INTO user_referral_codes (user_id, user_type, referral_code) VALUES (?, ?, ?)");
|
||||
|
||||
try {
|
||||
$insertStmt->execute([$user_id, $role, $newCode]);
|
||||
printSuccess(["referral_code" => $newCode]);
|
||||
} catch (PDOException $e) {
|
||||
error_log("[get_unified_code.php] " . $e->getMessage());
|
||||
jsonError("An internal error occurred. Please try again later.");
|
||||
}
|
||||
if ($code === null) {
|
||||
jsonError("An internal error occurred. Please try again later.");
|
||||
}
|
||||
?>
|
||||
|
||||
printSuccess(["referral_code" => $code]);
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* referral_code_helper.php — توليد كود الدعوة عند أول قراءة
|
||||
* ─────────────────────────────────────────────────────────────
|
||||
* سبب وجود هذا الملف عطل قائم: `get_unified_code.php` وحده يولّد الكود،
|
||||
* وهو غير مُستدعى من أي شاشة في التطبيقين — الرابط معرّف في links.dart
|
||||
* ولا أحد يناديه. بينما `get_driver_referrals.php` و
|
||||
* `get_passenger_referrals.php` تقرآن فقط وتردّان null.
|
||||
*
|
||||
* النتيجة: صفّ لم يُكتب قط في `user_referral_codes`، فكود الدعوة null
|
||||
* دائماً وشاشة الدعوة تدور بلا نهاية عند السائق والراكب معاً.
|
||||
*
|
||||
* الإصلاح هنا لا في التطبيق: القراءة نفسها تُنشئ الكود إن غاب. هذا
|
||||
* يعالج التطبيقات المنشورة أصلاً بلا انتظار إصدار جديد.
|
||||
*/
|
||||
|
||||
if (!function_exists('generateUnifiedCode')) {
|
||||
function generateUnifiedCode(PDO $con): string
|
||||
{
|
||||
// حروف بلا I/O وأرقام بلا 0/1: الكود يُملى صوتياً بين السائقين.
|
||||
for ($attempt = 0; $attempt < 20; $attempt++) {
|
||||
$letters = substr(str_shuffle("ABCDEFGHJKLMNPQRSTUVWXYZ"), 0, 2);
|
||||
$numbers = substr(str_shuffle("23456789"), 0, 4);
|
||||
$code = $letters . $numbers;
|
||||
|
||||
$stmt = $con->prepare("SELECT COUNT(*) FROM user_referral_codes WHERE referral_code = ?");
|
||||
$stmt->execute([$code]);
|
||||
if ((int) $stmt->fetchColumn() === 0) {
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
// احتياط: حلقة while اللانهائية الأصلية كانت تُعلّق العامل كاملاً
|
||||
// لو امتلأت مساحة الأكواد. نسقط لكود أطول بدل التعليق.
|
||||
return substr(strtoupper(bin2hex(random_bytes(4))), 0, 8);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('ensureReferralCode')) {
|
||||
/**
|
||||
* يعيد كود الدعوة للمستخدم، ويُنشئه إن لم يكن موجوداً.
|
||||
* يعيد null فقط إذا تعذّرت الكتابة (لا يُسقط الطلب).
|
||||
*/
|
||||
function ensureReferralCode(PDO $con, string $userId, string $userType): ?string
|
||||
{
|
||||
$stmt = $con->prepare("SELECT referral_code FROM user_referral_codes WHERE user_id = ? AND user_type = ?");
|
||||
$stmt->execute([$userId, $userType]);
|
||||
$existing = $stmt->fetchColumn();
|
||||
if ($existing !== false && $existing !== null && $existing !== '') {
|
||||
return (string) $existing;
|
||||
}
|
||||
|
||||
try {
|
||||
$newCode = generateUnifiedCode($con);
|
||||
$con->prepare("INSERT INTO user_referral_codes (user_id, user_type, referral_code) VALUES (?, ?, ?)")
|
||||
->execute([$userId, $userType, $newCode]);
|
||||
return $newCode;
|
||||
} catch (PDOException $e) {
|
||||
// سباق بين طلبين متزامنين: الآخر كتب الصفّ أولاً — نقرأه.
|
||||
$stmt->execute([$userId, $userType]);
|
||||
$raced = $stmt->fetchColumn();
|
||||
if ($raced !== false && $raced !== null && $raced !== '') {
|
||||
return (string) $raced;
|
||||
}
|
||||
error_log("[referral_code_helper] " . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user