2024-03-21 21:15:27 +00:00
|
|
|
import { auth } from '@clerk/nextjs/server';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
2024-02-16 01:49:19 +00:00
|
|
|
import { db } from '@/lib/db';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
2024-03-17 04:48:01 +00:00
|
|
|
cacheStrategy: { ttl: 30, swr: 60 },
|
2024-02-15 02:30:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!orgSubscription) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isValid =
|
|
|
|
orgSubscription.stripePriceId &&
|
|
|
|
orgSubscription.stripeCurrentPeriodEnd?.getTime()! + DAY_IN_MS > Date.now();
|
|
|
|
|
|
|
|
return !!isValid;
|
|
|
|
};
|