Spaces:
Paused
Paused
import express from 'express'; | |
import { Database } from '../database/database.js'; | |
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js'; | |
const AntibanRoutes = express.Router(); | |
AntibanRoutes.post("/api/v1/user/ban-user", authenticateApiKey, apiLimiter, async (req, res) => { | |
const dbClient = new Database("AkenoXJs"); | |
const collection = dbClient.collection("ban_users"); | |
const collectionKey = dbClient.collection("api_keys"); | |
try { | |
const apiKey = req.headers["x-api-key"]; | |
const userIdString = req.query.user_id; | |
const reasonString = req.query.reason; | |
const userIdNumber = Number(userIdString); | |
if (!apiKey) { | |
return res.status(400).json({ error: "Missing API key in headers" }); | |
} | |
if (isNaN(userIdNumber)) { | |
return res.status(400).json({ error: "Invalid or missing user_id" }); | |
} | |
const existingUser = await collection.findOne({ user_ban: userIdNumber }); | |
const existingUserKey = await collectionKey.findOne({ key: apiKey }); | |
if (!existingUserKey) { | |
return res.status(401).json({ message: "API key not found" }); | |
} | |
if (existingUser) { | |
return res.status(200).json({ message: `User is already banned: ${existingUser.user_ban}`, is_ban: true }); | |
} | |
const userDocument = { | |
key: existingUserKey.key, | |
user_ban: userIdNumber, | |
createdAt: new Date(), | |
}; | |
if (reasonString) { | |
userDocument.reason = reasonString; | |
} | |
await collection.insertOne(userDocument); | |
res.json({ message: `User successfully banned: ${userIdNumber}`, is_ban: true }); | |
} catch (error) { | |
res.status(500).json({ error: `Internal server error: ${error.message}` }); | |
} | |
}); | |
export { AntibanRoutes }; |