tasko/lib/subscription.ts
2024-02-14 21:30:10 -05:00

35 lines
678 B
TypeScript

import { auth } from "@clerk/nextjs";
import { db } from "@/lib/db";
const DAY_IN_MS = 86_400_000;
export const checkSubscription = async () => {
const { orgId } = auth();
if (!orgId) {
return false;
}
const orgSubscription = await db.orgSubscription.findUnique({
where: {
orgId,
},
select: {
stripeSubscriptionId: true,
stripeCurrentPeriodEnd: true,
stripeCustomerId: true,
stripePriceId: true,
},
});
if (!orgSubscription) {
return false;
}
const isValid =
orgSubscription.stripePriceId &&
orgSubscription.stripeCurrentPeriodEnd?.getTime()! + DAY_IN_MS > Date.now();
return !!isValid;
};