umami/pages/api/teams/[id]/website.ts
Brian Cao 06bebadbb9
Feat/um 114 roles and permissions (#1683)
* Auth checkpoint.

* Merge branch 'dev' into feat/um-114-roles-and-permissions
2022-12-01 20:53:37 -08:00

40 lines
951 B
TypeScript

import { NextApiRequestQueryBody } from 'interface/api/nextApi';
import { canViewTeam } from 'lib/auth';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsitesByTeamId } from 'queries';
export interface TeamWebsiteRequestQuery {
id: string;
}
export interface TeamWebsiteRequestBody {
website_id: string;
team_website_id?: string;
}
export default async (
req: NextApiRequestQueryBody<TeamWebsiteRequestQuery, TeamWebsiteRequestBody>,
res: NextApiResponse,
) => {
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 website = await getWebsitesByTeamId({ teamId });
return ok(res, website);
}
return methodNotAllowed(res);
};