veltrixcode commited on
Commit
aa7dcdc
·
verified ·
1 Parent(s): 3029b18

Upload 10 files

Browse files
Files changed (5) hide show
  1. CHANGELOG.md +45 -0
  2. dbSecondary.js +16 -0
  3. package-lock.json +92 -0
  4. package.json +1 -0
  5. server.js +13 -11
CHANGELOG.md ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 3. Endpoints and Data Model Changes
2
+
3
+ #### /api/register
4
+ - Registers a new user in the `users` collection.
5
+ - Returns `{ id: <ObjectId>, name, email }` in the response.
6
+
7
+ #### /api/login
8
+ - Authenticates user by email and password from MongoDB.
9
+ - Returns `{ id: <ObjectId>, name, email }` in the response.
10
+
11
+ #### /api/gift-cards
12
+ - Submits a gift card entry to the `submissions` collection.
13
+ - `user_id` is now stored as an ObjectId, but accepts both ObjectId string and legacy numeric ID for compatibility.
14
+
15
+ #### /api/user-submissions/:userId
16
+ - Fetches submissions for a user.
17
+ - Accepts both ObjectId string and legacy numeric ID for `userId`.
18
+
19
+ #### /api/messages (POST)
20
+ - Sends a message for a user.
21
+ - Accepts both ObjectId string and legacy numeric ID for `userId`.
22
+ - Response includes `user_id`, `id`, and `_id` for compatibility.
23
+
24
+ #### /api/messages/:userId (GET)
25
+ - Fetches messages for a user.
26
+ - Accepts both ObjectId string and legacy numeric ID for `userId`.
27
+
28
+ #### /api/admin/users, /api/admin/messages, /api/admin/submissions
29
+ - All admin endpoints now use MongoDB queries.
30
+ - Deletion and lookup endpoints accept both ObjectId and legacy numeric IDs where relevant.
31
+
32
+ ### 4. Response Structure
33
+ - All endpoints that return user or message objects now include both `id` (legacy) and `_id` (MongoDB) fields where possible.
34
+ - This ensures compatibility with old clients expecting numeric IDs.
35
+
36
+ ### 5. Other Notes
37
+ - The server no longer uses `.env` or `dotenv` for configuration; all credentials and ports are hardcoded.
38
+ - The server port has changed from 3000 to 7860.
39
+ - MongoDB connection uses the recommended `ServerApiVersion` and logs connection status.
40
+
41
+ ---
42
+
43
+ **If you are updating a client or frontend:**
44
+ - Always use the `id` returned from login/register as `userId` in all requests.
45
+ - Both old numeric IDs and new ObjectId strings are supported for a smooth transition.
dbSecondary.js ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const { MongoClient } = require('mongodb');
2
+
3
+ const uri = 'mongodb+srv://veltrix620:NSwuYBmjWz0vfgFw@cluster0.m5j5psk.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0';
4
+
5
+ let client;
6
+ let db;
7
+
8
+ async function getSecondaryDb() {
9
+ if (db) return db;
10
+ client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
11
+ await client.connect();
12
+ db = client.db(); // Uses the default database
13
+ return db;
14
+ }
15
+
16
+ module.exports = { getSecondaryDb };
package-lock.json CHANGED
@@ -22,6 +22,7 @@
22
  "mongodb": "^6.17.0",
23
  "morgan": "^1.10.0",
24
  "multer": "^1.4.5-lts.1",
 
25
  "nodemailer": "^6.9.5",
26
  "pg": "^8.16.0",
27
  "socket.io": "^4.8.1"
@@ -555,6 +556,15 @@
555
  "node": ">= 0.10"
556
  }
557
  },
 
 
 
 
 
 
 
 
 
558
  "node_modules/debug": {
559
  "version": "2.6.9",
560
  "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@@ -835,6 +845,29 @@
835
  "url": "https://opencollective.com/express"
836
  }
837
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
838
  "node_modules/fill-range": {
839
  "version": "7.1.1",
840
  "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz",
@@ -917,6 +950,18 @@
917
  "node": ">= 6"
918
  }
