From 52562b5aa7194a6e0e259542d96069455516bf72 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Wed, 6 Aug 2025 23:19:37 +0800 Subject: [PATCH] fix build --- src/app/api/webhooks/stripe/route.ts | 66 +++++++++++++++++----------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/src/app/api/webhooks/stripe/route.ts b/src/app/api/webhooks/stripe/route.ts index 76b9830..69544a9 100644 --- a/src/app/api/webhooks/stripe/route.ts +++ b/src/app/api/webhooks/stripe/route.ts @@ -137,10 +137,14 @@ async function handleSubscriptionCreated(subscription: Record) return } - // 查找用户 - const user = await (prisma as any).user.findFirst({ - where: { stripeCustomerId: customerId } - }) + // 查找用户 - 使用原始查询避免类型问题 + const users = await prisma.$queryRaw>` + SELECT id, email, "stripeCustomerId" + FROM users + WHERE "stripeCustomerId" = ${customerId} + LIMIT 1 + ` + const user = users[0] || null if (!user) { console.error('❌ User not found for customer:', customerId) @@ -211,9 +215,13 @@ async function handleSubscriptionUpdate(subscription: Record) { }) // 查找用户 - const user = await (prisma as any).user.findFirst({ - where: { stripeCustomerId: customerId } - }) + const users = await prisma.$queryRaw>` + SELECT id, email, "stripeCustomerId" + FROM users + WHERE "stripeCustomerId" = ${customerId} + LIMIT 1 + ` + const user = users[0] || null if (!user) { console.error('❌ User not found for customer:', customerId) @@ -227,25 +235,27 @@ async function handleSubscriptionUpdate(subscription: Record) { const planId = await SubscriptionService.getPlanIdByStripePriceId(priceId) // 查找现有的订阅记录(通过 Stripe 订阅 ID) - const existingSubscription = await (prisma as any).subscription.findFirst({ - where: { - stripeSubscriptionId, - } - }) + const existingSubscriptions = await prisma.$queryRaw>` + SELECT id, "userId", status + FROM subscriptions + WHERE "stripeSubscriptionId" = ${stripeSubscriptionId} + LIMIT 1 + ` + const existingSubscription = existingSubscriptions[0] || null if (existingSubscription) { // 更新现有的订阅记录 - await (prisma as any).subscription.update({ - where: { id: existingSubscription.id }, - data: { - isActive: true, - status: 'active', - startDate: new Date(currentPeriodStart * 1000), - endDate: new Date(currentPeriodEnd * 1000), - metadata: subscription ? JSON.parse(JSON.stringify(subscription)) : undefined, - updatedAt: new Date() - } - }) + await prisma.$executeRaw` + UPDATE subscriptions + SET + "isActive" = true, + status = 'active', + "startDate" = ${new Date(currentPeriodStart * 1000)}, + "endDate" = ${new Date(currentPeriodEnd * 1000)}, + metadata = ${subscription ? JSON.stringify(subscription) : null}::jsonb, + "updatedAt" = NOW() + WHERE id = ${existingSubscription.id} + ` console.log(`✅ Updated existing subscription ${existingSubscription.id} for user ${user.id}`) // 更新用户的默认订阅套餐(保持向后兼容) @@ -271,9 +281,13 @@ async function handleSubscriptionDeleted(subscription: Record) const customerId = subscription.customer as string // 查找用户 - const user = await (prisma as any).user.findFirst({ - where: { stripeCustomerId: customerId } - }) + const users = await prisma.$queryRaw>` + SELECT id, email, "stripeCustomerId" + FROM users + WHERE "stripeCustomerId" = ${customerId} + LIMIT 1 + ` + const user = users[0] || null if (!user) { console.error('User not found for customer:', customerId)