randydev commited on
Commit
e954f42
·
verified ·
1 Parent(s): 24d6f21

Update plugins/sangmata.js

Browse files
Files changed (1) hide show
  1. plugins/sangmata.js +47 -0
plugins/sangmata.js CHANGED
@@ -4,4 +4,51 @@ import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
4
  import { SangMata } from '../models.js';
5
  const SangmataRoutes = express.Router();
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  export { SangmataRoutes };
 
4
  import { SangMata } from '../models.js';
5
  const SangmataRoutes = express.Router();
6
 
7
+ SangmataRoutes.post("/api/v2/sangmata/tracker", authenticateApiKey, async (req, res) => {
8
+ try {
9
+ const { user_id, username, first_name } = req.body;
10
+
11
+ if (!user_id || !first_name) {
12
+ return res.status(400).json({ error: "User ID and First Name are required" });
13
+ }
14
+
15
+ let user = await SangMata.findOne({ user_id });
16
+
17
+ if (!user) {
18
+ user = new SangMata({ user_id, username, first_name: [first_name] });
19
+ } else {
20
+ user.username = username;
21
+
22
+ if (!user.first_name.includes(first_name)) {
23
+ user.first_name.push(first_name);
24
+ }
25
+ }
26
+ await user.save();
27
+ res.json({ success: true, message: "User data updated successfully", user });
28
+
29
+ } catch (error) {
30
+ res.status(500).json({ error: `Failed to track user: ${error.message}` });
31
+ }
32
+ });
33
+
34
+ SangmataRoutes.get("/api/v2/sangmata/tracker/:user_id", authenticateApiKey, async (req, res) => {
35
+ try {
36
+ const { user_id } = req.params;
37
+ const user = await SangMata.findOne({ user_id });
38
+
39
+ if (!user) {
40
+ return res.status(404).json({ error: "User not found" });
41
+ }
42
+
43
+ res.json({
44
+ user_id: user.user_id,
45
+ username: user.username || "No username recorded",
46
+ first_name_history: user.first_name
47
+ });
48
+
49
+ } catch (error) {
50
+ res.status(500).json({ error: `Failed to fetch user data: ${error.message}` });
51
+ }
52
+ });
53
+
54
  export { SangmataRoutes };