2023-08-20 05:23:15 +00:00
|
|
|
import { canViewTeam } from 'lib/auth';
|
2022-11-18 08:27:42 +00:00
|
|
|
import { useAuth } 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface TeamUserRequestBody {
|
2022-11-23 06:17:49 +00:00
|
|
|
email: string;
|
2022-12-27 01:36:48 +00:00
|
|
|
roleId: string;
|
2022-11-18 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async (
|
|
|
|
|
req: NextApiRequestQueryBody<TeamUserRequestQuery, TeamUserRequestBody>,
|
|
|
|
|
res: NextApiResponse,
|
|
|
|
|
) => {
|
|
|
|
|
await useAuth(req, res);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
};
|