2022-12-07 03:36:41 +01:00
|
|
|
import { canViewTeam } from 'lib/auth';
|
|
|
|
import { useAuth } from 'lib/middleware';
|
2023-03-09 21:42:12 +01:00
|
|
|
import { NextApiRequestQueryBody } from 'lib/types';
|
|
|
|
import { NextApiResponse } from 'next';
|
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
|
|
import { createTeamWebsites, getTeamWebsites } from 'queries/admin/teamWebsite';
|
2022-11-18 09:27:42 +01:00
|
|
|
|
|
|
|
export interface TeamWebsiteRequestQuery {
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TeamWebsiteRequestBody {
|
2023-02-02 12:30:09 +01:00
|
|
|
teamWebsiteId?: string;
|
2023-03-09 21:42:12 +01:00
|
|
|
websiteIds?: string[];
|
2022-11-18 09:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async (
|
|
|
|
req: NextApiRequestQueryBody<TeamWebsiteRequestQuery, TeamWebsiteRequestBody>,
|
|
|
|
res: NextApiResponse,
|
|
|
|
) => {
|
|
|
|
await useAuth(req, res);
|
|
|
|
|
|
|
|
const { id: teamId } = req.query;
|
2023-03-09 21:42:12 +01:00
|
|
|
const {
|
|
|
|
user: { id: userId },
|
|
|
|
} = req.auth;
|
2022-11-18 09:27:42 +01:00
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
2023-01-25 16:42:46 +01:00
|
|
|
if (!(await canViewTeam(req.auth, teamId))) {
|
2022-11-20 09:48:13 +01:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
const websites = await getTeamWebsites(teamId);
|
2022-11-18 09:27:42 +01:00
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
return ok(res, websites);
|
2022-11-18 09:27:42 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 21:42:12 +01: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 09:27:42 +01:00
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|