diff --git a/packages/ee/server-only/util/is-document-enterprise.ts b/packages/ee/server-only/util/is-document-enterprise.ts index 01c2d7327..c49d98edb 100644 --- a/packages/ee/server-only/util/is-document-enterprise.ts +++ b/packages/ee/server-only/util/is-document-enterprise.ts @@ -1,8 +1,10 @@ import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app'; -import { subscriptionsContainActiveEnterprisePlan } from '@documenso/lib/utils/billing'; +import { subscriptionsContainsActivePlan } from '@documenso/lib/utils/billing'; import { prisma } from '@documenso/prisma'; import type { Subscription } from '@documenso/prisma/client'; +import { getEnterprisePlanPriceIds } from '../stripe/get-enterprise-plan-prices'; + export type IsUserEnterpriseOptions = { userId: number; teamId?: number; @@ -52,5 +54,11 @@ export const isUserEnterprise = async ({ .then((user) => user.Subscription); } - return subscriptionsContainActiveEnterprisePlan(subscriptions); + if (subscriptions.length === 0) { + return false; + } + + const enterprisePlanPriceIds = await getEnterprisePlanPriceIds(); + + return subscriptionsContainsActivePlan(subscriptions, enterprisePlanPriceIds, true); }; diff --git a/packages/lib/utils/billing.ts b/packages/lib/utils/billing.ts index 454fe7fba..d98ec82cf 100644 --- a/packages/lib/utils/billing.ts +++ b/packages/lib/utils/billing.ts @@ -1,6 +1,3 @@ -import { env } from 'next-runtime-env'; - -import { IS_BILLING_ENABLED } from '../constants/app'; import type { Subscription } from '.prisma/client'; import { SubscriptionStatus } from '.prisma/client'; @@ -10,10 +7,18 @@ import { SubscriptionStatus } from '.prisma/client'; export const subscriptionsContainsActivePlan = ( subscriptions: Subscription[], priceIds: string[], + allowPastDue?: boolean, ) => { + const allowedSubscriptionStatuses: SubscriptionStatus[] = [SubscriptionStatus.ACTIVE]; + + if (allowPastDue) { + allowedSubscriptionStatuses.push(SubscriptionStatus.PAST_DUE); + } + return subscriptions.some( (subscription) => - subscription.status === SubscriptionStatus.ACTIVE && priceIds.includes(subscription.priceId), + allowedSubscriptionStatuses.includes(subscription.status) && + priceIds.includes(subscription.priceId), ); }; @@ -29,23 +34,3 @@ export const subscriptionsContainsActiveProductId = ( subscription.status === SubscriptionStatus.ACTIVE && productId.includes(subscription.planId), ); }; - -export const subscriptionsContainActiveEnterprisePlan = ( - subscriptions?: Subscription[], -): boolean => { - const enterprisePlanId = env('NEXT_PUBLIC_STRIPE_ENTERPRISE_PLAN_MONTHLY_PRICE_ID'); - - if (!enterprisePlanId || !subscriptions || !IS_BILLING_ENABLED()) { - return false; - } - - const acceptableStatuses: SubscriptionStatus[] = [ - SubscriptionStatus.ACTIVE, - SubscriptionStatus.PAST_DUE, - ]; - - return subscriptions.some( - (subscription) => - acceptableStatuses.includes(subscription.status) && enterprisePlanId === subscription.priceId, - ); -};