veltrixcode commited on
Commit
8531197
·
verified ·
1 Parent(s): c58b1de

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +9 -6
server.js CHANGED
@@ -403,11 +403,13 @@ app.get('/api/messages/:userId', checkOtpAuth, async (req, res) => {
403
  try {
404
  const db = await getDb();
405
  const user = await db.collection('users').findOne({ email: userEmail });
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);
@@ -425,12 +427,13 @@ app.post('/api/messages', checkOtpAuth, async (req, res) => {
425
  try {
426
  const db = await getDb();
427
  const user = await db.collection('users').findOne({ email: userEmail });
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);
436
  res.status(500).json({ success: false, error: 'Failed to send message' });
 
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;
409
+ if (!user || (user._id.toString() !== userIdStr && user.id?.toString() !== userIdStr)) {
410
  return res.status(403).json({ success: false, error: 'Unauthorized access' });
411
  }
412
+ const messages = await db.collection('messages').find({ $or: [ { user_id: userIdObj }, { user_id: user.id } ] }).sort({ created_at: 1 }).toArray();
 
413
  res.json({ success: true, messages });
414
  } catch (error) {
415
  console.error('Error fetching messages:', error);
 
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)) {
433
  return res.status(403).json({ success: false, error: 'Unauthorized access' });
434
  }
 
435
  const result = await db.collection('messages').insertOne({ user_id: userIdObj, content, sender, created_at: new Date() });
436
+ res.json({ success: true, message: { ...result.ops?.[0], user_id: userId, id: userId, _id: result.insertedId } });
437
  } catch (error) {
438
  console.error('Error sending message:', error);
439
  res.status(500).json({ success: false, error: 'Failed to send message' });