ryu-js / plugins /sangmata.js
randydev's picture
Update plugins/sangmata.js
e954f42 verified
raw
history blame
1.59 kB
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 };