veltrixcode commited on
Commit
182a656
·
verified ·
1 Parent(s): a0ac627

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +8 -8
server.js CHANGED
@@ -402,8 +402,8 @@ app.get('/api/messages/:userId', checkOtpAuth, async (req, res) => {
402
  const { userId } = req.params;
403
  const userEmail = req.headers['x-user-email'];
404
  try {
405
- const db = await getSecondaryDb();
406
  const mainDb = await getDb();
 
407
  const user = await mainDb.collection('users').findOne({ email: userEmail });
408
  // Allow legacy numeric id or ObjectId string
409
  const userIdStr = userId.toString();
@@ -427,8 +427,8 @@ app.post('/api/messages', checkOtpAuth, async (req, res) => {
427
  return res.status(400).json({ success: false, error: 'Content and sender are required' });
428
  }
429
  try {
430
- const db = await getSecondaryDb();
431
  const mainDb = await getDb();
 
432
  const user = await mainDb.collection('users').findOne({ email: userEmail });
433
  const userIdStr = userId.toString();
434
  const userIdObj = ObjectId.isValid(userIdStr) ? new ObjectId(userIdStr) : userIdStr;
@@ -439,7 +439,7 @@ app.post('/api/messages', checkOtpAuth, async (req, res) => {
439
  res.json({ success: true, message: { ...result.ops?.[0], user_id: userId, id: userId, _id: result.insertedId } });
440
  } catch (error) {
441
  console.error('Error sending message:', error);
442
- res.status(500).json({ success: false, error: 'Failed to send message' });
443
  }
444
  });
445
 
@@ -462,7 +462,7 @@ app.get('/api/admin/messages/all', async (req, res) => {
462
  res.json({ success: true, messages });
463
  } catch (error) {
464
  console.error('Error fetching messages:', error);
465
- res.status(500).json({ success: false, error: 'Failed to fetch messages' });
466
  }
467
  });
468
 
@@ -475,7 +475,7 @@ app.get('/api/admin/messages/:userId', async (req, res) => {
475
  res.json({ success: true, messages });
476
  } catch (error) {
477
  console.error('Error fetching messages:', error);
478
- res.status(500).json({ success: false, error: 'Failed to fetch messages' });
479
  }
480
  });
481
 
@@ -489,7 +489,7 @@ app.post('/api/admin/messages', async (req, res) => {
489
  res.json({ success: true });
490
  } catch (error) {
491
  console.error('Error sending message:', error);
492
- res.status(500).json({ success: false, error: 'Failed to send message' });
493
  }
494
  });
495
 
@@ -503,7 +503,7 @@ app.delete('/api/admin/messages/:messageId', async (req, res) => {
503
  res.json({ success: true });
504
  } catch (error) {
505
  console.error('Error deleting message:', error);
506
- res.status(500).json({ success: false, error: 'Failed to delete message' });
507
  }
508
  });
509
 
@@ -520,7 +520,7 @@ app.delete('/api/admin/users/:userId', async (req, res) => {
520
  res.json({ success: true });
521
  } catch (error) {
522
  console.error('Error deleting user:', error);
523
- res.status(500).json({ success: false, error: 'Failed to delete user' });
524
  }
525
  });
526
 
 
402
  const { userId } = req.params;
403
  const userEmail = req.headers['x-user-email'];
404
  try {
 
405
  const mainDb = await getDb();
406
+ const db = await getSecondaryDb();
407
  const user = await mainDb.collection('users').findOne({ email: userEmail });
408
  // Allow legacy numeric id or ObjectId string
409
  const userIdStr = userId.toString();
 
427
  return res.status(400).json({ success: false, error: 'Content and sender are required' });
428
  }
429
  try {
 
430
  const mainDb = await getDb();
431
+ const db = await getSecondaryDb();
432
  const user = await mainDb.collection('users').findOne({ email: userEmail });
433
  const userIdStr = userId.toString();
434
  const userIdObj = ObjectId.isValid(userIdStr) ? new ObjectId(userIdStr) : userIdStr;
 
439
  res.json({ success: true, message: { ...result.ops?.[0], user_id: userId, id: userId, _id: result.insertedId } });
440
  } catch (error) {
441
  console.error('Error sending message:', error);
442
+ res.status(500).json({ success: false, error: 'Failed to send message', details: error.message });
443
  }
444
  });
445
 
 
462
  res.json({ success: true, messages });
463
  } catch (error) {
464
  console.error('Error fetching messages:', error);
465
+ res.status(500).json({ success: false, error: 'Failed to fetch messages', details: error.message });
466
  }
467
  });
468
 
 
475
  res.json({ success: true, messages });
476
  } catch (error) {
477
  console.error('Error fetching messages:', error);
478
+ res.status(500).json({ success: false, error: 'Failed to fetch messages', details: error.message });
479
  }
480
  });
481
 
 
489
  res.json({ success: true });
490
  } catch (error) {
491
  console.error('Error sending message:', error);
492
+ res.status(500).json({ success: false, error: 'Failed to send message', details: error.message });
493
  }
494
  });
495
 
 
503
  res.json({ success: true });
504
  } catch (error) {
505
  console.error('Error deleting message:', error);
506
+ res.status(500).json({ success: false, error: 'Failed to delete message', details: error.message });
507
  }
508
  });
509
 
 
520
  res.json({ success: true });
521
  } catch (error) {
522
  console.error('Error deleting user:', error);
523
+ res.status(500).json({ success: false, error: 'Failed to delete user', details: error.message });
524
  }
525
  });
526