2023-09-22 07:59:00 +00:00
|
|
|
import * as yup from 'yup';
|
2023-08-20 05:23:15 +00:00
|
|
|
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
2023-09-27 06:20:29 +00:00
|
|
|
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
|
2023-09-22 07:59:00 +00:00
|
|
|
import { pageInfo } from 'lib/schema';
|
2023-07-27 18:43:45 +00:00
|
|
|
import { NextApiResponse } from 'next';
|
|
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
2024-01-30 08:10:25 +00:00
|
|
|
import { getUserTeams } from 'queries';
|
2023-09-22 07:59:00 +00:00
|
|
|
|
2023-09-27 06:20:29 +00:00
|
|
|
export interface UserTeamsRequestQuery extends SearchFilter {
|
2024-02-07 18:26:36 +00:00
|
|
|
userId: string;
|
2023-08-11 05:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UserTeamsRequestBody {
|
2023-07-27 18:43:45 +00:00
|
|
|
name: string;
|
|
|
|
|
domain: string;
|
|
|
|
|
shareId: string;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 05:23:15 +00:00
|
|
|
const schema = {
|
|
|
|
|
GET: yup.object().shape({
|
2024-02-07 18:26:36 +00:00
|
|
|
userId: yup.string().uuid().required(),
|
2023-09-22 07:59:00 +00:00
|
|
|
...pageInfo,
|
2023-08-20 05:23:15 +00:00
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-27 18:43:45 +00:00
|
|
|
export default async (
|
2024-02-07 18:26:36 +00:00
|
|
|
req: NextApiRequestQueryBody<UserTeamsRequestQuery, UserTeamsRequestBody>,
|
2023-07-27 18:43:45 +00:00
|
|
|
res: NextApiResponse,
|
|
|
|
|
) => {
|
|
|
|
|
await useCors(req, res);
|
|
|
|
|
await useAuth(req, res);
|
2023-09-30 03:24:48 +00:00
|
|
|
await useValidate(schema, req, res);
|
2023-08-20 05:23:15 +00:00
|
|
|
|
2023-07-27 18:43:45 +00:00
|
|
|
const { user } = req.auth;
|
2024-02-17 02:58:15 +00:00
|
|
|
const { userId } = req.query;
|
2023-07-27 18:43:45 +00:00
|
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
2024-02-17 02:58:15 +00:00
|
|
|
if (!user.isAdmin && (!userId || user.id !== userId)) {
|
2023-07-27 18:43:45 +00:00
|
|
|
return unauthorized(res);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 07:59:00 +00:00
|
|
|
const { page, query, pageSize } = req.query;
|
2023-08-11 05:50:41 +00:00
|
|
|
|
2024-02-07 18:26:36 +00:00
|
|
|
const teams = await getUserTeams(userId as string, {
|
2023-09-22 07:59:00 +00:00
|
|
|
query,
|
2023-08-11 05:50:41 +00:00
|
|
|
page,
|
2024-02-05 23:32:36 +00:00
|
|
|
pageSize,
|
2023-08-11 05:50:41 +00:00
|
|
|
});
|
2023-07-27 18:43:45 +00:00
|
|
|
|
|
|
|
|
return ok(res, teams);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
|
};
|