Reaperxxxx commited on
Commit
dab771e
·
verified ·
1 Parent(s): d6926a1

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +46 -0
index.js ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const { MongoClient } = require('mongodb');
3
+ require('dotenv').config(); // Load .env variables
4
+
5
+ const MONGO_URI = process.env.MONGO_URI;
6
+ const DB_NAME = 'finral';
7
+ const COLLECTION_NAME = 'json';
8
+
9
+ const app = express();
10
+ const PORT = 7860;
11
+
12
+ // GET /group/:id/users → returns group users
13
+ app.get('/group/:id/users', async (req, res) => {
14
+ const groupId = req.params.id;
15
+ const filename = `data/groups/${groupId}.json`;
16
+
17
+ const client = new MongoClient(MONGO_URI);
18
+ try {
19
+ await client.connect();
20
+ const db = client.db(DB_NAME);
21
+ const collection = db.collection(COLLECTION_NAME);
22
+
23
+ const doc = await collection.findOne({ filename });
24
+
25
+ if (!doc) {
26
+ return res.status(404).json({ status: false, message: 'Group not found in MongoDB' });
27
+ }
28
+
29
+ const groupData = doc.data;
30
+ return res.json({
31
+ status: true,
32
+ groupId,
33
+ totalUsers: Object.keys(groupData.users || {}).length,
34
+ users: groupData.users || {},
35
+ lastReset: groupData.lastReset || null
36
+ });
37
+ } catch (err) {
38
+ return res.status(500).json({ status: false, error: err.message });
39
+ } finally {
40
+ await client.close();
41
+ }
42
+ });
43
+
44
+ app.listen(PORT, () => {
45
+ console.log(`Express server running at http://localhost:${PORT}`);
46
+ });