Skip to content

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

feat: default team creation

Aashish-Upadhyay-10165fa49f05e17
diff --git a/pages/api/teams/index.ts b/pages/api/teams/index.ts
index 6c45b15b..b53378c9 100644
--- a/pages/api/teams/index.ts
+++ b/pages/api/teams/index.ts
@@ -17,10 +17,12 @@ export default async function handle(
return res.status(401).end("Unauthorized");
}
+ const user = session.user as CustomUser;
+
try {
const userTeams = await prisma.userTeam.findMany({
where: {
- userId: (session.user as CustomUser).id,
+ userId: user.id,
},
include: {
team: {
@@ -33,6 +35,28 @@ export default async function handle(
});
const teams = userTeams.map((userTeam) => userTeam.team);
+
+ // if no teams then create a default one
+ if (teams.length === 0) {
+ const defaultTeamName = `${user.name}'s Team`;
+ const defaultTeam = await prisma.team.create({
+ data: {
+ name: defaultTeamName,
+ users: {
+ create: {
+ userId: user.id,
+ role: "ADMIN",
+ },
+ },
+ },
+ select: {
+ id: true,
+ name: true,
+ },
+ });
+ teams.push(defaultTeam);
+ }
+
return res.status(200).json(teams);
} catch (error) {
log(`Failed to add domain. Error: \n\n ${error}`);
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 3beb2214..83cd84b5 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -84,6 +84,8 @@ model UserTeam {
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
@@id([userId, teamId])
+ @@index([userId])
+ @@index([teamId])
}
model VerificationToken {