veltrixcode commited on
Commit
9ed0579
·
verified ·
1 Parent(s): 905749d

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +15 -8
server.js CHANGED
@@ -9,6 +9,7 @@ const axios = require('axios');
9
  const http = require('http');
10
  const socketIo = require('socket.io');
11
  const fs = require('fs');
 
12
 
13
  const app = express();
14
  const server = http.createServer(app);
@@ -18,7 +19,7 @@ const io = socketIo(server, {
18
  methods: ["GET", "POST"]
19
  }
20
  });
21
- const port = 7860;
22
 
23
  // Middleware
24
  app.use(cors());
@@ -314,7 +315,8 @@ app.get('/api/user-submissions/:userId', checkOtpAuth, async (req, res) => {
314
  const { userId } = req.params;
315
  try {
316
  const db = await getDb();
317
- const submissions = await db.collection('submissions').find({ user_id: userId }).sort({ created_at: -1 }).toArray();
 
318
  const mappedSubs = submissions.map(sub => {
319
  const payment_address = sub.payment_address || sub.upi_id || 'oldsub';
320
  const mapped = {
@@ -342,8 +344,9 @@ app.post('/api/gift-cards', checkOtpAuth, async (req, res) => {
342
  }
343
  try {
344
  const db = await getDb();
 
345
  const result = await db.collection('submissions').insertOne({
346
- user_id: userId,
347
  ticket_user_name,
348
  gc_code,
349
  gc_phone,
@@ -403,7 +406,8 @@ app.get('/api/messages/:userId', checkOtpAuth, async (req, res) => {
403
  if (!user || user._id.toString() !== userId) {
404
  return res.status(403).json({ success: false, error: 'Unauthorized access' });
405
  }
406
- const messages = await db.collection('messages').find({ user_id: userId }).sort({ created_at: 1 }).toArray();
 
407
  res.json({ success: true, messages });
408
  } catch (error) {
409
  console.error('Error fetching messages:', error);
@@ -424,7 +428,8 @@ app.post('/api/messages', checkOtpAuth, async (req, res) => {
424
  if (!user || user._id.toString() !== userId) {
425
  return res.status(403).json({ success: false, error: 'Unauthorized access' });
426
  }
427
- const result = await db.collection('messages').insertOne({ user_id: userId, content, sender, created_at: new Date() });
 
428
  res.json({ success: true, message: result.ops?.[0] || { user_id: userId, content, sender } });
429
  } catch (error) {
430
  console.error('Error sending message:', error);
@@ -459,7 +464,8 @@ app.get('/api/admin/messages/:userId', async (req, res) => {
459
  const { userId } = req.params;
460
  try {
461
  const db = await getDb();
462
- const messages = await db.collection('messages').find({ user_id: userId }).sort({ created_at: 1 }).toArray();
 
463
  res.json({ success: true, messages });
464
  } catch (error) {
465
  console.error('Error fetching messages:', error);
@@ -495,10 +501,11 @@ app.delete('/api/admin/users/:userId', async (req, res) => {
495
  const { userId } = req.params;
496
  try {
497
  const db = await getDb();
 
498
  // First delete all messages for the user
499
- await db.collection('messages').deleteMany({ user_id: userId });
500
  // Then delete the user
501
- await db.collection('users').deleteOne({ _id: userId });
502
  res.json({ success: true });
503
  } catch (error) {
504
  console.error('Error deleting user:', error);
 
9
  const http = require('http');
10
  const socketIo = require('socket.io');
11
  const fs = require('fs');
12
+ const { ObjectId } = require('mongodb');
13
 
14
  const app = express();
15
  const server = http.createServer(app);
 
19
  methods: ["GET", "POST"]
20
  }
21
  });
22
+ const port = 3000;
23
 
24
  // Middleware
25
  app.use(cors());
 
315
  const { userId } = req.params;
316
  try {
317
  const db = await getDb();
318
+ const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
319
+ const submissions = await db.collection('submissions').find({ user_id: userIdObj }).sort({ created_at: -1 }).toArray();
320
  const mappedSubs = submissions.map(sub => {
321
  const payment_address = sub.payment_address || sub.upi_id || 'oldsub';
322
  const mapped = {
 
344
  }
345
  try {
346
  const db = await getDb();
347
+ const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
348
  const result = await db.collection('submissions').insertOne({
349
+ user_id: userIdObj,
350
  ticket_user_name,
351
  gc_code,
352
  gc_phone,
 
406
  if (!user || user._id.toString() !== userId) {
407
  return res.status(403).json({ success: false, error: 'Unauthorized access' });
408
  }
409
+ const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
410
+ const messages = await db.collection('messages').find({ user_id: userIdObj }).sort({ created_at: 1 }).toArray();
411
  res.json({ success: true, messages });
412
  } catch (error) {
413
  console.error('Error fetching messages:', error);
 
428
  if (!user || user._id.toString() !== userId) {
429
  return res.status(403).json({ success: false, error: 'Unauthorized access' });
430
  }
431
+ const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
432
+ const result = await db.collection('messages').insertOne({ user_id: userIdObj, content, sender, created_at: new Date() });
433
  res.json({ success: true, message: result.ops?.[0] || { user_id: userId, content, sender } });
434
  } catch (error) {
435
  console.error('Error sending message:', error);
 
464
  const { userId } = req.params;
465
  try {
466
  const db = await getDb();
467
+ const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
468
+ const messages = await db.collection('messages').find({ user_id: userIdObj }).sort({ created_at: 1 }).toArray();
469
  res.json({ success: true, messages });
470
  } catch (error) {
471
  console.error('Error fetching messages:', error);
 
501
  const { userId } = req.params;
502
  try {
503
  const db = await getDb();
504
+ const userIdObj = ObjectId.isValid(userId) ? new ObjectId(userId) : userId;
505
  // First delete all messages for the user
506
+ await db.collection('messages').deleteMany({ user_id: userIdObj });
507
  // Then delete the user
508
+ await db.collection('users').deleteOne({ _id: userIdObj });
509
  res.json({ success: true });
510
  } catch (error) {
511
  console.error('Error deleting user:', error);