| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- /**
- * dev-credit.mjs — 开发环境用户余额充值脚本(仅限开发/测试使用,⚠️ 不可用于生产)
- *
- * 用法:
- * node apps/api/scripts/dev-credit.mjs <email> <amount_cents>
- *
- * 示例:
- * node apps/api/scripts/dev-credit.mjs test@dotouch.ai 100000
- * # 为用户 test@dotouch.ai 充值 100000 美分($1000.00)
- */
- import { PrismaClient } from '@prisma/client';
- const email = process.argv[2];
- const amountCents = parseInt(process.argv[3], 10);
- if (!email || isNaN(amountCents) || amountCents <= 0) {
- console.error('用法: node dev-credit.mjs <email> <amount_cents>');
- console.error('示例: node dev-credit.mjs test@dotouch.ai 100000');
- process.exit(1);
- }
- const prisma = new PrismaClient();
- async function main() {
- const user = await prisma.user.findUnique({ where: { email } });
- if (!user) {
- console.error(`错误: 未找到用户 ${email}`);
- await prisma.$disconnect();
- process.exit(1);
- }
- const result = await prisma.$transaction(async (tx) => {
- const updated = await tx.user.update({
- where: { id: user.id },
- data: { balanceCents: { increment: BigInt(amountCents) } },
- select: { balanceCents: true },
- });
- await tx.balanceTransaction.create({
- data: {
- userId: user.id,
- amountCents: BigInt(amountCents),
- type: 'topup',
- refId: `dev_credit_${Date.now()}`,
- },
- });
- return updated;
- });
- console.log(
- `✅ 已为用户 ${email} 充值 ${amountCents} 美分。当前余额: ${result.balanceCents} 美分`,
- );
- }
- main()
- .catch((err) => {
- console.error('充值失败:', err);
- process.exit(1);
- })
- .finally(() => prisma.$disconnect());
|