mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
## Description Add support for teams which will allow users to collaborate on documents. Teams features allows users to: - Create, manage and transfer teams - Manage team members - Manage team emails - Manage a shared team inbox and documents These changes do NOT include the following, which are planned for a future release: - Team templates - Team API - Search menu integration ## Testing Performed - Added E2E tests for general team management - Added E2E tests to validate document counts ## Checklist - [X] I have tested these changes locally and they work as expected. - [X] I have added/updated tests that prove the effectiveness of these changes. - [ ] I have updated the documentation to reflect these changes, if applicable. - [X] I have followed the project's coding style guidelines.
143 lines
3.4 KiB
TypeScript
143 lines
3.4 KiB
TypeScript
import { DateTime } from 'luxon';
|
|
|
|
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
|
|
import { STRIPE_PLAN_TYPE } from '@documenso/lib/constants/billing';
|
|
import { prisma } from '@documenso/prisma';
|
|
import { SubscriptionStatus } from '@documenso/prisma/client';
|
|
|
|
import { getPricesByPlan } from '../stripe/get-prices-by-plan';
|
|
import { FREE_PLAN_LIMITS, SELFHOSTED_PLAN_LIMITS, TEAM_PLAN_LIMITS } from './constants';
|
|
import { ERROR_CODES } from './errors';
|
|
import { ZLimitsSchema } from './schema';
|
|
|
|
export type GetServerLimitsOptions = {
|
|
email?: string | null;
|
|
teamId?: number | null;
|
|
};
|
|
|
|
export const getServerLimits = async ({ email, teamId }: GetServerLimitsOptions) => {
|
|
if (!IS_BILLING_ENABLED) {
|
|
return {
|
|
quota: SELFHOSTED_PLAN_LIMITS,
|
|
remaining: SELFHOSTED_PLAN_LIMITS,
|
|
};
|
|
}
|
|
|
|
if (!email) {
|
|
throw new Error(ERROR_CODES.UNAUTHORIZED);
|
|
}
|
|
|
|
return teamId ? handleTeamLimits({ email, teamId }) : handleUserLimits({ email });
|
|
};
|
|
|
|
type HandleUserLimitsOptions = {
|
|
email: string;
|
|
};
|
|
|
|
const handleUserLimits = async ({ email }: HandleUserLimitsOptions) => {
|
|
const user = await prisma.user.findFirst({
|
|
where: {
|
|
email,
|
|
},
|
|
include: {
|
|
Subscription: true,
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
throw new Error(ERROR_CODES.USER_FETCH_FAILED);
|
|
}
|
|
|
|
let quota = structuredClone(FREE_PLAN_LIMITS);
|
|
let remaining = structuredClone(FREE_PLAN_LIMITS);
|
|
|
|
const activeSubscriptions = user.Subscription.filter(
|
|
({ status }) => status === SubscriptionStatus.ACTIVE,
|
|
);
|
|
|
|
if (activeSubscriptions.length > 0) {
|
|
const communityPlanPrices = await getPricesByPlan(STRIPE_PLAN_TYPE.COMMUNITY);
|
|
|
|
for (const subscription of activeSubscriptions) {
|
|
const price = communityPlanPrices.find((price) => price.id === subscription.priceId);
|
|
if (!price || typeof price.product === 'string' || price.product.deleted) {
|
|
continue;
|
|
}
|
|
|
|
const currentQuota = ZLimitsSchema.parse(
|
|
'metadata' in price.product ? price.product.metadata : {},
|
|
);
|
|
|
|
// Use the subscription with the highest quota.
|
|
if (currentQuota.documents > quota.documents && currentQuota.recipients > quota.recipients) {
|
|
quota = currentQuota;
|
|
remaining = structuredClone(quota);
|
|
}
|
|
}
|
|
}
|
|
|
|
const documents = await prisma.document.count({
|
|
where: {
|
|
userId: user.id,
|
|
teamId: null,
|
|
createdAt: {
|
|
gte: DateTime.utc().startOf('month').toJSDate(),
|
|
},
|
|
},
|
|
});
|
|
|
|
remaining.documents = Math.max(remaining.documents - documents, 0);
|
|
|
|
return {
|
|
quota,
|
|
remaining,
|
|
};
|
|
};
|
|
|
|
type HandleTeamLimitsOptions = {
|
|
email: string;
|
|
teamId: number;
|
|
};
|
|
|
|
const handleTeamLimits = async ({ email, teamId }: HandleTeamLimitsOptions) => {
|
|
const team = await prisma.team.findFirst({
|
|
where: {
|
|
id: teamId,
|
|
members: {
|
|
some: {
|
|
user: {
|
|
email,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
include: {
|
|
subscription: true,
|
|
},
|
|
});
|
|
|
|
if (!team) {
|
|
throw new Error('Team not found');
|
|
}
|
|
|
|
const { subscription } = team;
|
|
|
|
if (subscription && subscription.status === SubscriptionStatus.INACTIVE) {
|
|
return {
|
|
quota: {
|
|
documents: 0,
|
|
recipients: 0,
|
|
},
|
|
remaining: {
|
|
documents: 0,
|
|
recipients: 0,
|
|
},
|
|
};
|
|
}
|
|
|
|
return {
|
|
quota: structuredClone(TEAM_PLAN_LIMITS),
|
|
remaining: structuredClone(TEAM_PLAN_LIMITS),
|
|
};
|
|
};
|