mirror of
https://github.com/umami-software/umami.git
synced 2026-02-12 16:45:35 +01:00
64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import * as yup from 'yup';
|
|
import { canViewTeam } from 'lib/auth';
|
|
import { useAuth, useValidate } from 'lib/middleware';
|
|
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
|
|
import { pageInfo } from 'lib/schema';
|
|
import { NextApiResponse } from 'next';
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
import { getWebsitesByTeamId } from 'queries';
|
|
import { createTeamWebsites } from 'queries/admin/teamWebsite';
|
|
|
|
export interface TeamWebsiteRequestQuery extends SearchFilter {
|
|
id: string;
|
|
}
|
|
|
|
export interface TeamWebsiteRequestBody {
|
|
websiteIds?: string[];
|
|
}
|
|
|
|
const schema = {
|
|
GET: yup.object().shape({
|
|
id: yup.string().uuid().required(),
|
|
...pageInfo,
|
|
}),
|
|
POST: yup.object().shape({
|
|
id: yup.string().uuid().required(),
|
|
websiteIds: yup.array().of(yup.string()).min(1).required(),
|
|
}),
|
|
};
|
|
|
|
export default async (
|
|
req: NextApiRequestQueryBody<TeamWebsiteRequestQuery, TeamWebsiteRequestBody>,
|
|
res: NextApiResponse,
|
|
) => {
|
|
await useAuth(req, res);
|
|
|
|
req.yup = schema;
|
|
await useValidate(req, res);
|
|
|
|
const { id: teamId } = req.query;
|
|
|
|
if (req.method === 'GET') {
|
|
if (!(await canViewTeam(req.auth, teamId))) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
const websites = await getWebsitesByTeamId(teamId, { ...req.query });
|
|
|
|
return ok(res, websites);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
return methodNotAllowed(res);
|
|
};
|