( userId: string )
| 5 | import { db } from "@/lib/db" |
| 6 | |
| 7 | export async function getUserSubscriptionPlan( |
| 8 | userId: string |
| 9 | ): Promise<UserSubscriptionPlan> { |
| 10 | const user = await db.user.findFirst({ |
| 11 | where: { |
| 12 | id: userId, |
| 13 | }, |
| 14 | select: { |
| 15 | stripeSubscriptionId: true, |
| 16 | stripeCurrentPeriodEnd: true, |
| 17 | stripeCustomerId: true, |
| 18 | stripePriceId: true, |
| 19 | }, |
| 20 | }) |
| 21 | |
| 22 | if (!user) { |
| 23 | throw new Error("User not found") |
| 24 | } |
| 25 | |
| 26 | // Check if user is on a pro plan. |
| 27 | const isPro = |
| 28 | user.stripePriceId && |
| 29 | user.stripeCurrentPeriodEnd?.getTime() + 86_400_000 > Date.now() |
| 30 | |
| 31 | const plan = isPro ? proPlan : freePlan |
| 32 | |
| 33 | return { |
| 34 | ...plan, |
| 35 | ...user, |
| 36 | stripeCurrentPeriodEnd: user.stripeCurrentPeriodEnd?.getTime(), |
| 37 | isPro, |
| 38 | } |
| 39 | } |
no outgoing calls
no test coverage detected