mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-16 02:05:04 +01:00
06bebadbb9
* Auth checkpoint. * Merge branch 'dev' into feat/um-114-roles-and-permissions
62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
import { Team } from '@prisma/client';
|
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
|
import { canDeleteTeam, canUpdateTeam, canViewTeam } from 'lib/auth';
|
|
import { useAuth } from 'lib/middleware';
|
|
import { NextApiResponse } from 'next';
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
import { deleteTeam, getTeam, updateTeam } from 'queries';
|
|
|
|
export interface TeamRequestQuery {
|
|
id: string;
|
|
}
|
|
|
|
export interface TeamRequestBody {
|
|
name: string;
|
|
}
|
|
|
|
export default async (
|
|
req: NextApiRequestQueryBody<TeamRequestQuery, TeamRequestBody>,
|
|
res: NextApiResponse<Team>,
|
|
) => {
|
|
await useAuth(req, res);
|
|
|
|
const {
|
|
user: { id: userId },
|
|
} = req.auth;
|
|
const { id: teamId } = req.query;
|
|
|
|
if (req.method === 'GET') {
|
|
if (await canViewTeam(userId, teamId)) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
const user = await getTeam({ id: teamId });
|
|
|
|
return ok(res, user);
|
|
}
|
|
|
|
if (req.method === 'POST') {
|
|
const { name } = req.body;
|
|
|
|
if (await canUpdateTeam(userId, teamId)) {
|
|
return unauthorized(res, 'You must be the owner of this team.');
|
|
}
|
|
|
|
const updated = await updateTeam({ name }, { id: teamId });
|
|
|
|
return ok(res, updated);
|
|
}
|
|
|
|
if (req.method === 'DELETE') {
|
|
if (await canDeleteTeam(userId, teamId)) {
|
|
return unauthorized(res, 'You must be the owner of this team.');
|
|
}
|
|
|
|
await deleteTeam(teamId);
|
|
|
|
return ok(res);
|
|
}
|
|
|
|
return methodNotAllowed(res);
|
|
};
|