feat: add local auth billing and usage management
This commit is contained in:
1 parent
f642b5e71f
commit
196fdde83f
119 files changed
+15695
-2650
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
|
||||
import { requireSuperAdminSession } from "@/lib/server/auth/current-user";
|
||||
import { getBillingPriceRule, updateBillingPriceRule, updateBillingPriceTierMultiplier } from "@/lib/server/billing-store";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function PATCH(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
await requireSuperAdminSession();
|
||||
const { id } = await context.params;
|
||||
const body = await readJsonBody<Record<string, unknown>>(request);
|
||||
const unsupportedFields = Object.keys(body).filter((key) => !["markupMultiplier", "dimensionKey", "tierValue"].includes(key));
|
||||
if (unsupportedFields.length) throw badRequest("平台标准价格与参数由系统维护,超管仅可调整上浮倍率。");
|
||||
const multiplier = Number(body.markupMultiplier);
|
||||
if (!Number.isFinite(multiplier) || multiplier < 1 || multiplier > 1000) {
|
||||
throw badRequest("上浮倍率必须在 1.00 至 1000.00 之间。");
|
||||
}
|
||||
const dimensionKey = typeof body.dimensionKey === "string" ? body.dimensionKey.trim() : "";
|
||||
const tierValue = typeof body.tierValue === "string" ? body.tierValue : "";
|
||||
if ((dimensionKey && !tierValue) || (!dimensionKey && tierValue)) {
|
||||
throw badRequest("参数档位倍率更新必须同时提供参数维度和档位。");
|
||||
}
|
||||
const existing = await getBillingPriceRule(id);
|
||||
if (!existing) return jsonError("计费规则不存在", 404);
|
||||
if (!dimensionKey && existing.parameterDimensions?.length) {
|
||||
throw badRequest("当前服务包含参数档位,请指定要调整的参数档位倍率。");
|
||||
}
|
||||
const rule = dimensionKey || tierValue
|
||||
? await updateBillingPriceTierMultiplier({
|
||||
ruleId: id,
|
||||
dimensionKey,
|
||||
tierValue,
|
||||
markupMultiplier: Number(multiplier.toFixed(4))
|
||||
})
|
||||
: await updateBillingPriceRule(id, { markupMultiplier: Number(multiplier.toFixed(4)) });
|
||||
if (!rule) return jsonError("计费规则不存在", 404);
|
||||
return jsonOk({ rule });
|
||||
} catch (error) {
|
||||
return jsonError(error, 500, { request, source: "api.admin.billing.prices.updateMultiplier" });
|
||||
}
|
||||
}
|
||||
|
||||
function badRequest(message: string): Error & { status: number } {
|
||||
return Object.assign(new Error(message), { status: 400 });
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { jsonError, jsonOk } from "@/lib/server/api";
|
||||
import { requireSuperAdminSession } from "@/lib/server/auth/current-user";
|
||||
import { listBillingPriceRules } from "@/lib/server/billing-store";
|
||||
import { ensureDefaultBillingPriceRules } from "@/lib/server/billing-catalog";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
await requireSuperAdminSession();
|
||||
await ensureDefaultBillingPriceRules();
|
||||
return jsonOk({ priceRules: await listBillingPriceRules({ includeDisabled: true }) });
|
||||
} catch (error) {
|
||||
return jsonError(error, 500, { source: "api.admin.billing.prices" });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user