mirror of
https://github.com/shadcn-ui/taxonomy
synced 2026-05-23 09:18:30 +00:00
33 lines
781 B
TypeScript
33 lines
781 B
TypeScript
import { UserSubscriptionPlan } from "types"
|
|
import { freePlan, proPlan } from "@/config/subscriptions"
|
|
import { db } from "@/lib/db"
|
|
|
|
export async function getUserSubscriptionPlan(
|
|
userId: string
|
|
): Promise<UserSubscriptionPlan> {
|
|
const user = await db.user.findFirst({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
select: {
|
|
stripeSubscriptionId: true,
|
|
stripeCurrentPeriodEnd: true,
|
|
stripeCustomerId: true,
|
|
stripePriceId: true,
|
|
},
|
|
})
|
|
|
|
// Check if user is on a pro plan.
|
|
const isPro =
|
|
user.stripePriceId &&
|
|
user.stripeCurrentPeriodEnd?.getTime() + 86_400_000 > Date.now()
|
|
|
|
const plan = isPro ? proPlan : freePlan
|
|
|
|
return {
|
|
...plan,
|
|
...user,
|
|
stripeCurrentPeriodEnd: user.stripeCurrentPeriodEnd?.getTime(),
|
|
isPro,
|
|
}
|
|
}
|