File size: 1,587 Bytes
24d6f21
 
 
 
 
 
e954f42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24d6f21
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 axios from 'axios';
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
import { SangMata } from '../models.js';
const SangmataRoutes = express.Router();

SangmataRoutes.post("/api/v2/sangmata/tracker", authenticateApiKey, async (req, res) => {
  try {
    const { user_id, username, first_name } = req.body;

    if (!user_id || !first_name) {
      return res.status(400).json({ error: "User ID and First Name are required" });
    }

    let user = await SangMata.findOne({ user_id });

    if (!user) {
      user = new SangMata({ user_id, username, first_name: [first_name] });
    } else {
      user.username = username;

      if (!user.first_name.includes(first_name)) {
        user.first_name.push(first_name);
      }
    }
    await user.save();
    res.json({ success: true, message: "User data updated successfully", user });

  } catch (error) {
    res.status(500).json({ error: `Failed to track user: ${error.message}` });
  }
});

SangmataRoutes.get("/api/v2/sangmata/tracker/:user_id", authenticateApiKey, async (req, res) => {
  try {
    const { user_id } = req.params;
    const user = await SangMata.findOne({ user_id });

    if (!user) {
      return res.status(404).json({ error: "User not found" });
    }

    res.json({
      user_id: user.user_id,
      username: user.username || "No username recorded",
      first_name_history: user.first_name
    });

  } catch (error) {
    res.status(500).json({ error: `Failed to fetch user data: ${error.message}` });
  }
});

export { SangmataRoutes };