taxonomy/lib/subscription.ts
2023-03-03 19:17:05 +04:00

39 lines
901 B
TypeScript

// @ts-nocheck
// TODO: Fix this when we turn strict mode on.
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,
},
})
if (!user) {
throw new Error("User not found")
}
// 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,
}
}