fix: check db initialisation on getOrCreateCloudUser

This commit is contained in:
vas3k
2025-05-22 18:26:19 +02:00
parent c0966ab327
commit feb56fa3ac
3 changed files with 11 additions and 11 deletions

View File

@@ -41,10 +41,6 @@ export default async function CloudPaymentSuccessPage({
aiBalance: plan?.limits.ai, aiBalance: plan?.limits.ai,
}) })
if (await isDatabaseEmpty(user.id)) {
await createUserDefaults(user.id)
}
return ( return (
<Card className="w-full max-w-xl mx-auto p-8 flex flex-col items-center justify-center gap-4"> <Card className="w-full max-w-xl mx-auto p-8 flex flex-col items-center justify-center gap-4">
<Cake className="w-36 h-36" /> <Cake className="w-36 h-36" />

View File

@@ -86,16 +86,12 @@ async function handleUserSubscriptionUpdate(
if (!user) { if (!user) {
const customer = (await stripeClient.customers.retrieve(customerId)) as Stripe.Customer 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}`) console.log(`User not found for customer ${customerId}, creating new user with email ${customer.email}`)
user = await getOrCreateCloudUser(customer.email as string, { user = await getOrCreateCloudUser(customer.email as string, {
email: customer.email as string, email: customer.email as string,
name: customer.name as string, name: customer.name as string,
stripeCustomerId: customer.id, stripeCustomerId: customer.id,
}) })
if (await isDatabaseEmpty(user.id)) {
await createUserDefaults(user.id)
}
} }
const newMembershipExpiresAt = new Date(item.current_period_end * 1000) const newMembershipExpiresAt = new Date(item.current_period_end * 1000)

View File

@@ -1,6 +1,8 @@
import { prisma } from "@/lib/db" import { prisma } from "@/lib/db"
import { Prisma } from "@/prisma/client" import { Prisma } from "@/prisma/client"
import { cache } from "react" import { cache } from "react"
import { isDatabaseEmpty } from "./defaults"
import { createUserDefaults } from "./defaults"
export const SELF_HOSTED_USER = { export const SELF_HOSTED_USER = {
email: "taxhacker@localhost", email: "taxhacker@localhost",
@@ -26,12 +28,18 @@ export const getOrCreateSelfHostedUser = cache(async () => {
}) })
}) })
export function getOrCreateCloudUser(email: string, data: Prisma.UserCreateInput) { export async function getOrCreateCloudUser(email: string, data: Prisma.UserCreateInput) {
return prisma.user.upsert({ const user = await prisma.user.upsert({
where: { email: email.toLowerCase() }, where: { email: email.toLowerCase() },
update: data, update: data,
create: data, create: data,
}) })
if (await isDatabaseEmpty(user.id)) {
await createUserDefaults(user.id)
}
return user
} }
export const getUserById = cache(async (id: string) => { export const getUserById = cache(async (id: string) => {