feat: move template to team (#1217)

Allows users to move templates from their personal account to a team
account.
This commit is contained in:
Ephraim Duncan
2024-07-05 03:20:27 +00:00
committed by GitHub
parent 2c320e8b92
commit a757ab2303
5 changed files with 227 additions and 1 deletions

View File

@ -0,0 +1,57 @@
import { TRPCError } from '@trpc/server';
import { prisma } from '@documenso/prisma';
export type MoveTemplateToTeamOptions = {
templateId: number;
teamId: number;
userId: number;
};
export const moveTemplateToTeam = async ({
templateId,
teamId,
userId,
}: MoveTemplateToTeamOptions) => {
return await prisma.$transaction(async (tx) => {
const template = await tx.template.findFirst({
where: {
id: templateId,
userId,
teamId: null,
},
});
if (!template) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Template not found or already associated with a team.',
});
}
const team = await tx.team.findFirst({
where: {
id: teamId,
members: {
some: {
userId,
},
},
},
});
if (!team) {
throw new TRPCError({
code: 'FORBIDDEN',
message: 'You are not a member of this team.',
});
}
const updatedTemplate = await tx.template.update({
where: { id: templateId },
data: { teamId },
});
return updatedTemplate;
});
};