919
  },
 
 
 
 
 
 
 
 
 
 
 
 
920
  "node_modules/forwarded": {
921
  "version": "0.2.0",
922
  "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
@@ -1659,6 +1704,44 @@
1659
  "node": "^18 || ^20 || >= 21"
1660
  }
1661
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1662
  "node_modules/node-gyp-build": {
1663
  "version": "4.8.4",
1664
  "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
@@ -2571,6 +2654,15 @@
2571
  "node": ">= 0.8"
2572
  }
2573
  },
 
 
 
 
 
 
 
 
 
2574
  "node_modules/webidl-conversions": {
2575
  "version": "7.0.0",
2576
  "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
 
22
  "mongodb": "^6.17.0",
23
  "morgan": "^1.10.0",
24
  "multer": "^1.4.5-lts.1",
25
+ "node-fetch": "^3.3.2",
26
  "nodemailer": "^6.9.5",
27
  "pg": "^8.16.0",
28
  "socket.io": "^4.8.1"
 
556
  "node": ">= 0.10"
557
  }
558
  },
559
+ "node_modules/data-uri-to-buffer": {
560
+ "version": "4.0.1",
561
+ "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
562
+ "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
563
+ "license": "MIT",
564
+ "engines": {
565
+ "node": ">= 12"
566
+ }
567
+ },
568
  "node_modules/debug": {
569
  "version": "2.6.9",
570
  "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
 
845
  "url": "https://opencollective.com/express"
846
  }
847
  },
848
+ "node_modules/fetch-blob": {
849
+ "version": "3.2.0",
850
+ "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",
851
+ "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",
852
+ "funding": [
853
+ {
854
+ "type": "github",
855
+ "url": "https://github.com/sponsors/jimmywarting"
856
+ },
857
+ {
858
+ "type": "paypal",
859
+ "url": "https://paypal.me/jimmywarting"
860
+ }
861
+ ],
862
+ "license": "MIT",
863
+ "dependencies": {
864
+ "node-domexception": "^1.0.0",
865
+ "web-streams-polyfill": "^3.0.3"
866
+ },
867
+ "engines": {
868
+ "node": "^12.20 || >= 14.13"
869
+ }
870
+ },
871
  "node_modules/fill-range": {
872
  "version": "7.1.1",
873
  "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz",
 
950
  "node": ">= 6"
951
  }
952
  },
953
+ "node_modules/formdata-polyfill": {
954
+ "version": "4.0.10",
955
+ "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
956
+ "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
957
+ "license": "MIT",
958
+ "dependencies": {
959
+ "fetch-blob": "^3.1.2"
960
+ },
961
+ "engines": {
962
+ "node": ">=12.20.0"
963
+ }
964
+ },
965
  "node_modules/forwarded": {
966
  "version": "0.2.0",
967
  "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
 
1704
  "node": "^18 || ^20 || >= 21"
1705
  }
1706
  },
1707
+ "node_modules/node-domexception": {
1708
+ "version": "1.0.0",
1709
+ "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
1710
+ "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
1711
+ "deprecated": "Use your platform's native DOMException instead",
1712
+ "funding": [
1713
+ {
1714
+ "type": "github",
1715
+ "url": "https://github.com/sponsors/jimmywarting"
1716
+ },
1717
+ {
1718
+ "type": "github",
1719
+ "url": "https://paypal.me/jimmywarting"
1720
+ }
1721
+ ],
1722
+ "license": "MIT",
1723
+ "engines": {
1724
+ "node": ">=10.5.0"
1725
+ }
1726
+ },
1727
+ "node_modules/node-fetch": {
1728
+ "version": "3.3.2",
1729
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
1730
+ "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
1731
+ "license": "MIT",
1732
+ "dependencies": {
1733
+ "data-uri-to-buffer": "^4.0.0",
1734
+ "fetch-blob": "^3.1.4",
1735
+ "formdata-polyfill": "^4.0.10"
1736
+ },
1737
+ "engines": {
1738
+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1739
+ },
1740
+ "funding": {
1741
+ "type": "opencollective",
1742
+ "url": "https://opencollective.com/node-fetch"
1743
+ }
1744
+ },
1745
  "node_modules/node-gyp-build": {
1746
  "version": "4.8.4",
1747
  "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
 
2654
  "node": ">= 0.8"
2655
  }
