Reaperxxxx commited on
Commit
eedf20a
Β·
verified Β·
1 Parent(s): 355ccc9

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +54 -0
index.js CHANGED
@@ -118,6 +118,60 @@ app.get('/user/:id/banner', async (req, res) => {
118
  }
119
  });
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  // βœ… NEW: GET /user/:id/info β†’ Get user's global info across all groups
122
  app.get('/user/:id/info', async (req, res) => {
123
  const userId = req.params.id;
 
118
  }
119
  });
120
 
121
+ // GET /user/:chatid/nfts β†’ Return all NFTs owned by a user, with holders count
122
+ app.get('/user/:chatid/nfts', async (req, res) => {
123
+ const userId = req.params.chatid;
124
+ const filename = 'data/nfts.json';
125
+
126
+ const client = new MongoClient(MONGO_URI);
127
+
128
+ try {
129
+ await client.connect();
130
+ const db = client.db(DB_NAME);
131
+ const collection = db.collection(COLLECTION_NAME);
132
+
133
+ const doc = await collection.findOne({ filename });
134
+
135
+ if (!doc || !doc.data) {
136
+ return res.status(404).json({ status: false, message: 'NFT data not found' });
137
+ }
138
+
139
+ const nftData = doc.data;
140
+ const userNFTs = nftData[userId];
141
+
142
+ if (!userNFTs || userNFTs.length === 0) {
143
+ return res.status(404).json({ status: false, message: 'No NFTs found for this user' });
144
+ }
145
+
146
+ // Calculate how many users hold each nftid
147
+ const holdersCount = {};
148
+ for (const uid in nftData) {
149
+ const ownedNFTs = nftData[uid];
150
+ for (const nft of ownedNFTs) {
151
+ holdersCount[nft.nftid] = (holdersCount[nft.nftid] || 0) + 1;
152
+ }
153
+ }
154
+
155
+ // Append holders count to each NFT the user owns
156
+ const enrichedNFTs = userNFTs.map(nft => ({
157
+ ...nft,
158
+ holders: holdersCount[nft.nftid] || 1
159
+ }));
160
+
161
+ return res.json({
162
+ status: true,
163
+ userId,
164
+ totalNFTs: enrichedNFTs.length,
165
+ nfts: enrichedNFTs
166
+ });
167
+
168
+ } catch (err) {
169
+ return res.status(500).json({ status: false, error: err.message });
170
+ } finally {
171
+ await client.close();
172
+ }
173
+ });
174
+
175
  // βœ… NEW: GET /user/:id/info β†’ Get user's global info across all groups
176
  app.get('/user/:id/info', async (req, res) => {
177
  const userId = req.params.id;