mirror of
https://github.com/umami-software/umami.git
synced 2026-02-07 22:27:16 +01:00
Moved code into src folder. Added build for component library.
This commit is contained in:
parent
7a7233ead4
commit
ede658771e
490 changed files with 749 additions and 442 deletions
49
src/pages/api/teams/join.ts
Normal file
49
src/pages/api/teams/join.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import { Team } from '@prisma/client';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { useAuth, useValidate } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, notFound, ok } from 'next-basics';
|
||||
import { createTeamUser, getTeamByAccessCode, getTeamUser } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
export interface TeamsJoinRequestBody {
|
||||
accessCode: string;
|
||||
}
|
||||
|
||||
const schema = {
|
||||
POST: yup.object().shape({
|
||||
accessCode: yup.string().max(50).required(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<any, TeamsJoinRequestBody>,
|
||||
res: NextApiResponse<Team>,
|
||||
) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
req.yup = schema;
|
||||
await useValidate(req, res);
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { accessCode } = req.body;
|
||||
|
||||
const team = await getTeamByAccessCode(accessCode);
|
||||
|
||||
if (!team) {
|
||||
return notFound(res, 'message.team-not-found');
|
||||
}
|
||||
|
||||
const teamUser = await getTeamUser(team.id, req.auth.user.id);
|
||||
|
||||
if (teamUser) {
|
||||
return methodNotAllowed(res, 'message.team-already-member');
|
||||
}
|
||||
|
||||
await createTeamUser(req.auth.user.id, team.id, ROLES.teamMember);
|
||||
|
||||
return ok(res, team);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue