import express from 'express'; | |
import axios from 'axios'; | |
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js'; | |
import { SangMata } from '../models.js'; | |
const SangmataRoutes = express.Router(); | |
/** | |
* @swagger | |
* /api/v2/sangmata/tracker: | |
* post: | |
* summary: Track and update a user's first name history | |
* tags: [Sangmata] | |
* description: Updates or creates a record tracking a user's first name changes. | |
* requestBody: | |
* required: true | |
* content: | |
* application/json: | |
* schema: | |
* type: object | |
* properties: | |
* user_id: | |
* type: integer | |
* description: Unique Telegram user ID. | |
* example: 123456789 | |
* username: | |
* type: string | |
* description: User's latest Telegram username. | |
* example: "new_user" | |
* first_name: | |
* type: string | |
* description: New first name to be added to history. | |
* example: "John" | |
* parameters: | |
* - in: header | |
* name: x-api-key | |
* required: true | |
* description: API key for authentication. | |
* schema: | |
* type: string | |
* responses: | |
* 200: | |
* description: User data updated successfully. | |
* content: | |
* application/json: | |
* schema: | |
* type: object | |
* properties: | |
* success: | |
* type: boolean | |
* example: true | |
* message: | |
* type: string | |
* example: "User data updated successfully" | |
* user: | |
* type: object | |
* properties: | |
* user_id: | |
* type: integer | |
* example: 123456789 | |
* username: | |
* type: string | |
* example: "new_user" | |
* first_name: | |
* type: array | |
* items: | |
* type: string | |
* example: ["John", "Johnny", "Jonathan"] | |
* 400: | |
* description: Missing required parameters or invalid data. | |
* 500: | |
* description: Internal server error. | |
*/ | |
SangmataRoutes.post("/api/v2/sangmata/tracker", authenticateApiKey, apiLimiter, 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}` }); | |
} | |
}); | |
/** | |
* @swagger | |
* /api/v2/sangmata/tracker/{user_id}: | |
* get: | |
* summary: Retrieve a user's first name history | |
* tags: [Sangmata] | |
* description: Fetches the historical first names and latest username of a tracked user. | |
* parameters: | |
* - in: header | |
* name: x-api-key | |
* required: true | |
* description: API key for authentication. | |
* schema: | |
* type: string | |
* - in: path | |
* name: user_id | |
* required: true | |
* description: The Telegram user ID to fetch tracking data for. | |
* schema: | |
* type: integer | |
* example: 123456789 | |
* responses: | |
* 200: | |
* description: Successfully retrieved user tracking data. | |
* content: | |
* application/json: | |
* schema: | |
* type: object | |
* properties: | |
* user_id: | |
* type: integer | |
* example: 123456789 | |
* username: | |
* type: string | |
* example: "new_user" | |
* first_name_history: | |
* type: array | |
* items: | |
* type: string | |
* example: ["John", "Johnny", "Jonathan"] | |
* 404: | |
* description: User not found. | |
* 500: | |
* description: Internal server error. | |
*/ | |
SangmataRoutes.get("/api/v2/sangmata/tracker/:user_id", authenticateApiKey, apiLimiter, 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 }; |