import { subscriptionsContainsActivePlan } from '@documenso/lib/utils/billing'; import { prisma } from '@documenso/prisma'; import type { Subscription } from '@documenso/prisma/client'; import { getCommunityPlanPriceIds } from '../stripe/get-community-plan-prices'; export type IsCommunityPlanOptions = { userId: number; teamId?: number; }; /** * Whether the user or team is on the community plan. */ export const isCommunityPlan = async ({ userId, teamId, }: IsCommunityPlanOptions): Promise => { let subscriptions: Subscription[] = []; if (teamId) { subscriptions = await prisma.team .findFirstOrThrow({ where: { id: teamId, }, select: { owner: { include: { subscriptions: true, }, }, }, }) .then((team) => team.owner.subscriptions); } else { subscriptions = await prisma.user .findFirstOrThrow({ where: { id: userId, }, select: { subscriptions: true, }, }) .then((user) => user.subscriptions); } if (subscriptions.length === 0) { return false; } const communityPlanPriceIds = await getCommunityPlanPriceIds(); return subscriptionsContainsActivePlan(subscriptions, communityPlanPriceIds); };