umami/pages/api/teams/[id]/websites/index.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-12-07 02:36:41 +00:00
import { canViewTeam } from 'lib/auth';
import { useAuth } from 'lib/middleware';
2023-08-10 20:26:33 +00:00
import { NextApiRequestQueryBody, SearchFilter, WebsiteSearchFilterType } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
2023-08-10 20:26:33 +00:00
import { getWebsites, getWebsitesByTeamId } from 'queries';
import { createTeamWebsites } from 'queries/admin/teamWebsite';
2022-11-18 08:27:42 +00:00
2023-08-10 20:26:33 +00:00
export interface TeamWebsiteRequestQuery extends SearchFilter<WebsiteSearchFilterType> {
2022-11-18 08:27:42 +00:00
id: string;
}
export interface TeamWebsiteRequestBody {
websiteIds?: string[];
2022-11-18 08:27:42 +00:00
}
export default async (
req: NextApiRequestQueryBody<TeamWebsiteRequestQuery, TeamWebsiteRequestBody>,
res: NextApiResponse,
) => {
await useAuth(req, res);
const { id: teamId } = req.query;
if (req.method === 'GET') {
2023-01-25 15:42:46 +00:00
if (!(await canViewTeam(req.auth, teamId))) {
2022-11-20 08:48:13 +00:00
return unauthorized(res);
}
2023-08-10 20:26:33 +00:00
const { page, filter, pageSize } = req.query;
const websites = await getWebsitesByTeamId(teamId, {
page,
filter,
pageSize: +pageSize || null,
});
2022-11-18 08:27:42 +00:00
2022-12-07 02:36:41 +00:00
return ok(res, websites);
2022-11-18 08:27:42 +00:00
}
if (req.method === 'POST') {
if (!(await canViewTeam(req.auth, teamId))) {
return unauthorized(res);
}
const { websiteIds } = req.body;
const websites = await createTeamWebsites(teamId, websiteIds);
return ok(res, websites);
}
2022-11-18 08:27:42 +00:00
return methodNotAllowed(res);
};