diff --git a/pages/api/v2/bots/[id]/vote.ts b/pages/api/v2/bots/[id]/vote.ts index d3bc2e0..e7c5a9a 100644 --- a/pages/api/v2/bots/[id]/vote.ts +++ b/pages/api/v2/bots/[id]/vote.ts @@ -4,8 +4,22 @@ import { CaptchaVerify, get, put } from '@utils/Query' import RequestHandler from '@utils/RequestHandler' import ResponseWrapper from '@utils/ResponseWrapper' import { checkToken } from '@utils/Csrf' +import Yup from '@utils/Yup' +import { VOTE_COOLDOWN } from '@utils/Constants' const BotVote = RequestHandler() + .get(async (req: GetApiRequest, res) => { + const bot = await get.BotAuthorization(req.headers.authorization) + if(!bot) return ResponseWrapper(res, { code: 401 }) + if(req.query.id !== bot) return ResponseWrapper(res, { code: 403 }) + const userID = await Yup.string().required().validate(req.query.userID).then(el => el).catch(e => { + ResponseWrapper(res, { code: 400, errors: e.errors }) + return null + }) + if(!userID) return ResponseWrapper(res, { code: 400 }) + const result = await get.botVote(userID, bot) + return ResponseWrapper(res, { code: 200, data: { voted: +new Date() < result + VOTE_COOLDOWN, lastVote: result } }) + }) .post(async (req: PostApiRequest, res) => { const user = await get.Authorization(req.cookies.token) if(!user) return ResponseWrapper(res, { code: 401 }) @@ -22,14 +36,22 @@ const BotVote = RequestHandler() else return ResponseWrapper(res, { code: 429, data: { retryAfter: vote } }) }) -interface PostApiRequest extends NextApiRequest { +interface ApiRequest extends NextApiRequest { query: { id: string } - body: { +} + +interface GetApiRequest extends ApiRequest { + query: { + id: string + userID: string + } +} +interface PostApiRequest extends ApiRequest { + body: { _captcha: string _csrf: string } } - export default BotVote \ No newline at end of file