dev-credit.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * dev-credit.mjs — 开发环境用户余额充值脚本(仅限开发/测试使用,⚠️ 不可用于生产)
  3. *
  4. * 用法:
  5. * node apps/api/scripts/dev-credit.mjs <email> <amount_cents>
  6. *
  7. * 示例:
  8. * node apps/api/scripts/dev-credit.mjs test@dotouch.ai 100000
  9. * # 为用户 test@dotouch.ai 充值 100000 美分($1000.00)
  10. */
  11. import { PrismaClient } from '@prisma/client';
  12. const email = process.argv[2];
  13. const amountCents = parseInt(process.argv[3], 10);
  14. if (!email || isNaN(amountCents) || amountCents <= 0) {
  15. console.error('用法: node dev-credit.mjs <email> <amount_cents>');
  16. console.error('示例: node dev-credit.mjs test@dotouch.ai 100000');
  17. process.exit(1);
  18. }
  19. const prisma = new PrismaClient();
  20. async function main() {
  21. const user = await prisma.user.findUnique({ where: { email } });
  22. if (!user) {
  23. console.error(`错误: 未找到用户 ${email}`);
  24. await prisma.$disconnect();
  25. process.exit(1);
  26. }
  27. const result = await prisma.$transaction(async (tx) => {
  28. const updated = await tx.user.update({
  29. where: { id: user.id },
  30. data: { balanceCents: { increment: BigInt(amountCents) } },
  31. select: { balanceCents: true },
  32. });
  33. await tx.balanceTransaction.create({
  34. data: {
  35. userId: user.id,
  36. amountCents: BigInt(amountCents),
  37. type: 'topup',
  38. refId: `dev_credit_${Date.now()}`,
  39. },
  40. });
  41. return updated;
  42. });
  43. console.log(
  44. `✅ 已为用户 ${email} 充值 ${amountCents} 美分。当前余额: ${result.balanceCents} 美分`,
  45. );
  46. }
  47. main()
  48. .catch((err) => {
  49. console.error('充值失败:', err);
  50. process.exit(1);
  51. })
  52. .finally(() => prisma.$disconnect());