import { NextApiResponse } from 'next'; import { ApiMethods, ApiRoutes, NextRequestWithBody, } from '../../src/model/api'; import { CustomResponse } from '../../src/model/common'; import { NewTeam, Team } from '../../src/model/user'; import authenticateRequest from '../../src/utils/api/authenticateRequest'; const createTeam = async ( req: NextRequestWithBody<{ team: string }>, res: NextApiResponse>, ): Promise => { const { team } = req.body; if (!team) { return res.status(200).json({ ok: false, error: 'NO_TEAM_PROVIDED' }); } const teams = await authenticateRequest( ApiMethods.Get, ApiRoutes.GetTeams, ); if (teams.ok && !!teams.data?.length) { const existTeam = teams.data.find((t) => t.name === team); if (existTeam) return res.status(200).json({ ok: false, error: 'DUPLICATE_TEAM_NAME' }); } const response = await authenticateRequest( ApiMethods.Post, ApiRoutes.CreateTeam, { name: team }, ); if (!response.ok) { return res.status(200).json({ ok: false, error: response.error }); } return res.status(200).json({ ok: true, data: response.data.id }); }; export default createTeam;