import { NextApiRequest, NextApiResponse } from 'next' import nc from 'next-connect' import { get, put } from '@utils/Query' import ResponseWrapper from '@utils/ResponseWrapper' import { checkToken } from '@utils/Csrf' import { AddBotSubmit, AddBotSubmitSchema } from '@utils/Yup' const BotInfo = nc() .get(async(req, res) => { const bot = await get.bot.load(req.query.id) if(!bot) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 봇입니다.' }) else return ResponseWrapper(res, { code: 200, data: bot }) }) .post(async (req, res) => { const user = await get.Authorization(req.cookies.token) console.log(user) if(!user) return ResponseWrapper(res, { code: 401 }) const csrfValidated = checkToken(req, res, req.body._csrf) if(!csrfValidated) return const validated = await AddBotSubmitSchema.validate(req.body, { abortEarly: false }).then(el => el).catch(e => { ResponseWrapper(res, { code: 400, errors: e.errors }) return null }) if(!validated) return if(validated.id !== req.query.id) return ResponseWrapper(res, { code: 400, errors: ['요청 주소와 Body의 정보가 다릅니다.'] }) const result = await put.submitBot(user, validated) if(result === 1) return ResponseWrapper(res, { code: 403, message: '이미 대기중인 봇이 있습니다.', errors: ['한 번에 최대 2개의 봇까지만 신청하실 수 있습니다.\n다른 봇들의 심사가 완료된 뒤에 신청해주세요.'] }) else if(result === 2) return ResponseWrapper(res, { code: 406, message: '해당 봇은 이미 심사중입니다.', errors: ['해당 아이디의 봇은 이미 심사중입니다. 본인 소유의 봇이고 신청하신 적이 없으시다면 문의해주세요.'] }) return ResponseWrapper(res, { code: 200, data: result }) }) .patch(async (req, res) => { return res.send('Reserved') }) interface ApiRequest extends NextApiRequest { body: AddBotSubmit query: { id: string } } export default BotInfo