|
| 1 | +import { z } from 'zod'; |
| 2 | +import { unauthorized, json, badRequest, notFound, ok } from 'lib/response'; |
| 3 | +import { canDeleteTeam, canUpdateTeam, canViewTeam, checkAuth } from 'lib/auth'; |
| 4 | +import { checkRequest } from 'lib/request'; |
| 5 | +import { deleteTeam, getTeam, updateTeam } from 'queries'; |
| 6 | + |
| 7 | +export async function GET(request: Request, { params }: { params: Promise<{ teamId: string }> }) { |
| 8 | + const schema = z.object({ |
| 9 | + teamId: z.string().uuid(), |
| 10 | + }); |
| 11 | + |
| 12 | + const { error } = await checkRequest(request, schema); |
| 13 | + |
| 14 | + if (error) { |
| 15 | + return badRequest(error); |
| 16 | + } |
| 17 | + |
| 18 | + const { teamId } = await params; |
| 19 | + |
| 20 | + const auth = await checkAuth(request); |
| 21 | + |
| 22 | + if (!auth || !(await canViewTeam(auth, teamId))) { |
| 23 | + return unauthorized(); |
| 24 | + } |
| 25 | + |
| 26 | + const team = await getTeam(teamId, { includeMembers: true }); |
| 27 | + |
| 28 | + if (!team) { |
| 29 | + return notFound('Team not found.'); |
| 30 | + } |
| 31 | + |
| 32 | + return json(team); |
| 33 | +} |
| 34 | + |
| 35 | +export async function POST(request: Request, { params }: { params: Promise<{ teamId: string }> }) { |
| 36 | + const schema = z.object({ |
| 37 | + name: z.string().max(50), |
| 38 | + accessCode: z.string().max(50), |
| 39 | + }); |
| 40 | + |
| 41 | + const { body, error } = await checkRequest(request, schema); |
| 42 | + |
| 43 | + if (error) { |
| 44 | + return badRequest(error); |
| 45 | + } |
| 46 | + |
| 47 | + const { teamId } = await params; |
| 48 | + |
| 49 | + const auth = await checkAuth(request); |
| 50 | + |
| 51 | + if (!auth || !(await canUpdateTeam(auth, teamId))) { |
| 52 | + return unauthorized('You must be the owner of this team.'); |
| 53 | + } |
| 54 | + |
| 55 | + const team = await updateTeam(teamId, body); |
| 56 | + |
| 57 | + return json(team); |
| 58 | +} |
| 59 | + |
| 60 | +export async function DELETE( |
| 61 | + request: Request, |
| 62 | + { params }: { params: Promise<{ teamId: string }> }, |
| 63 | +) { |
| 64 | + const { teamId } = await params; |
| 65 | + |
| 66 | + const auth = await checkAuth(request); |
| 67 | + |
| 68 | + if (!auth || !(await canDeleteTeam(auth, teamId))) { |
| 69 | + return unauthorized('You must be the owner of this team.'); |
| 70 | + } |
| 71 | + |
| 72 | + await deleteTeam(teamId); |
| 73 | + |
| 74 | + return ok(); |
| 75 | +} |
0 commit comments