File size: 1,872 Bytes
03a92a9
 
 
 
 
d9d9e44
03a92a9
 
d9d9e44
 
03a92a9
d9d9e44
03a92a9
d9d9e44
03a92a9
 
d9d9e44
 
 
03a92a9
 
 
 
 
d9d9e44
 
 
 
 
 
03a92a9
d9d9e44
03a92a9
d9d9e44
03a92a9
d9d9e44
 
 
03a92a9
d9d9e44
 
 
03a92a9
d9d9e44
03a92a9
d9d9e44
 
03a92a9
d9d9e44
03a92a9
 
 
d9d9e44
03a92a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 };