mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 07:37:11 +01:00
Convert teams api routes.
This commit is contained in:
parent
7d5556a637
commit
e51f182403
15 changed files with 354 additions and 4 deletions
46
src/app/api/teams/join/route.ts
Normal file
46
src/app/api/teams/join/route.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { z } from 'zod';
|
||||
import { unauthorized, json, badRequest, notFound } from 'lib/response';
|
||||
import { canCreateTeam, checkAuth } from 'lib/auth';
|
||||
import { checkRequest } from 'lib/request';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { createTeamUser, findTeam, getTeamUser } from 'queries';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const schema = z.object({
|
||||
accessCode: z.string().max(50),
|
||||
});
|
||||
|
||||
const { body, error } = await checkRequest(request, schema);
|
||||
|
||||
if (error) {
|
||||
return badRequest(error);
|
||||
}
|
||||
|
||||
const auth = await checkAuth(request);
|
||||
|
||||
if (!auth || !(await canCreateTeam(auth))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const { accessCode } = body;
|
||||
|
||||
const team = await findTeam({
|
||||
where: {
|
||||
accessCode,
|
||||
},
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
return notFound('Team not found.');
|
||||
}
|
||||
|
||||
const teamUser = await getTeamUser(team.id, auth.user.id);
|
||||
|
||||
if (teamUser) {
|
||||
return badRequest('User is already a team member.');
|
||||
}
|
||||
|
||||
const user = await createTeamUser(auth.user.id, team.id, ROLES.teamMember);
|
||||
|
||||
return json(user);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue