mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 15:47:13 +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
69
src/pages/api/teams/index.ts
Normal file
69
src/pages/api/teams/index.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import { Team } from '@prisma/client';
|
||||
import { canCreateTeam } from 'lib/auth';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { useAuth, useValidate } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody, SearchFilter, TeamSearchFilterType } from 'lib/types';
|
||||
import { getFilterValidation } from 'lib/yup';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { createTeam, getTeamsByUserId } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface TeamsRequestQuery extends SearchFilter<TeamSearchFilterType> {}
|
||||
export interface TeamsRequestBody {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface MyTeamsRequestQuery extends SearchFilter<TeamSearchFilterType> {}
|
||||
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
...getFilterValidation(/All|Name|Owner/i),
|
||||
}),
|
||||
POST: yup.object().shape({
|
||||
name: yup.string().max(50).required(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<TeamsRequestQuery, TeamsRequestBody>,
|
||||
res: NextApiResponse<Team[] | Team>,
|
||||
) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
req.yup = schema;
|
||||
await useValidate(req, res);
|
||||
|
||||
const {
|
||||
user: { id: userId },
|
||||
} = req.auth;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const { page, filter, pageSize } = req.query;
|
||||
|
||||
const results = await getTeamsByUserId(userId, { page, filter, pageSize: +pageSize || null });
|
||||
|
||||
return ok(res, results);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
if (!(await canCreateTeam(req.auth))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { name } = req.body;
|
||||
|
||||
const team = await createTeam(
|
||||
{
|
||||
id: uuid(),
|
||||
name,
|
||||
accessCode: getRandomChars(16),
|
||||
},
|
||||
userId,
|
||||
);
|
||||
|
||||
return ok(res, team);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue