Mark-Lasfar commited on
Commit
f20ebe9
·
1 Parent(s): 6835469

Add GraphQL

Browse files
Files changed (1) hide show
  1. server.js +33 -14
server.js CHANGED
@@ -18389,8 +18389,9 @@ app.get('/api/users/:userId/contributions', async (req, res) => {
18389
  }
18390
  });
18391
 
 
18392
  // ============================================
18393
- // GET /api/analytics/also-viewed - النسخة النهائية
18394
  // ============================================
18395
  app.get('/api/analytics/also-viewed', async (req, res) => {
18396
  try {
@@ -18400,6 +18401,9 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18400
  return res.status(400).json({ error: 'userId is required' });
18401
  }
18402
 
 
 
 
18403
  const isOwner = req.user && req.user.userId === userId;
18404
 
18405
  // ============================
@@ -18409,17 +18413,31 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18409
  .sort({ viewedAt: -1 })
18410
  .limit(20);
18411
 
 
 
18412
  // ============================
18413
  // ✅ 2. جلب المشاهدات من ProfileAnalytics
18414
  // ============================
18415
  const analytics = await ProfileAnalytics.findOne({ userId });
18416
  let historicalViews = [];
18417
 
 
 
 
 
 
 
18418
  if (analytics && analytics.profileViews && analytics.profileViews.length > 0) {
18419
  const views = analytics.profileViews;
18420
  const viewerMap = new Map();
18421
 
18422
  views.forEach((view, index) => {
 
 
 
 
 
 
18423
  let key = null;
18424
  if (view.viewerId) {
18425
  key = view.viewerId.toString();
@@ -18437,18 +18455,14 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18437
  count: 1,
18438
  isLegacy: !view.viewerId && !view.viewerIp
18439
  });
18440
- } else {
18441
- const existing = viewerMap.get(key);
18442
- if (view.viewedAt > existing.viewedAt) {
18443
- existing.viewedAt = view.viewedAt;
18444
- }
18445
- existing.count++;
18446
  }
18447
  });
18448
 
18449
  historicalViews = Array.from(viewerMap.values())
18450
  .sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt))
18451
  .slice(0, 20);
 
 
18452
  }
18453
 
18454
  // ============================
@@ -18478,6 +18492,8 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18478
  }
18479
  }
18480
 
 
 
18481
  for (const item of historicalViews) {
18482
  let key = null;
18483
  if (item.viewerId) {
@@ -18499,6 +18515,8 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18499
  }
18500
  }
18501
 
 
 
18502
  allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
18503
 
18504
  // ============================
@@ -18508,6 +18526,8 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18508
  .filter(v => v.viewerId)
18509
  .map(v => v.viewerId);
18510
 
 
 
18511
  let userMap = {};
18512
  if (userIds.length > 0) {
18513
  const users = await User.find({ _id: { $in: userIds } })
@@ -18516,6 +18536,8 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18516
  users.forEach(u => {
18517
  userMap[u._id.toString()] = u;
18518
  });
 
 
18519
  }
18520
 
18521
  // ============================
@@ -18573,14 +18595,9 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18573
  };
18574
  });
18575
 
18576
- // ============================
18577
- // ✅ 6. التصفية حسب الصلاحية
18578
- // ============================
18579
- if (!isOwner) {
18580
- // ✅ الزائر: يرى فقط المستخدمين المسجلين
18581
- formattedViewers = formattedViewers.filter(v => !v.isGuest);
18582
- }
18583
 
 
18584
  // ============================
18585
  // ✅ 7. حساب الإحصائيات
18586
  // ============================
@@ -18588,6 +18605,8 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
18588
  const registeredCount = formattedViewers.filter(v => !v.isGuest).length;
18589
  const guestCount = formattedViewers.filter(v => v.isGuest).length;
18590
 
 
 
18591
  // ============================
18592
  // ✅ 8. خلط عشوائي
18593
  // ============================
 
18389
  }
18390
  });
