Update artifacts/api-server/src/routes/me/index.ts
Browse files
artifacts/api-server/src/routes/me/index.ts
CHANGED
|
@@ -51,41 +51,67 @@ router.get("/", async (req: Request, res: Response) => {
|
|
| 51 |
try {
|
| 52 |
// ========== 🆕 البحث عن مستخدم قديم بنفس البريد ==========
|
| 53 |
if (userEmail) {
|
| 54 |
-
const
|
| 55 |
-
.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
.
|
|
|
|
|
|
|
| 60 |
|
| 61 |
if (legacyUser) {
|
| 62 |
-
console.log(`🔗 ربط
|
| 63 |
-
|
| 64 |
-
// ت
|
| 65 |
-
await supabase
|
| 66 |
-
.from('users')
|
| 67 |
-
.update({
|
| 68 |
-
supabase_id: userId,
|
| 69 |
-
updated_at: now.toISOString()
|
| 70 |
-
})
|
| 71 |
-
.eq('id', legacyUser.id);
|
| 72 |
-
|
| 73 |
-
// 🎯 استخدم اشتراك المستخدم القديم
|
| 74 |
const [existingSub] = await db
|
| 75 |
.select()
|
| 76 |
.from(subscriptionsTable)
|
| 77 |
.where(eq(subscriptionsTable.userId, legacyUser.id));
|
| 78 |
|
| 79 |
if (existingSub) {
|
| 80 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
await db
|
| 82 |
-
.
|
| 83 |
-
.
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
return res.json({
|
| 91 |
userId, email: userEmail,
|
|
@@ -96,7 +122,7 @@ router.get("/", async (req: Request, res: Response) => {
|
|
| 96 |
paypalSubscriptionId: null,
|
| 97 |
hasAccess
|
| 98 |
},
|
| 99 |
-
isReturningUser: true
|
| 100 |
});
|
| 101 |
}
|
| 102 |
}
|
|
@@ -124,8 +150,14 @@ router.get("/", async (req: Request, res: Response) => {
|
|
| 124 |
}
|
| 125 |
|
| 126 |
let status: string = sub?.status ?? "none";
|
| 127 |
-
let hasAccess =
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
return res.json({
|
| 131 |
userId, email: userEmail,
|
|
@@ -136,9 +168,10 @@ router.get("/", async (req: Request, res: Response) => {
|
|
| 136 |
paypalSubscriptionId: null,
|
| 137 |
hasAccess
|
| 138 |
},
|
| 139 |
-
isReturningUser: false
|
| 140 |
});
|
| 141 |
} catch (error) {
|
|
|
|
| 142 |
return res.json({
|
| 143 |
userId, email: userEmail,
|
| 144 |
subscription: { status: "trialing", trialEnd: buildTrialEnd().toISOString(), currentPeriodEnd: null, paypalSubscriptionId: null, hasAccess: true }
|
|
|
|
| 51 |
try {
|
| 52 |
// ========== 🆕 البحث عن مستخدم قديم بنفس البريد ==========
|
| 53 |
if (userEmail) {
|
| 54 |
+
const [legacyUser] = await db
|
| 55 |
+
.select({
|
| 56 |
+
id: usersTable.id,
|
| 57 |
+
email: usersTable.email,
|
| 58 |
+
})
|
| 59 |
+
.from(usersTable)
|
| 60 |
+
.where(eq(usersTable.email, userEmail.toLowerCase()))
|
| 61 |
+
.limit(1);
|
| 62 |
|
| 63 |
if (legacyUser) {
|
| 64 |
+
console.log(`🔗 ربط: ${userEmail} | القديم=${legacyUser.id} | الجديد=${userId}`);
|
| 65 |
+
|
| 66 |
+
// جلب اشتراك المستخدم القديم
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
const [existingSub] = await db
|
| 68 |
.select()
|
| 69 |
.from(subscriptionsTable)
|
| 70 |
.where(eq(subscriptionsTable.userId, legacyUser.id));
|
| 71 |
|
| 72 |
if (existingSub) {
|
| 73 |
+
// إذا كان userId مختلف، نحذف الاشتراك الجديد أولاً ثم ننقل القديم
|
| 74 |
+
if (legacyUser.id !== userId) {
|
| 75 |
+
// حذف الاشتراك الجديد (التجريبي الذي أنشئ عند أول دخول)
|
| 76 |
+
await db
|
| 77 |
+
.delete(subscriptionsTable)
|
| 78 |
+
.where(eq(subscriptionsTable.userId, userId));
|
| 79 |
+
|
| 80 |
+
// نقل الاشتراك القديم للمستخدم الجديد
|
| 81 |
+
await db
|
| 82 |
+
.update(subscriptionsTable)
|
| 83 |
+
.set({ userId })
|
| 84 |
+
.where(eq(subscriptionsTable.userId, legacyUser.id));
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// تحديث بيانات المستخدم
|
| 88 |
await db
|
| 89 |
+
.insert(usersTable)
|
| 90 |
+
.values({
|
| 91 |
+
id: userId,
|
| 92 |
+
email: userEmail.toLowerCase(),
|
| 93 |
+
isBanned: false,
|
| 94 |
+
lastSeenAt: now
|
| 95 |
+
})
|
| 96 |
+
.onConflictDoUpdate({
|
| 97 |
+
target: usersTable.id,
|
| 98 |
+
set: {
|
| 99 |
+
email: userEmail.toLowerCase(),
|
| 100 |
+
lastSeenAt: now,
|
| 101 |
+
},
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
let status: string = existingSub?.status ?? "none";
|
| 105 |
+
let hasAccess = false;
|
| 106 |
+
|
| 107 |
+
if (status === "trialing") {
|
| 108 |
+
hasAccess = existingSub.trialEnd ? existingSub.trialEnd > now : false;
|
| 109 |
+
if (!hasAccess) status = "trial_expired";
|
| 110 |
+
} else if (status === "trial_expired" || status === "expired") {
|
| 111 |
+
hasAccess = false;
|
| 112 |
+
} else if (status === "active") {
|
| 113 |
+
hasAccess = !existingSub.currentPeriodEnd || existingSub.currentPeriodEnd > now;
|
| 114 |
+
}
|
| 115 |
|
| 116 |
return res.json({
|
| 117 |
userId, email: userEmail,
|
|
|
|
| 122 |
paypalSubscriptionId: null,
|
| 123 |
hasAccess
|
| 124 |
},
|
| 125 |
+
isReturningUser: true,
|
| 126 |
});
|
| 127 |
}
|
| 128 |
}
|
|
|
|
| 150 |
}
|
| 151 |
|
| 152 |
let status: string = sub?.status ?? "none";
|
| 153 |
+
let hasAccess = false;
|
| 154 |
+
|
| 155 |
+
if (status === "trialing") {
|
| 156 |
+
hasAccess = sub?.trialEnd ? new Date(sub.trialEnd) > now : false;
|
| 157 |
+
if (!hasAccess) status = "trial_expired";
|
| 158 |
+
} else if (status === "active") {
|
| 159 |
+
hasAccess = !sub?.currentPeriodEnd || new Date(sub.currentPeriodEnd) > now;
|
| 160 |
+
}
|
| 161 |
|
| 162 |
return res.json({
|
| 163 |
userId, email: userEmail,
|
|
|
|
| 168 |
paypalSubscriptionId: null,
|
| 169 |
hasAccess
|
| 170 |
},
|
| 171 |
+
isReturningUser: false,
|
| 172 |
});
|
| 173 |
} catch (error) {
|
| 174 |
+
console.error("[me] خطأ:", error);
|
| 175 |
return res.json({
|
| 176 |
userId, email: userEmail,
|
| 177 |
subscription: { status: "trialing", trialEnd: buildTrialEnd().toISOString(), currentPeriodEnd: null, paypalSubscriptionId: null, hasAccess: true }
|