mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +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.
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
import { updateSubscriptionItemQuantity } from '@documenso/ee/server-only/stripe/update-subscription-item-quantity';
|
|
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
|
|
import { TEAM_MEMBER_ROLE_PERMISSIONS_MAP } from '@documenso/lib/constants/teams';
|
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import { isTeamRoleWithinUserHierarchy } from '@documenso/lib/utils/teams';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
export type DeleteTeamMembersOptions = {
|
|
/**
|
|
* The ID of the user who is initiating this action.
|
|
*/
|
|
userId: number;
|
|
|
|
/**
|
|
* The ID of the team to remove members from.
|
|
*/
|
|
teamId: number;
|
|
|
|
/**
|
|
* The IDs of the team members to remove.
|
|
*/
|
|
teamMemberIds: number[];
|
|
};
|
|
|
|
export const deleteTeamMembers = async ({
|
|
userId,
|
|
teamId,
|
|
teamMemberIds,
|
|
}: DeleteTeamMembersOptions) => {
|
|
await prisma.$transaction(async (tx) => {
|
|
// Find the team and validate that the user is allowed to remove members.
|
|
const team = await tx.team.findFirstOrThrow({
|
|
where: {
|
|
id: teamId,
|
|
members: {
|
|
some: {
|
|
userId,
|
|
role: {
|
|
in: TEAM_MEMBER_ROLE_PERMISSIONS_MAP['MANAGE_TEAM'],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
include: {
|
|
members: {
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
role: true,
|
|
},
|
|
},
|
|
subscription: true,
|
|
},
|
|
});
|
|
|
|
const currentTeamMember = team.members.find((member) => member.userId === userId);
|
|
const teamMembersToRemove = team.members.filter((member) => teamMemberIds.includes(member.id));
|
|
|
|
if (!currentTeamMember) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, 'Team member record does not exist');
|
|
}
|
|
|
|
if (teamMembersToRemove.find((member) => member.userId === team.ownerUserId)) {
|
|
throw new AppError(AppErrorCode.UNAUTHORIZED, 'Cannot remove the team owner');
|
|
}
|
|
|
|
const isMemberToRemoveHigherRole = teamMembersToRemove.some(
|
|
(member) => !isTeamRoleWithinUserHierarchy(currentTeamMember.role, member.role),
|
|
);
|
|
|
|
if (isMemberToRemoveHigherRole) {
|
|
throw new AppError(AppErrorCode.UNAUTHORIZED, 'Cannot remove a member with a higher role');
|
|
}
|
|
|
|
// Remove the team members.
|
|
await tx.teamMember.deleteMany({
|
|
where: {
|
|
id: {
|
|
in: teamMemberIds,
|
|
},
|
|
teamId,
|
|
userId: {
|
|
not: team.ownerUserId,
|
|
},
|
|
},
|
|
});
|
|
|
|
if (IS_BILLING_ENABLED && team.subscription) {
|
|
const numberOfSeats = await tx.teamMember.count({
|
|
where: {
|
|
teamId,
|
|
},
|
|
});
|
|
|
|
await updateSubscriptionItemQuantity({
|
|
priceId: team.subscription.priceId,
|
|
subscriptionId: team.subscription.planId,
|
|
quantity: numberOfSeats,
|
|
});
|
|
}
|
|
});
|
|
};
|