2656
  },
2657
+ "node_modules/web-streams-polyfill": {
2658
+ "version": "3.3.3",
2659
+ "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
2660
+ "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
2661
+ "license": "MIT",
2662
+ "engines": {
2663
+ "node": ">= 8"
2664
+ }
2665
+ },
2666
  "node_modules/webidl-conversions": {
2667
  "version": "7.0.0",
2668
  "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
package.json CHANGED
@@ -31,6 +31,7 @@
31
  "mongodb": "^6.17.0",
32
  "morgan": "^1.10.0",
33
  "multer": "^1.4.5-lts.1",
 
34
  "nodemailer": "^6.9.5",
35
  "pg": "^8.16.0",
36
  "socket.io": "^4.8.1"
 
31
  "mongodb": "^6.17.0",
32
  "morgan": "^1.10.0",
33
  "multer": "^1.4.5-lts.1",
34
+ "node-fetch": "^3.3.2",
35
  "nodemailer": "^6.9.5",
36
  "pg": "^8.16.0",
37
  "socket.io": "^4.8.1"
server.js CHANGED
@@ -4,6 +4,7 @@ const path = require('path');
4
  const bcrypt = require('bcrypt');
5
  const multer = require('multer');
6
  const { getDb } = require('./db');
 
7
  const otpService = require('./otpService');
8
  const axios = require('axios');
9
  const http = require('http');
@@ -401,8 +402,7 @@ app.get('/api/messages/:userId', checkOtpAuth, async (req, res) => {
401
  const { userId } = req.params;
402
  const userEmail = req.headers['x-user-email'];
403
  try {
404
- const db = await getDb();
405
- const user = await db.collection('users').findOne({ email: userEmail });
406
  // Allow legacy numeric id or ObjectId string
407
  const userIdStr = userId.toString();
408
  const userIdObj = ObjectId.isValid(userIdStr) ? new ObjectId(userIdStr) : userIdStr;
@@ -425,8 +425,9 @@ app.post('/api/messages', checkOtpAuth, async (req, res) => {
425
  return res.status(400).json({ success: false, error: 'Content and sender are required' });
426
  }
427
  try {
428
- const db = await getDb();
429
- const user = await db.collection('users').findOne({ email: userEmail });
 
430
  const userIdStr = userId.toString();
431
  const userIdObj = ObjectId.isValid(userIdStr) ? new ObjectId(userIdStr) : userIdStr;
432
  if (!user || (user._id.toString() !== userIdStr && user.id?.toString() !== userIdStr)) {
@@ -454,7 +455,7 @@ app.get('/api/admin/users', async (req, res) => {
454
 
455
  app.get('/api/admin/messages/all', async (req, res) => {
456
  try {
457
- const db = await getDb();
458
  const messages = await db.collection('messages').find({}).sort({ created_at: -1 }).toArray();
459
  res.json({ success: true, messages });
460
  } catch (error) {
@@ -466,7 +467,7 @@ app.get('/api/admin/messages/all', async (req, res) => {
466
  app.get('/api/admin/messages/:userId', async (req, res) => {
467
  const { userId } = req.params;
468
  try {
469
- const db = await getDb();
470
  const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
471
  const messages = await db.collection('messages').find({ user_id: userIdObj }).sort({ created_at: 1 }).toArray();
472
  res.json({ success: true, messages });
@@ -479,7 +480,7 @@ app.get('/api/admin/messages/:userId', async (req, res) => {
479
  app.post('/api/admin/messages', async (req, res) => {
480
  try {
481
  const { userId, content } = req.body;
482
- const db = await getDb();
483
  // Convert userId to ObjectId
484
  const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
485
  const result = await db.collection('messages').insertOne({ user_id: userIdObj, content, sender: 'admin', created_at: new Date() });
@@ -493,7 +494,7 @@ app.post('/api/admin/messages', async (req, res) => {
493
  app.delete('/api/admin/messages/:messageId', async (req, res) => {
494
  const { messageId } = req.params;
495
  try {
496
- const db = await getDb();
497
  // Convert messageId to ObjectId
498
  const messageIdObj = ObjectId.isValid(messageId) ? new ObjectId(messageId) : messageId;
499
  await db.collection('messages').deleteOne({ _id: messageIdObj });
@@ -508,10 +509,11 @@ app.delete('/api/admin/users/:userId', async (req, res) => {
508
  const { userId } = req.params;
509
  try {
510
  const db = await getDb();
 
511
  const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
512
- // First delete all messages for the user
513
- await db.collection('messages').deleteMany({ user_id: userIdObj });
514
- // Then delete the user
515
  await db.collection('users').deleteOne({ _id: userIdObj });
516
  res.json({ success: true });
517
  } catch (error) {
 
4
  const bcrypt = require('bcrypt');
5
  const multer = require('multer');
6
  const { getDb } = require('./db');
7
+ const { getSecondaryDb } = require('./dbSecondary');
8
  const otpService = require('./otpService');
9
  const axios = require('axios');
10
  const http = require('http');
 
402
  const { userId } = req.params;
403
  const userEmail = req.headers['x-user-email'];
404
  try {
405
+ const db = await getSecondaryDb();
 
406
  // Allow legacy numeric id or ObjectId string
407
  const userIdStr = userId.toString();
408
  const userIdObj = ObjectId.isValid(userIdStr) ? new ObjectId(userIdStr) : userIdStr;
 
425
  return res.status(400).json({ success: false, error: 'Content and sender are required' });
426
  }
427
  try {
428
+ const db = await getSecondaryDb();
429
+ const mainDb = await getDb();
430
+ const user = await mainDb.collection('users').findOne({ email: userEmail });
431
  const userIdStr = userId.toString();
432
  const userIdObj = ObjectId.isValid(userIdStr) ? new ObjectId(userIdStr) : userIdStr;
433
  if (!user || (user._id.toString() !== userIdStr && user.id?.toString() !== userIdStr)) {
 
455
 
456
  app.get('/api/admin/messages/all', async (req, res) => {
457
  try {
458
+ const db = await getSecondaryDb();
459
  const messages = await db.collection('messages').find({}).sort({ created_at: -1 }).toArray();
460
  res.json({ success: true, messages });
461
  } catch (error) {
 
467
  app.get('/api/admin/messages/:userId', async (req, res) => {
468
  const { userId } = req.params;
469
  try {
470
+ const db = await getSecondaryDb();
471
  const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
472
  const messages = await db.collection('messages').find({ user_id: userIdObj }).sort({ created_at: 1 }).toArray();
473
  res.json({ success: true, messages });
 
480
  app.post('/api/admin/messages', async (req, res) => {
481
  try {
482
  const { userId, content } = req.body;
483
+ const db = await getSecondaryDb();
484
  // Convert userId to ObjectId
485
  const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
486
  const result = await db.collection('messages').insertOne({ user_id: userIdObj, content, sender: 'admin', created_at: new Date() });
 
494
  app.delete('/api/admin/messages/:messageId', async (req, res) => {
495
  const { messageId } = req.params;
496
  try {
497
+ const db = await getSecondaryDb();
498
  // Convert messageId to ObjectId
499
  const messageIdObj = ObjectId.isValid(messageId) ? new ObjectId(messageId) : messageId;
500
  await db.collection('messages').deleteOne({ _id: messageIdObj });
 
509
  const { userId } = req.params;
510
  try {
511
  const db = await getDb();
512
+ const secondaryDb = await getSecondaryDb();
513
  const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
514
+ // First delete all messages for the user from secondary DB
515
+ await secondaryDb.collection('messages').deleteMany({ user_id: userIdObj });
516
+ // Then delete the user from main DB
517
  await db.collection('users').deleteOne({ _id: userIdObj });
518
  res.json({ success: true });
519
  } catch (error) {