18391
 
18392
+
18393
  // ============================================
18394
+ // GET /api/analytics/also-viewed - النسخة النهائية مع سجلات
18395
  // ============================================
18396
  app.get('/api/analytics/also-viewed', async (req, res) => {
18397
  try {
 
18401
  return res.status(400).json({ error: 'userId is required' });
18402
  }
18403
 
18404
+ // ✅ سجلات للتحقق
18405
+ console.log('🔍 also-viewed called for userId:', userId);
18406
+
18407
  const isOwner = req.user && req.user.userId === userId;
18408
 
18409
  // ============================
 
18413
  .sort({ viewedAt: -1 })
18414
  .limit(20);
18415
 
18416
+ console.log('👥 AlsoViewed count:', alsoViewed.length);
18417
+
18418
  // ============================
18419
  // ✅ 2. جلب المشاهدات من ProfileAnalytics
18420
  // ============================
18421
  const analytics = await ProfileAnalytics.findOne({ userId });
18422
  let historicalViews = [];
18423
 
18424
+ console.log('📊 Analytics found:', analytics ? 'Yes' : 'No');
18425
+
18426
+ if (analytics) {
18427
+ console.log('📊 profileViews length:', analytics.profileViews?.length || 0);
18428
+ }
18429
+
18430
  if (analytics && analytics.profileViews && analytics.profileViews.length > 0) {
18431
  const views = analytics.profileViews;
18432
  const viewerMap = new Map();
18433
 
18434
  views.forEach((view, index) => {
18435
+ console.log(`📊 View ${index}:`, {
18436
+ viewerId: view.viewerId,
18437
+ viewerIp: view.viewerIp,
18438
+ viewedAt: view.viewedAt
18439
+ });
18440
+
18441
  let key = null;
18442
  if (view.viewerId) {
18443
  key = view.viewerId.toString();
 
18455
  count: 1,
18456
  isLegacy: !view.viewerId && !view.viewerIp
18457
  });
 
 
 
 
 
 
18458
  }
18459
  });
18460
 
18461
  historicalViews = Array.from(viewerMap.values())
18462
  .sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt))
18463
  .slice(0, 20);
18464
+
18465
+ console.log('📊 historicalViews count:', historicalViews.length);
18466
  }
18467
 
18468
  // ============================
 
18492
  }
18493
  }
18494
 
18495
+ console.log('👥 allViewers from AlsoViewed:', allViewers.length);
18496
+
18497
  for (const item of historicalViews) {
18498
  let key = null;
18499
  if (item.viewerId) {
 
18515
  }
18516
  }
18517
 
18518
+ console.log('📊 Total allViewers:', allViewers.length);
18519
+
18520
  allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
18521
 
18522
  // ============================
 
18526
  .filter(v => v.viewerId)
18527
  .map(v => v.viewerId);
18528
 
18529
+ console.log('👤 UserIds found:', userIds.length);
18530
+
18531
  let userMap = {};
18532
  if (userIds.length > 0) {
18533
  const users = await User.find({ _id: { $in: userIds } })
 
18536
  users.forEach(u => {
18537
  userMap[u._id.toString()] = u;
18538
  });
18539
+
18540
+ console.log('👤 Users found in DB:', users.length);
18541
  }
18542
 
18543
  // ============================
 
18595
  };
18596
  });
18597
 
18598
+ console.log('📊 formattedViewers count:', formattedViewers.length);
 
 
 
 
 
 
18599
 
18600
+
18601
  // ============================
18602
  // ✅ 7. حساب الإحصائيات
18603
  // ============================
 
18605
  const registeredCount = formattedViewers.filter(v => !v.isGuest).length;
18606
  const guestCount = formattedViewers.filter(v => v.isGuest).length;
18607
 
18608
+ console.log('📊 Final stats:', { totalViewers, registeredCount, guestCount });
18609
+
18610
  // ============================
18611
  // ✅ 8. خلط عشوائي
18612
  // ============================