mirror of
https://github.com/JetSprow/J-Board-Lite.git
synced 2026-05-01 01:14:10 +05:30
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { z } from "zod";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { requireAuth } from "@/lib/require-auth";
|
|
import { createWalletRechargeOrder, redeemRechargeCard } from "@/services/wallet";
|
|
|
|
const rechargeSchema = z.object({
|
|
amount: z.coerce.number().min(1, "充值金额不能低于 1 元").max(100000, "单次充值金额过大"),
|
|
});
|
|
|
|
const redeemSchema = z.object({
|
|
code: z.string().trim().min(4, "请输入充值卡卡密"),
|
|
});
|
|
|
|
export async function createWalletRecharge(formData: FormData) {
|
|
const session = await requireAuth();
|
|
const data = rechargeSchema.parse(Object.fromEntries(formData));
|
|
const order = await createWalletRechargeOrder(session.user.id, data.amount);
|
|
return { id: order.id };
|
|
}
|
|
|
|
export async function redeemWalletCard(formData: FormData) {
|
|
const session = await requireAuth();
|
|
const data = redeemSchema.parse(Object.fromEntries(formData));
|
|
const result = await redeemRechargeCard(session.user.id, data.code);
|
|
revalidatePath("/wallet");
|
|
revalidatePath("/subscriptions");
|
|
revalidatePath("/dashboard");
|
|
return result;
|
|
}
|
|
|
|
export async function cancelWalletRecharge(rechargeId: string) {
|
|
const session = await requireAuth();
|
|
const recharge = await prisma.walletRechargeOrder.findFirst({
|
|
where: { id: rechargeId, userId: session.user.id },
|
|
select: { id: true, status: true },
|
|
});
|
|
|
|
if (!recharge) {
|
|
throw new Error("充值订单不存在");
|
|
}
|
|
if (recharge.status !== "PENDING") {
|
|
throw new Error("这笔充值订单已经不在待支付状态");
|
|
}
|
|
|
|
await prisma.walletRechargeOrder.update({
|
|
where: { id: rechargeId },
|
|
data: {
|
|
status: "CANCELLED",
|
|
paymentMethod: null,
|
|
paymentRef: null,
|
|
paymentUrl: null,
|
|
tradeNo: null,
|
|
expireAt: null,
|
|
note: null,
|
|
},
|
|
});
|
|
|
|
revalidatePath("/wallet");
|
|
revalidatePath(`/wallet/recharge/${rechargeId}`);
|
|
}
|