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.
这个提交包含在:
+22
-10
@@ -95,16 +95,22 @@ async function confirmBind(userId, deviceId, bindToken) {
|
|||||||
'UPDATE bindings SET bind_status = 1, bind_time = NOW() WHERE binding_id = ?',
|
'UPDATE bindings SET bind_status = 1, bind_time = NOW() WHERE binding_id = ?',
|
||||||
[rows[0].binding_id]
|
[rows[0].binding_id]
|
||||||
)
|
)
|
||||||
// Ensure trial subscription
|
// Grant trial only if user has never had one before
|
||||||
const [subs] = await conn.execute(
|
const [activeSubs] = await conn.execute(
|
||||||
'SELECT subscription_id FROM subscriptions WHERE user_id = ? AND status = 1 AND expire_time > NOW() LIMIT 1',
|
'SELECT subscription_id FROM subscriptions WHERE user_id = ? AND status = 1 AND expire_time > NOW() LIMIT 1',
|
||||||
[userId]
|
[userId]
|
||||||
)
|
)
|
||||||
if (subs.length === 0) {
|
if (activeSubs.length === 0) {
|
||||||
await conn.execute(
|
const [trialHistory] = 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))",
|
"SELECT subscription_id FROM subscriptions WHERE user_id = ? AND plan = 'trial' LIMIT 1",
|
||||||
[userId]
|
[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
|
return true
|
||||||
})
|
})
|
||||||
@@ -136,16 +142,22 @@ 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())",
|
"INSERT INTO bindings (user_id, device_id, bind_token, bind_expires, bind_status, bind_time) VALUES (?, ?, 'mock', NOW(), 1, NOW())",
|
||||||
[userId, deviceId]
|
[userId, deviceId]
|
||||||
)
|
)
|
||||||
// Ensure trial
|
// Grant trial only if user has never had one before
|
||||||
const [subs] = await conn.execute(
|
const [activeSubs] = await conn.execute(
|
||||||
'SELECT subscription_id FROM subscriptions WHERE user_id = ? AND status = 1 AND expire_time > NOW() LIMIT 1',
|
'SELECT subscription_id FROM subscriptions WHERE user_id = ? AND status = 1 AND expire_time > NOW() LIMIT 1',
|
||||||
[userId]
|
[userId]
|
||||||
)
|
)
|
||||||
if (subs.length === 0) {
|
if (activeSubs.length === 0) {
|
||||||
await conn.execute(
|
const [trialHistory] = 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))",
|
"SELECT subscription_id FROM subscriptions WHERE user_id = ? AND plan = 'trial' LIMIT 1",
|
||||||
[userId]
|
[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 }
|
return { success: true }
|
||||||
})
|
})
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户