tasko/lib/subscription.ts
2024-03-17 00:48:01 -04:00

36 lines
719 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,
},
cacheStrategy: { ttl: 30, swr: 60 },
});
if (!orgSubscription) {
return false;
}
const isValid =
orgSubscription.stripePriceId &&
orgSubscription.stripeCurrentPeriodEnd?.getTime()! + DAY_IN_MS > Date.now();
return !!isValid;
};