Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat(ee): add plan-specific limits to documents and links

Marc Seitzab389193eb46
diff --git a/ee/limits/constants.ts b/ee/limits/constants.ts
index 41510b26..b5632e89 100644
--- a/ee/limits/constants.ts
+++ b/ee/limits/constants.ts
@@ -1,5 +1,20 @@
+// INFO: for numeric values,`null` means unlimited
+
+export type TPlanLimits = {
+ users: number;
+ links: number | null;
+ documents: number | null;
+ domains: number;
+ datarooms: number;
+ customDomainOnPro: boolean;
+ customDomainInDataroom: boolean;
+ advancedLinkControlsOnPro: boolean | null;
+};
+
export const FREE_PLAN_LIMITS = {
users: 1,
+ links: 10,
+ documents: 10,
domains: 0,
datarooms: 0,
customDomainOnPro: false,
@@ -9,7 +24,9 @@ export const FREE_PLAN_LIMITS = {
export const PRO_PLAN_LIMITS = {
users: 2,
- domains: 5,
+ links: null,
+ documents: 100,
+ domains: 2,
datarooms: 0,
customDomainOnPro: false,
customDomainInDataroom: false,
@@ -18,6 +35,8 @@ export const PRO_PLAN_LIMITS = {
export const BUSINESS_PLAN_LIMITS = {
users: 3,
+ links: null,
+ documents: null,
domains: 5,
datarooms: 1,
customDomainOnPro: true,
@@ -27,6 +46,8 @@ export const BUSINESS_PLAN_LIMITS = {
export const DATAROOMS_PLAN_LIMITS = {
users: 3,
+ links: null,
+ documents: null,
domains: 10,
datarooms: 100,
customDomainOnPro: true,
diff --git a/ee/limits/server.ts b/ee/limits/server.ts
index 7956d748..1c70bbff 100644
--- a/ee/limits/server.ts
+++ b/ee/limits/server.ts
@@ -2,6 +2,44 @@ import { z } from "zod";
import prisma from "@/lib/prisma";
+import {
+ BUSINESS_PLAN_LIMITS,
+ DATAROOMS_PLAN_LIMITS,
+ FREE_PLAN_LIMITS,
+ PRO_PLAN_LIMITS,
+ TPlanLimits,
+} from "./constants";
+
+// Function to determine if a plan is free or free+drtrial
+const isFreePlan = (plan: string) => plan === "free" || plan === "free+drtrial";
+
+// Function to get the base plan from a plan string
+const getBasePlan = (plan: string) => plan.split("+")[0];
+
+const planLimitsMap: Record<string, TPlanLimits> = {
+ free: FREE_PLAN_LIMITS,
+ pro: PRO_PLAN_LIMITS,
+ business: BUSINESS_PLAN_LIMITS,
+ datarooms: DATAROOMS_PLAN_LIMITS,
+};
+
+const configSchema = z.object({
+ datarooms: z.number(),
+ links: z
+ .preprocess((v) => (v === null ? Infinity : Number(v)), z.number())
+ .optional()
+ .default(10),
+ documents: z
+ .preprocess((v) => (v === null ? Infinity : Number(v)), z.number())
+ .optional()
+ .default(10),
+ users: z.number(),
+ domains: z.number(),
+ customDomainOnPro: z.boolean(),
+ customDomainInDataroom: z.boolean(),
+ advancedLinkControlsOnPro: z.boolean().nullish(),
+});
+
export async function getLimits({
teamId,
userId,
@@ -21,6 +59,20 @@ export async function getLimits({
select: {
plan: true,
limits: true,
+ _count: {
+ select: {
+ documents: true,
+ },
+ },
+ documents: {
+ select: {
+ _count: {
+ select: {
+ links: true,
+ },
+ },
+ },
+ },
},
});
@@ -28,24 +80,42 @@ export async function getLimits({
throw new Error("Team not found");
}
+ const documentCount = team._count.documents;
+ const linkCount = team.documents.reduce(
+ (sum, doc) => sum + doc._count.links,
+ 0,
+ );
+
// parse the limits json with zod and return the limits
// {datarooms: 1, users: 1, domains: 1, customDomainOnPro: boolean, customDomainInDataroom: boolean}
- const configSchema = z.object({
- datarooms: z.number(),
- users: z.number(),
- domains: z.number(),
- customDomainOnPro: z.boolean(),
- customDomainInDataroom: z.boolean(),
- advancedLinkControlsOnPro: z.boolean().nullish(),
- });
-
try {
- const parsedData = configSchema.parse(team.limits);
+ let parsedData = configSchema.parse(team.limits);
- return parsedData;
+ // Adjust limits based on the plan if they're at the default value
+ if (isFreePlan(team.plan)) {
+ return {
+ ...parsedData,
+ usage: { documents: documentCount, links: linkCount },
+ };
+ } else {
+ return {
+ ...parsedData,
+ // if account is paid, but link and document limits are not set, then set them to Infinity
+ links: parsedData.links === 10 ? Infinity : parsedData.links,
+ documents:
+ parsedData.documents === 10 ? Infinity : parsedData.documents,
+ usage: { documents: documentCount, links: linkCount },
+ };
+ }
} catch (error) {
- // if no limits set, then return null and don't block the team
- return null;
+ // if no limits set or parsing fails, return default limits based on the plan
+ const basePlan = getBasePlan(team.plan);
+ const defaultLimits = planLimitsMap[basePlan] || FREE_PLAN_LIMITS;
+
+ return {
+ ...defaultLimits,
+ usage: { documents: documentCount, links: linkCount },
+ };
}
}
diff --git a/ee/limits/swr-handler.ts b/ee/limits/swr-handler.ts
index 4c027029..a9f1fbfb 100644
--- a/ee/limits/swr-handler.ts
+++ b/ee/limits/swr-handler.ts
@@ -5,11 +5,17 @@ import { fetcher } from "@/lib/utils";
export type LimitProps = {
datarooms: number;
+ links: number | undefined | null;
+ documents: number | undefined | null;
users: number;
domains: number;
customDomainOnPro: boolean;
customDomainInDataroom: boolean;
- advancedLinkControlsOnPro: boolean;
+ advancedLinkControlsOnPro: boolean | undefined | null;
+ usage: {
+ documents: number;
+ links: number;
+ };
};
export function useLimits() {
@@ -24,8 +30,15 @@ export function useLimits() {
},
);
+ const canAddDocuments = data?.documents
+ ? data?.usage?.documents < data?.documents
+ : true;
+ const canAddLinks = data?.links ? data?.usage?.links < data?.links : true;
+
return {
limits: data,
+ canAddDocuments,
+ canAddLinks,
error,
loading: !data && !error,
};