mirror of
https://github.com/umami-software/umami.git
synced 2026-02-19 12:05:41 +01:00
Add some api/team endpoints.
This commit is contained in:
parent
f5eb974d8d
commit
25279271ce
10 changed files with 172 additions and 43 deletions
|
|
@ -0,0 +1,47 @@
|
|||
import { Team } from '@prisma/client';
|
||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, methodNotAllowed, ok } from 'next-basics';
|
||||
import { createTeam, getTeam, getTeamsByUserId } from 'queries';
|
||||
export interface TeamsRequestBody {
|
||||
name: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<any, TeamsRequestBody>,
|
||||
res: NextApiResponse<Team[] | Team>,
|
||||
) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
const {
|
||||
user: { id },
|
||||
} = req.auth;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const users = await getTeamsByUserId(id);
|
||||
|
||||
return ok(res, users);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { name } = req.body;
|
||||
|
||||
const user = await getTeam({ name });
|
||||
|
||||
if (user) {
|
||||
return badRequest(res, 'Team already exists');
|
||||
}
|
||||
|
||||
const created = await createTeam({
|
||||
id: id || uuid(),
|
||||
name,
|
||||
});
|
||||
|
||||
return ok(res, created);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue