Update server.js
Browse files
server.js
CHANGED
|
@@ -9,7 +9,6 @@ const CODES_FILE = "codes.json";
|
|
| 9 |
function generateCode() {
|
| 10 |
return Math.random().toString(36).substring(2, 8).toUpperCase();
|
| 11 |
}
|
| 12 |
-
|
| 13 |
// Function to get current Unix timestamp
|
| 14 |
function getUnixTimestamp() {
|
| 15 |
return Math.floor(Date.now() / 1000);
|
|
@@ -20,6 +19,61 @@ if (!fs.existsSync(CODES_FILE)) {
|
|
| 20 |
fs.writeFileSync(CODES_FILE, JSON.stringify({}), "utf8");
|
| 21 |
}
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
// API to generate a new code
|
| 24 |
app.get("/api/generate", (req, res) => {
|
| 25 |
const chatId = req.query.chatid;
|
|
|
|
| 9 |
function generateCode() {
|
| 10 |
return Math.random().toString(36).substring(2, 8).toUpperCase();
|
| 11 |
}
|
|
|
|
| 12 |
// Function to get current Unix timestamp
|
| 13 |
function getUnixTimestamp() {
|
| 14 |
return Math.floor(Date.now() / 1000);
|
|
|
|
| 19 |
fs.writeFileSync(CODES_FILE, JSON.stringify({}), "utf8");
|
| 20 |
}
|
| 21 |
|
| 22 |
+
const AMOUNTS_FILE = "amounts.json";
|
| 23 |
+
|
| 24 |
+
// Function to get current Unix timestamp
|
| 25 |
+
function getUnixTimestamp() {
|
| 26 |
+
return Math.floor(Date.now() / 1000);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
// Ensure amounts.json exists
|
| 30 |
+
if (!fs.existsSync(AMOUNTS_FILE)) {
|
| 31 |
+
fs.writeFileSync(AMOUNTS_FILE, JSON.stringify({}), "utf8");
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
app.get("/add", (req, res) => {
|
| 35 |
+
const chatId = req.query.chatid;
|
| 36 |
+
const amount = parseInt(req.query.amount, 10);
|
| 37 |
+
|
| 38 |
+
if (!chatId || isNaN(amount) || amount <= 0) {
|
| 39 |
+
return res.status(400).json({ error: "Valid chatid and amount are required" });
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
const timestamp = getUnixTimestamp();
|
| 43 |
+
|
| 44 |
+
// Load existing amounts
|
| 45 |
+
let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE, "utf8"));
|
| 46 |
+
|
| 47 |
+
// Store the new amount
|
| 48 |
+
if (!amounts[chatId]) {
|
| 49 |
+
amounts[chatId] = [];
|
| 50 |
+
}
|
| 51 |
+
amounts[chatId].push({ amount, timestamp });
|
| 52 |
+
|
| 53 |
+
// Save to amounts.json
|
| 54 |
+
fs.writeFileSync(AMOUNTS_FILE, JSON.stringify(amounts, null, 2), "utf8");
|
| 55 |
+
|
| 56 |
+
res.json({ chatId, amount, timestamp });
|
| 57 |
+
});
|
| 58 |
+
|
| 59 |
+
// API to get stored amounts for a chat ID
|
| 60 |
+
app.get("/get", (req, res) => {
|
| 61 |
+
const chatId = req.query.chatid;
|
| 62 |
+
|
| 63 |
+
if (!chatId) {
|
| 64 |
+
return res.status(400).json({ error: "chatid is required" });
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Load existing amounts
|
| 68 |
+
let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE, "utf8"));
|
| 69 |
+
|
| 70 |
+
if (!amounts[chatId]) {
|
| 71 |
+
return res.status(404).json({ error: "No amounts found for this chat ID" });
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
res.json({ chatId, amounts: amounts[chatId] });
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
// API to generate a new code
|
| 78 |
app.get("/api/generate", (req, res) => {
|
| 79 |
const chatId = req.query.chatid;
|