feat(billing): record subscription-covered token usage in usage records
Memind CI / Test, build, and release guards (pull_request) Successful in 4m8s
Memind CI / Test, build, and release guards (pull_request) Successful in 4m8s
Write h5_usage_records when plan quota fully covers a session bill so WeChat and H5 consumption stays visible in admin usage history. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+30
-6
@@ -1442,10 +1442,12 @@ export function createUserAuth(pool, options = {}) {
|
||||
);
|
||||
|
||||
// Subscription quota check: consume tokens from active plan before touching balance.
|
||||
let subscriptionCovered = false;
|
||||
if (costCents > 0 && subscriptionService) {
|
||||
const coverage = await subscriptionService.consumeQuota(userId, deltaTokens, conn);
|
||||
if (coverage.fullyCovers) {
|
||||
costCents = 0;
|
||||
subscriptionCovered = true;
|
||||
} else if (coverage.overageRate < 1.0) {
|
||||
costCents = Math.max(1, Math.ceil(costCents * coverage.overageRate));
|
||||
}
|
||||
@@ -1482,8 +1484,8 @@ export function createUserAuth(pool, options = {}) {
|
||||
|
||||
await conn.query(
|
||||
`INSERT INTO h5_usage_records
|
||||
(user_id, agent_session_id, request_id, input_tokens, output_tokens, cost_cents, balance_after_cents, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
(user_id, agent_session_id, request_id, input_tokens, output_tokens, cost_cents, balance_after_cents, billing_source, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 'wallet', ?)`,
|
||||
[
|
||||
userId,
|
||||
agentSessionId,
|
||||
@@ -1524,6 +1526,28 @@ export function createUserAuth(pool, options = {}) {
|
||||
userId,
|
||||
]);
|
||||
}
|
||||
} else if (subscriptionCovered && deltaTokens > 0) {
|
||||
const [walletRows] = await conn.query(
|
||||
`SELECT balance_cents, tokens_used FROM h5_user_wallets WHERE user_id = ?`,
|
||||
[userId],
|
||||
);
|
||||
balanceAfter = walletRows[0] ? Number(walletRows[0].balance_cents) : 0;
|
||||
tokensUsedAfter = walletRows[0] ? Number(walletRows[0].tokens_used ?? 0) : null;
|
||||
|
||||
await conn.query(
|
||||
`INSERT INTO h5_usage_records
|
||||
(user_id, agent_session_id, request_id, input_tokens, output_tokens, cost_cents, balance_after_cents, billing_source, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, 0, ?, 'subscription', ?)`,
|
||||
[
|
||||
userId,
|
||||
agentSessionId,
|
||||
normalizedRequestId,
|
||||
deltaIn,
|
||||
deltaOut,
|
||||
balanceAfter,
|
||||
now,
|
||||
],
|
||||
);
|
||||
} else {
|
||||
const user = await getUserById(userId);
|
||||
balanceAfter = user ? Number(user.balance_cents) : null;
|
||||
@@ -1556,12 +1580,12 @@ export function createUserAuth(pool, options = {}) {
|
||||
if (userId) params.push(userId);
|
||||
const [rows] = await pool.query(
|
||||
`SELECT r.id, r.user_id, u.username, r.agent_session_id, r.request_id,
|
||||
r.input_tokens, r.output_tokens, r.cost_cents, r.balance_after_cents, r.created_at
|
||||
r.input_tokens, r.output_tokens, r.cost_cents, r.balance_after_cents, r.billing_source, r.created_at
|
||||
FROM h5_usage_records r JOIN h5_users u ON u.id = r.user_id
|
||||
${where} ORDER BY r.created_at DESC LIMIT ${safeLimit}`,
|
||||
params,
|
||||
);
|
||||
return rows.map((row) => ({ id: Number(row.id), userId: row.user_id, username: row.username, agentSessionId: row.agent_session_id, requestId: row.request_id, inputTokens: Number(row.input_tokens), outputTokens: Number(row.output_tokens), costCents: Number(row.cost_cents), balanceAfterCents: Number(row.balance_after_cents), createdAt: Number(row.created_at) }));
|
||||
return rows.map((row) => ({ id: Number(row.id), userId: row.user_id, username: row.username, agentSessionId: row.agent_session_id, requestId: row.request_id, inputTokens: Number(row.input_tokens), outputTokens: Number(row.output_tokens), costCents: Number(row.cost_cents), balanceAfterCents: Number(row.balance_after_cents), billingSource: row.billing_source ?? 'wallet', createdAt: Number(row.created_at) }));
|
||||
}
|
||||
const safePageSize = Math.min(Math.max(Number(pageSize) || 50, 1), 200);
|
||||
const safePage = Math.max(Number(page) || 1, 1);
|
||||
@@ -1575,7 +1599,7 @@ export function createUserAuth(pool, options = {}) {
|
||||
);
|
||||
const [rows] = await pool.query(
|
||||
`SELECT r.id, r.user_id, u.username, r.agent_session_id, r.request_id,
|
||||
r.input_tokens, r.output_tokens, r.cost_cents, r.balance_after_cents, r.created_at
|
||||
r.input_tokens, r.output_tokens, r.cost_cents, r.balance_after_cents, r.billing_source, r.created_at
|
||||
FROM h5_usage_records r JOIN h5_users u ON u.id = r.user_id
|
||||
${where}
|
||||
ORDER BY r.created_at DESC
|
||||
@@ -1583,7 +1607,7 @@ export function createUserAuth(pool, options = {}) {
|
||||
params,
|
||||
);
|
||||
return {
|
||||
records: rows.map((row) => ({ id: Number(row.id), userId: row.user_id, username: row.username, agentSessionId: row.agent_session_id, requestId: row.request_id, inputTokens: Number(row.input_tokens), outputTokens: Number(row.output_tokens), costCents: Number(row.cost_cents), balanceAfterCents: Number(row.balance_after_cents), createdAt: Number(row.created_at) })),
|
||||
records: rows.map((row) => ({ id: Number(row.id), userId: row.user_id, username: row.username, agentSessionId: row.agent_session_id, requestId: row.request_id, inputTokens: Number(row.input_tokens), outputTokens: Number(row.output_tokens), costCents: Number(row.cost_cents), balanceAfterCents: Number(row.balance_after_cents), billingSource: row.billing_source ?? 'wallet', createdAt: Number(row.created_at) })),
|
||||
total: Number(total),
|
||||
page: safePage,
|
||||
pageSize: safePageSize,
|
||||
|
||||
Reference in New Issue
Block a user