fix build
This commit is contained in:
parent
64e2ad63b8
commit
52562b5aa7
@ -137,10 +137,14 @@ async function handleSubscriptionCreated(subscription: Record<string, unknown>)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找用户
|
// 查找用户 - 使用原始查询避免类型问题
|
||||||
const user = await (prisma as any).user.findFirst({
|
const users = await prisma.$queryRaw<Array<{id: string, email: string, stripeCustomerId: string}>>`
|
||||||
where: { stripeCustomerId: customerId }
|
SELECT id, email, "stripeCustomerId"
|
||||||
})
|
FROM users
|
||||||
|
WHERE "stripeCustomerId" = ${customerId}
|
||||||
|
LIMIT 1
|
||||||
|
`
|
||||||
|
const user = users[0] || null
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
console.error('❌ User not found for customer:', customerId)
|
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({
|
const users = await prisma.$queryRaw<Array<{id: string, email: string, stripeCustomerId: string}>>`
|
||||||
where: { stripeCustomerId: customerId }
|
SELECT id, email, "stripeCustomerId"
|
||||||
})
|
FROM users
|
||||||
|
WHERE "stripeCustomerId" = ${customerId}
|
||||||
|
LIMIT 1
|
||||||
|
`
|
||||||
|
const user = users[0] || null
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
console.error('❌ User not found for customer:', customerId)
|
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)
|
const planId = await SubscriptionService.getPlanIdByStripePriceId(priceId)
|
||||||
|
|
||||||
// 查找现有的订阅记录(通过 Stripe 订阅 ID)
|
// 查找现有的订阅记录(通过 Stripe 订阅 ID)
|
||||||
const existingSubscription = await (prisma as any).subscription.findFirst({
|
const existingSubscriptions = await prisma.$queryRaw<Array<{id: string, userId: string, status: string}>>`
|
||||||
where: {
|
SELECT id, "userId", status
|
||||||
stripeSubscriptionId,
|
FROM subscriptions
|
||||||
}
|
WHERE "stripeSubscriptionId" = ${stripeSubscriptionId}
|
||||||
})
|
LIMIT 1
|
||||||
|
`
|
||||||
|
const existingSubscription = existingSubscriptions[0] || null
|
||||||
|
|
||||||
if (existingSubscription) {
|
if (existingSubscription) {
|
||||||
// 更新现有的订阅记录
|
// 更新现有的订阅记录
|
||||||
await (prisma as any).subscription.update({
|
await prisma.$executeRaw`
|
||||||
where: { id: existingSubscription.id },
|
UPDATE subscriptions
|
||||||
data: {
|
SET
|
||||||
isActive: true,
|
"isActive" = true,
|
||||||
status: 'active',
|
status = 'active',
|
||||||
startDate: new Date(currentPeriodStart * 1000),
|
"startDate" = ${new Date(currentPeriodStart * 1000)},
|
||||||
endDate: new Date(currentPeriodEnd * 1000),
|
"endDate" = ${new Date(currentPeriodEnd * 1000)},
|
||||||
metadata: subscription ? JSON.parse(JSON.stringify(subscription)) : undefined,
|
metadata = ${subscription ? JSON.stringify(subscription) : null}::jsonb,
|
||||||
updatedAt: new Date()
|
"updatedAt" = NOW()
|
||||||
}
|
WHERE id = ${existingSubscription.id}
|
||||||
})
|
`
|
||||||
console.log(`✅ Updated existing subscription ${existingSubscription.id} for user ${user.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 customerId = subscription.customer as string
|
||||||
|
|
||||||
// 查找用户
|
// 查找用户
|
||||||
const user = await (prisma as any).user.findFirst({
|
const users = await prisma.$queryRaw<Array<{id: string, email: string, stripeCustomerId: string}>>`
|
||||||
where: { stripeCustomerId: customerId }
|
SELECT id, email, "stripeCustomerId"
|
||||||
})
|
FROM users
|
||||||
|
WHERE "stripeCustomerId" = ${customerId}
|
||||||
|
LIMIT 1
|
||||||
|
`
|
||||||
|
const user = users[0] || null
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
console.error('User not found for customer:', customerId)
|
console.error('User not found for customer:', customerId)
|
||||||
|
Loading…
Reference in New Issue
Block a user