File size: 5,208 Bytes
24d6f21 460daf5 2e3802a e746c10 2e3802a 460daf5 2e3802a 460daf5 2e3802a 460daf5 2e3802a 460daf5 2e3802a 460daf5 2e3802a 460daf5 2e3802a 460daf5 2e3802a 460daf5 4f33a0b e954f42 93d75fb 054e177 93d75fb 4f33a0b 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
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 }; |