diff options
| author | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
| commit | 396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b (patch) | |
| tree | b9df4ca6a70db45cfffbae6fdd7252e20fb8e93c /src/app/api/teams/join | |
| download | umami-396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b.tar.xz umami-396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b.zip | |
Created from https://vercel.com/new
Diffstat (limited to 'src/app/api/teams/join')
| -rw-r--r-- | src/app/api/teams/join/route.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/app/api/teams/join/route.ts b/src/app/api/teams/join/route.ts new file mode 100644 index 0000000..3ce0913 --- /dev/null +++ b/src/app/api/teams/join/route.ts @@ -0,0 +1,39 @@ +import { z } from 'zod'; +import { ROLES } from '@/lib/constants'; +import { parseRequest } from '@/lib/request'; +import { badRequest, json, notFound } from '@/lib/response'; +import { createTeamUser, findTeam, getTeamUser } from '@/queries/prisma'; + +export async function POST(request: Request) { + const schema = z.object({ + accessCode: z.string().max(50), + }); + + const { auth, body, error } = await parseRequest(request, schema); + + if (error) { + return error(); + } + + const { accessCode } = body; + + const team = await findTeam({ + where: { + accessCode, + }, + }); + + if (!team) { + return notFound({ message: 'Team not found.', code: 'team-not-found' }); + } + + const teamUser = await getTeamUser(team.id, auth.user.id); + + if (teamUser) { + return badRequest({ message: 'User is already a team member.' }); + } + + const user = await createTeamUser(auth.user.id, team.id, ROLES.teamMember); + + return json(user); +} |