Convert billing to 1-to-100 credits
This commit is contained in:
+21
-6
@@ -6,6 +6,8 @@ const {
|
||||
listModelPrices,
|
||||
loadPriceCache,
|
||||
creditUserBalance,
|
||||
creditsToCreditUnits,
|
||||
formatCreditsFromCents,
|
||||
pool,
|
||||
validateUsername,
|
||||
validatePassword,
|
||||
@@ -156,14 +158,18 @@ function registerAdminRoutes(router) {
|
||||
|
||||
router.post("/admin/users/:id/credit", requireAuth, requireAdmin, async (req, res) => {
|
||||
const targetUserId = Number(req.params.id);
|
||||
const { amountCents } = req.body;
|
||||
if (!amountCents || amountCents <= 0) return res.status(400).json({ error: "积分必须大于 0" });
|
||||
const { amountCredits, amountCents } = req.body;
|
||||
const creditUnits =
|
||||
amountCredits !== undefined && amountCredits !== null && amountCredits !== ""
|
||||
? creditsToCreditUnits(amountCredits)
|
||||
: Number(amountCents);
|
||||
if (!creditUnits || creditUnits <= 0) return res.status(400).json({ error: "积分必须大于 0" });
|
||||
|
||||
try {
|
||||
const newBalance = await creditUserBalance(
|
||||
targetUserId,
|
||||
amountCents,
|
||||
`管理员 ${req.user.username} 发放 ${Math.floor(amountCents / 100)} 积分`,
|
||||
creditUnits,
|
||||
`管理员 ${req.user.username} 发放 ${formatCreditsFromCents(creditUnits)} 积分`,
|
||||
);
|
||||
res.json({ success: true, newBalanceCents: newBalance });
|
||||
} catch (err) {
|
||||
@@ -547,6 +553,8 @@ function registerAdminRoutes(router) {
|
||||
name,
|
||||
description = "",
|
||||
priceCents,
|
||||
credits,
|
||||
amountCredits,
|
||||
creditsCents = 0,
|
||||
imageQuota = 0,
|
||||
videoQuota = 0,
|
||||
@@ -572,7 +580,9 @@ function registerAdminRoutes(router) {
|
||||
name,
|
||||
description,
|
||||
Number(priceCents),
|
||||
Number(creditsCents || 0),
|
||||
credits !== undefined || amountCredits !== undefined
|
||||
? creditsToCreditUnits(credits ?? amountCredits)
|
||||
: Number(creditsCents || 0),
|
||||
Number(imageQuota || 0),
|
||||
Number(videoQuota || 0),
|
||||
Number(textQuota || 0),
|
||||
@@ -599,6 +609,8 @@ function registerAdminRoutes(router) {
|
||||
name,
|
||||
description,
|
||||
priceCents,
|
||||
credits,
|
||||
amountCredits,
|
||||
creditsCents,
|
||||
imageQuota,
|
||||
videoQuota,
|
||||
@@ -623,7 +635,10 @@ function registerAdminRoutes(router) {
|
||||
updates.push(`price_cents = $${idx++}`);
|
||||
params.push(Number(priceCents));
|
||||
}
|
||||
if (creditsCents !== undefined) {
|
||||
if (credits !== undefined || amountCredits !== undefined) {
|
||||
updates.push(`credits_cents = $${idx++}`);
|
||||
params.push(creditsToCreditUnits(credits ?? amountCredits));
|
||||
} else if (creditsCents !== undefined) {
|
||||
updates.push(`credits_cents = $${idx++}`);
|
||||
params.push(Number(creditsCents));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user