diff --git a/app/api/stripe/webhook/route.ts b/app/api/stripe/webhook/route.ts index f7d7ff9..5632b7c 100644 --- a/app/api/stripe/webhook/route.ts +++ b/app/api/stripe/webhook/route.ts @@ -71,6 +71,8 @@ async function handleUserSubscriptionUpdate( customerId: string, item: Stripe.SubscriptionItem ) { + console.log(`Updating subscription for customer ${customerId}`) + if (!stripeClient) { return new NextResponse("Stripe client is not initialized", { status: 500 }) } @@ -83,6 +85,8 @@ async function handleUserSubscriptionUpdate( let user = await getUserByStripeCustomerId(customerId) if (!user) { const customer = (await stripeClient.customers.retrieve(customerId)) as Stripe.Customer + console.log(`User not found for customer ${customerId}, creating new user with email ${customer.email}`) + user = await getOrCreateCloudUser(customer.email as string, { email: customer.email as string, name: customer.name as string, diff --git a/models/users.ts b/models/users.ts index 5286a16..ee21069 100644 --- a/models/users.ts +++ b/models/users.ts @@ -28,7 +28,7 @@ export const getOrCreateSelfHostedUser = cache(async () => { export function getOrCreateCloudUser(email: string, data: Prisma.UserCreateInput) { return prisma.user.upsert({ - where: { email }, + where: { email: email.toLowerCase() }, update: data, create: data, }) @@ -42,7 +42,7 @@ export const getUserById = cache(async (id: string) => { export const getUserByEmail = cache(async (email: string) => { return await prisma.user.findUnique({ - where: { email }, + where: { email: email.toLowerCase() }, }) })