fix: prevent trial reset on device rebind

confirmBind and mockBind now check trial history before granting trial.
Previously only checked for active subscriptions — if trial expired,
rebinding would create a new 7-day trial, allowing infinite free usage.
Now checks if user ever had a trial (any status), skips if so.
这个提交包含在:
Guoguo
2026-05-20 02:25:51 -07:00
父节点 f1a2523cb4
当前提交 1a180ea07b
+18 -6
查看文件
@@ -95,17 +95,23 @@ async function confirmBind(userId, deviceId, bindToken) {
'UPDATE bindings SET bind_status = 1, bind_time = NOW() WHERE binding_id = ?',
[rows[0].binding_id]
)
// Ensure trial subscription
const [subs] = await conn.execute(
// Grant trial only if user has never had one before
const [activeSubs] = await conn.execute(
'SELECT subscription_id FROM subscriptions WHERE user_id = ? AND status = 1 AND expire_time > NOW() LIMIT 1',
[userId]
)
if (subs.length === 0) {
if (activeSubs.length === 0) {
const [trialHistory] = await conn.execute(
"SELECT subscription_id FROM subscriptions WHERE user_id = ? AND plan = 'trial' LIMIT 1",
[userId]
)
if (trialHistory.length === 0) {
await conn.execute(
"INSERT INTO subscriptions (user_id, plan, status, amount, start_time, expire_time) VALUES (?, 'trial', 1, 0, NOW(), DATE_ADD(NOW(), INTERVAL 7 DAY))",
[userId]
)
}
}
return true
})
}
@@ -136,17 +142,23 @@ async function mockBind(userId, deviceId) {
"INSERT INTO bindings (user_id, device_id, bind_token, bind_expires, bind_status, bind_time) VALUES (?, ?, 'mock', NOW(), 1, NOW())",
[userId, deviceId]
)
// Ensure trial
const [subs] = await conn.execute(
// Grant trial only if user has never had one before
const [activeSubs] = await conn.execute(
'SELECT subscription_id FROM subscriptions WHERE user_id = ? AND status = 1 AND expire_time > NOW() LIMIT 1',
[userId]
)
if (subs.length === 0) {
if (activeSubs.length === 0) {
const [trialHistory] = await conn.execute(
"SELECT subscription_id FROM subscriptions WHERE user_id = ? AND plan = 'trial' LIMIT 1",
[userId]
)
if (trialHistory.length === 0) {
await conn.execute(
"INSERT INTO subscriptions (user_id, plan, status, amount, start_time, expire_time) VALUES (?, 'trial', 1, 0, NOW(), DATE_ADD(NOW(), INTERVAL 7 DAY))",
[userId]
)
}
}
return { success: true }
})
}