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.
89 lines
2.2 KiB
TypeScript
89 lines
2.2 KiB
TypeScript
import type Stripe from 'stripe';
|
|
|
|
import { transferTeamSubscription } from '@documenso/ee/server-only/stripe/transfer-team-subscription';
|
|
import { mapStripeSubscriptionToPrismaUpsertAction } from '@documenso/ee/server-only/stripe/webhook/on-subscription-updated';
|
|
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
|
|
import { prisma } from '@documenso/prisma';
|
|
import { TeamMemberRole } from '@documenso/prisma/client';
|
|
|
|
export type TransferTeamOwnershipOptions = {
|
|
token: string;
|
|
};
|
|
|
|
export const transferTeamOwnership = async ({ token }: TransferTeamOwnershipOptions) => {
|
|
await prisma.$transaction(async (tx) => {
|
|
const teamTransferVerification = await tx.teamTransferVerification.findFirstOrThrow({
|
|
where: {
|
|
token,
|
|
},
|
|
include: {
|
|
team: {
|
|
include: {
|
|
subscription: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const { team, userId: newOwnerUserId } = teamTransferVerification;
|
|
|
|
await tx.teamTransferVerification.delete({
|
|
where: {
|
|
teamId: team.id,
|
|
},
|
|
});
|
|
|
|
const newOwnerUser = await tx.user.findFirstOrThrow({
|
|
where: {
|
|
id: newOwnerUserId,
|
|
teamMembers: {
|
|
some: {
|
|
teamId: team.id,
|
|
},
|
|
},
|
|
},
|
|
include: {
|
|
Subscription: true,
|
|
},
|
|
});
|
|
|
|
let teamSubscription: Stripe.Subscription | null = null;
|
|
|
|
if (IS_BILLING_ENABLED) {
|
|
teamSubscription = await transferTeamSubscription({
|
|
user: newOwnerUser,
|
|
team,
|
|
clearPaymentMethods: teamTransferVerification.clearPaymentMethods,
|
|
});
|
|
}
|
|
|
|
if (teamSubscription) {
|
|
await tx.subscription.upsert(
|
|
mapStripeSubscriptionToPrismaUpsertAction(teamSubscription, undefined, team.id),
|
|
);
|
|
}
|
|
|
|
await tx.team.update({
|
|
where: {
|
|
id: team.id,
|
|
},
|
|
data: {
|
|
ownerUserId: newOwnerUserId,
|
|
members: {
|
|
update: {
|
|
where: {
|
|
userId_teamId: {
|
|
teamId: team.id,
|
|
userId: newOwnerUserId,
|
|
},
|
|
},
|
|
data: {
|
|
role: TeamMemberRole.ADMIN,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
});
|
|
};
|