fix build
This commit is contained in:
parent
64e2ad63b8
commit
52562b5aa7
@ -137,10 +137,14 @@ async function handleSubscriptionCreated(subscription: Record<string, unknown>)
|
||||
return
|
||||
}
|
||||
|
||||
// 查找用户
|
||||
const user = await (prisma as any).user.findFirst({
|
||||
where: { stripeCustomerId: customerId }
|
||||
})
|
||||
// 查找用户 - 使用原始查询避免类型问题
|
||||
const users = await prisma.$queryRaw<Array<{id: string, email: string, stripeCustomerId: string}>>`
|
||||
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<string, unknown>) {
|
||||
})
|
||||
|
||||
// 查找用户
|
||||
const user = await (prisma as any).user.findFirst({
|
||||
where: { stripeCustomerId: customerId }
|
||||
})
|
||||
const users = await prisma.$queryRaw<Array<{id: string, email: string, stripeCustomerId: string}>>`
|
||||
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<string, unknown>) {
|
||||
const planId = await SubscriptionService.getPlanIdByStripePriceId(priceId)
|
||||
|
||||
// 查找现有的订阅记录(通过 Stripe 订阅 ID)
|
||||
const existingSubscription = await (prisma as any).subscription.findFirst({
|
||||
where: {
|
||||
stripeSubscriptionId,
|
||||
}
|
||||
})
|
||||
const existingSubscriptions = await prisma.$queryRaw<Array<{id: string, userId: string, status: string}>>`
|
||||
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<string, unknown>)
|
||||
const customerId = subscription.customer as string
|
||||
|
||||
// 查找用户
|
||||
const user = await (prisma as any).user.findFirst({
|
||||
where: { stripeCustomerId: customerId }
|
||||
})
|
||||
const users = await prisma.$queryRaw<Array<{id: string, email: string, stripeCustomerId: string}>>`
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user