feat: make enterprise billing dynamic (#1539)
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
|
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 { prisma } from '@documenso/prisma';
|
||||||
import type { Subscription } from '@documenso/prisma/client';
|
import type { Subscription } from '@documenso/prisma/client';
|
||||||
|
|
||||||
|
import { getEnterprisePlanPriceIds } from '../stripe/get-enterprise-plan-prices';
|
||||||
|
|
||||||
export type IsUserEnterpriseOptions = {
|
export type IsUserEnterpriseOptions = {
|
||||||
userId: number;
|
userId: number;
|
||||||
teamId?: number;
|
teamId?: number;
|
||||||
@@ -52,5 +54,11 @@ export const isUserEnterprise = async ({
|
|||||||
.then((user) => user.Subscription);
|
.then((user) => user.Subscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
return subscriptionsContainActiveEnterprisePlan(subscriptions);
|
if (subscriptions.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const enterprisePlanPriceIds = await getEnterprisePlanPriceIds();
|
||||||
|
|
||||||
|
return subscriptionsContainsActivePlan(subscriptions, enterprisePlanPriceIds, true);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
import { env } from 'next-runtime-env';
|
|
||||||
|
|
||||||
import { IS_BILLING_ENABLED } from '../constants/app';
|
|
||||||
import type { Subscription } from '.prisma/client';
|
import type { Subscription } from '.prisma/client';
|
||||||
import { SubscriptionStatus } from '.prisma/client';
|
import { SubscriptionStatus } from '.prisma/client';
|
||||||
|
|
||||||
@@ -10,10 +7,18 @@ import { SubscriptionStatus } from '.prisma/client';
|
|||||||
export const subscriptionsContainsActivePlan = (
|
export const subscriptionsContainsActivePlan = (
|
||||||
subscriptions: Subscription[],
|
subscriptions: Subscription[],
|
||||||
priceIds: string[],
|
priceIds: string[],
|
||||||
|
allowPastDue?: boolean,
|
||||||
) => {
|
) => {
|
||||||
|
const allowedSubscriptionStatuses: SubscriptionStatus[] = [SubscriptionStatus.ACTIVE];
|
||||||
|
|
||||||
|
if (allowPastDue) {
|
||||||
|
allowedSubscriptionStatuses.push(SubscriptionStatus.PAST_DUE);
|
||||||
|
}
|
||||||
|
|
||||||
return subscriptions.some(
|
return subscriptions.some(
|
||||||
(subscription) =>
|
(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),
|
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,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user