2023-09-27 06:27:55 +00:00
|
|
|
import * as yup from 'yup';
|
2023-08-20 05:23:15 +00:00
|
|
|
import { canViewTeam } from 'lib/auth';
|
2023-09-25 20:19:56 +00:00
|
|
|
import { useAuth, useValidate } from 'lib/middleware';
|
2023-09-27 06:20:29 +00:00
|
|
|
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
|
2022-11-18 08:27:42 +00:00
|
|
|
import { NextApiResponse } from 'next';
|
2023-08-20 05:23:15 +00:00
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
|
|
|
import { getUsersByTeamId } from 'queries';
|
2022-11-18 08:27:42 +00:00
|
|
|
|
2023-09-27 06:20:29 +00:00
|
|
|
export interface TeamUserRequestQuery extends SearchFilter {
|
2022-11-18 08:27:42 +00:00
|
|
|
id: string;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 20:19:56 +00:00
|
|
|
const schema = {
|
|
|
|
|
GET: yup.object().shape({
|
|
|
|
|
id: yup.string().uuid().required(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
2022-11-18 08:27:42 +00:00
|
|
|
|
|
|
|
|
export default async (
|
2023-09-25 20:19:56 +00:00
|
|
|
req: NextApiRequestQueryBody<TeamUserRequestQuery, any>,
|
2022-11-18 08:27:42 +00:00
|
|
|
res: NextApiResponse,
|
|
|
|
|
) => {
|
|
|
|
|
await useAuth(req, res);
|
2023-09-30 03:24:48 +00:00
|
|
|
await useValidate(schema, req, res);
|
2022-11-18 08:27:42 +00:00
|
|
|
|
|
|
|
|
const { id: teamId } = req.query;
|
|
|
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
2022-12-27 23:18:58 +00:00
|
|
|
if (!(await canViewTeam(req.auth, teamId))) {
|
2022-11-20 08:48:13 +00:00
|
|
|
return unauthorized(res);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 06:20:29 +00:00
|
|
|
const { query, page } = req.query;
|
2023-08-10 20:26:33 +00:00
|
|
|
|
|
|
|
|
const users = await getUsersByTeamId(teamId, {
|
2023-09-27 06:20:29 +00:00
|
|
|
query,
|
2023-08-10 20:26:33 +00:00
|
|
|
page,
|
|
|
|
|
});
|
2022-11-18 08:27:42 +00:00
|
|
|
|
2022-12-07 02:36:41 +00:00
|
|
|
return ok(res, users);
|
2022-11-18 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
|
};
|