Mark-Lasfar commited on
Commit ·
ac4fbf7
1
Parent(s): 683a335
Add GraphQL
Browse files
server.js
CHANGED
|
@@ -18391,9 +18391,10 @@ app.get('/api/users/:userId/contributions', async (req, res) => {
|
|
| 18391 |
|
| 18392 |
|
| 18393 |
// ============================================
|
| 18394 |
-
// GET /api/analytics/also-viewed -
|
| 18395 |
// ============================================
|
| 18396 |
app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
|
|
| 18397 |
try {
|
| 18398 |
const { userId } = req.query;
|
| 18399 |
|
|
@@ -18401,27 +18402,43 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18401 |
return res.status(400).json({ error: 'userId is required' });
|
| 18402 |
}
|
| 18403 |
|
| 18404 |
-
// ✅
|
| 18405 |
-
console.log('🔍
|
| 18406 |
-
console.log('🔍 userId from query:', userId);
|
| 18407 |
|
| 18408 |
const isOwner = req.user && req.user.userId === userId;
|
| 18409 |
-
console.log('🔍 isOwner:', isOwner);
|
| 18410 |
|
|
|
|
| 18411 |
// ✅ 1. جلب المشاهدات من AlsoViewed
|
|
|
|
| 18412 |
const alsoViewed = await AlsoViewed.find({ targetUserId: userId })
|
| 18413 |
.sort({ viewedAt: -1 })
|
| 18414 |
.limit(20);
|
| 18415 |
|
|
|
|
|
|
|
|
|
|
| 18416 |
// ✅ 2. جلب المشاهدات من ProfileAnalytics
|
|
|
|
| 18417 |
const analytics = await ProfileAnalytics.findOne({ userId });
|
| 18418 |
let historicalViews = [];
|
| 18419 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18420 |
if (analytics && analytics.profileViews && analytics.profileViews.length > 0) {
|
| 18421 |
const views = analytics.profileViews;
|
| 18422 |
const viewerMap = new Map();
|
| 18423 |
|
| 18424 |
views.forEach((view, index) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18425 |
let key = null;
|
| 18426 |
if (view.viewerId) {
|
| 18427 |
key = view.viewerId.toString();
|
|
@@ -18436,7 +18453,8 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18436 |
viewerId: view.viewerId || null,
|
| 18437 |
viewerIp: view.viewerIp || null,
|
| 18438 |
viewedAt: view.viewedAt,
|
| 18439 |
-
count: 1
|
|
|
|
| 18440 |
});
|
| 18441 |
}
|
| 18442 |
});
|
|
@@ -18444,17 +18462,25 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18444 |
historicalViews = Array.from(viewerMap.values())
|
| 18445 |
.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt))
|
| 18446 |
.slice(0, 20);
|
|
|
|
|
|
|
| 18447 |
}
|
| 18448 |
|
|
|
|
| 18449 |
// ✅ 3. دمج المشاهدات
|
|
|
|
| 18450 |
const allViewers = [];
|
| 18451 |
const processedIds = new Set();
|
| 18452 |
|
| 18453 |
for (const item of alsoViewed) {
|
| 18454 |
let key = null;
|
| 18455 |
-
if (item.viewerId)
|
| 18456 |
-
|
| 18457 |
-
else
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18458 |
|
| 18459 |
if (!processedIds.has(key)) {
|
| 18460 |
processedIds.add(key);
|
|
@@ -18467,11 +18493,17 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18467 |
}
|
| 18468 |
}
|
| 18469 |
|
|
|
|
|
|
|
| 18470 |
for (const item of historicalViews) {
|
| 18471 |
let key = null;
|
| 18472 |
-
if (item.viewerId)
|
| 18473 |
-
|
| 18474 |
-
else
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18475 |
|
| 18476 |
if (!processedIds.has(key)) {
|
| 18477 |
processedIds.add(key);
|
|
@@ -18484,25 +18516,38 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18484 |
}
|
| 18485 |
}
|
| 18486 |
|
|
|
|
|
|
|
| 18487 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18488 |
|
|
|
|
| 18489 |
// ✅ 4. جلب بيانات المستخدمين
|
| 18490 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18491 |
let userMap = {};
|
| 18492 |
if (userIds.length > 0) {
|
| 18493 |
const users = await User.find({ _id: { $in: userIds } })
|
| 18494 |
.select('username profile.nickname profile.avatar profile.jobTitle');
|
| 18495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18496 |
}
|
| 18497 |
|
| 18498 |
-
//
|
| 18499 |
-
|
| 18500 |
-
|
| 18501 |
-
|
| 18502 |
-
allViewers.forEach(item => {
|
| 18503 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18504 |
const user = userMap[item.viewerId.toString()];
|
| 18505 |
-
|
| 18506 |
_id: user._id,
|
| 18507 |
name: user.profile?.nickname || user.username || 'Someone',
|
| 18508 |
nickname: user.profile?.nickname,
|
|
@@ -18510,10 +18555,13 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18510 |
avatar: user.profile?.avatar || '/assets/img/default-avatar.png',
|
| 18511 |
title: user.profile?.jobTitle || 'Professional',
|
| 18512 |
viewedAt: item.viewedAt,
|
| 18513 |
-
isGuest: false
|
| 18514 |
-
|
| 18515 |
-
|
| 18516 |
-
|
|
|
|
|
|
|
|
|
|
| 18517 |
_id: item.viewerId,
|
| 18518 |
name: 'Deleted User',
|
| 18519 |
nickname: null,
|
|
@@ -18521,20 +18569,55 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18521 |
avatar: '/assets/img/default-avatar.png',
|
| 18522 |
title: 'Unknown',
|
| 18523 |
viewedAt: item.viewedAt,
|
| 18524 |
-
isGuest: false
|
| 18525 |
-
|
| 18526 |
-
|
| 18527 |
-
|
| 18528 |
-
|
| 18529 |
-
|
| 18530 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18531 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18532 |
});
|
| 18533 |
|
| 18534 |
-
console.log('
|
| 18535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18536 |
|
| 18537 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18538 |
function shuffleArray(array) {
|
| 18539 |
const shuffled = [...array];
|
| 18540 |
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
@@ -18544,38 +18627,15 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18544 |
return shuffled;
|
| 18545 |
}
|
| 18546 |
|
| 18547 |
-
const
|
| 18548 |
-
|
| 18549 |
-
// ✅ 7. بناء النتيجة النهائية
|
| 18550 |
-
let data = shuffledRegistered.slice(0, 5);
|
| 18551 |
-
const guestCount = guestViewers.length;
|
| 18552 |
-
|
| 18553 |
-
console.log('✅ guestCount:', guestCount);
|
| 18554 |
-
console.log('✅ isOwner:', isOwner);
|
| 18555 |
|
| 18556 |
-
//
|
| 18557 |
-
|
| 18558 |
-
|
| 18559 |
-
_id: null,
|
| 18560 |
-
name: `+ ${guestCount} others from outside XCV`,
|
| 18561 |
-
nickname: null,
|
| 18562 |
-
username: null,
|
| 18563 |
-
avatar: null,
|
| 18564 |
-
title: 'Guest Visitors',
|
| 18565 |
-
viewedAt: null,
|
| 18566 |
-
isGuest: true,
|
| 18567 |
-
isSummary: true,
|
| 18568 |
-
guestCount: guestCount
|
| 18569 |
-
});
|
| 18570 |
-
}
|
| 18571 |
-
|
| 18572 |
-
console.log('✅ final data length:', data.length);
|
| 18573 |
-
|
| 18574 |
-
// ✅ 8. إرجاع النتيجة
|
| 18575 |
res.json({
|
| 18576 |
-
data:
|
| 18577 |
-
total:
|
| 18578 |
-
registered:
|
| 18579 |
guests: guestCount,
|
| 18580 |
lastUpdated: new Date().toISOString()
|
| 18581 |
});
|
|
|
|
| 18391 |
|
| 18392 |
|
| 18393 |
// ============================================
|
| 18394 |
+
// GET /api/analytics/also-viewed - النسخة النهائية مع سجلات
|
| 18395 |
// ============================================
|
| 18396 |
app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
| 18397 |
+
|
| 18398 |
try {
|
| 18399 |
const { userId } = req.query;
|
| 18400 |
|
|
|
|
| 18402 |
return res.status(400).json({ error: 'userId is required' });
|
| 18403 |
}
|
| 18404 |
|
| 18405 |
+
// ✅ سجلات للتحقق
|
| 18406 |
+
console.log('🔍 also-viewed called for userId:', userId);
|
|
|
|
| 18407 |
|
| 18408 |
const isOwner = req.user && req.user.userId === userId;
|
|
|
|
| 18409 |
|
| 18410 |
+
// ============================
|
| 18411 |
// ✅ 1. جلب المشاهدات من AlsoViewed
|
| 18412 |
+
// ============================
|
| 18413 |
const alsoViewed = await AlsoViewed.find({ targetUserId: userId })
|
| 18414 |
.sort({ viewedAt: -1 })
|
| 18415 |
.limit(20);
|
| 18416 |
|
| 18417 |
+
console.log('👥 AlsoViewed count:', alsoViewed.length);
|
| 18418 |
+
|
| 18419 |
+
// ============================
|
| 18420 |
// ✅ 2. جلب المشاهدات من ProfileAnalytics
|
| 18421 |
+
// ============================
|
| 18422 |
const analytics = await ProfileAnalytics.findOne({ userId });
|
| 18423 |
let historicalViews = [];
|
| 18424 |
|
| 18425 |
+
console.log('📊 Analytics found:', analytics ? 'Yes' : 'No');
|
| 18426 |
+
|
| 18427 |
+
if (analytics) {
|
| 18428 |
+
console.log('📊 profileViews length:', analytics.profileViews?.length || 0);
|
| 18429 |
+
}
|
| 18430 |
+
|
| 18431 |
if (analytics && analytics.profileViews && analytics.profileViews.length > 0) {
|
| 18432 |
const views = analytics.profileViews;
|
| 18433 |
const viewerMap = new Map();
|
| 18434 |
|
| 18435 |
views.forEach((view, index) => {
|
| 18436 |
+
console.log(`📊 View ${index}:`, {
|
| 18437 |
+
viewerId: view.viewerId,
|
| 18438 |
+
viewerIp: view.viewerIp,
|
| 18439 |
+
viewedAt: view.viewedAt
|
| 18440 |
+
});
|
| 18441 |
+
|
| 18442 |
let key = null;
|
| 18443 |
if (view.viewerId) {
|
| 18444 |
key = view.viewerId.toString();
|
|
|
|
| 18453 |
viewerId: view.viewerId || null,
|
| 18454 |
viewerIp: view.viewerIp || null,
|
| 18455 |
viewedAt: view.viewedAt,
|
| 18456 |
+
count: 1,
|
| 18457 |
+
isLegacy: !view.viewerId && !view.viewerIp
|
| 18458 |
});
|
| 18459 |
}
|
| 18460 |
});
|
|
|
|
| 18462 |
historicalViews = Array.from(viewerMap.values())
|
| 18463 |
.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt))
|
| 18464 |
.slice(0, 20);
|
| 18465 |
+
|
| 18466 |
+
console.log('📊 historicalViews count:', historicalViews.length);
|
| 18467 |
}
|
| 18468 |
|
| 18469 |
+
// ============================
|
| 18470 |
// ✅ 3. دمج المشاهدات
|
| 18471 |
+
// ============================
|
| 18472 |
const allViewers = [];
|
| 18473 |
const processedIds = new Set();
|
| 18474 |
|
| 18475 |
for (const item of alsoViewed) {
|
| 18476 |
let key = null;
|
| 18477 |
+
if (item.viewerId) {
|
| 18478 |
+
key = item.viewerId.toString();
|
| 18479 |
+
} else if (item.viewerIp) {
|
| 18480 |
+
key = item.viewerIp;
|
| 18481 |
+
} else {
|
| 18482 |
+
key = `also_${item._id}`;
|
| 18483 |
+
}
|
| 18484 |
|
| 18485 |
if (!processedIds.has(key)) {
|
| 18486 |
processedIds.add(key);
|
|
|
|
| 18493 |
}
|
| 18494 |
}
|
| 18495 |
|
| 18496 |
+
console.log('👥 allViewers from AlsoViewed:', allViewers.length);
|
| 18497 |
+
|
| 18498 |
for (const item of historicalViews) {
|
| 18499 |
let key = null;
|
| 18500 |
+
if (item.viewerId) {
|
| 18501 |
+
key = item.viewerId.toString();
|
| 18502 |
+
} else if (item.viewerIp) {
|
| 18503 |
+
key = item.viewerIp;
|
| 18504 |
+
} else {
|
| 18505 |
+
key = `legacy_${Math.random().toString(36).substring(7)}`;
|
| 18506 |
+
}
|
| 18507 |
|
| 18508 |
if (!processedIds.has(key)) {
|
| 18509 |
processedIds.add(key);
|
|
|
|
| 18516 |
}
|
| 18517 |
}
|
| 18518 |
|
| 18519 |
+
console.log('📊 Total allViewers:', allViewers.length);
|
| 18520 |
+
|
| 18521 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18522 |
|
| 18523 |
+
// ============================
|
| 18524 |
// ✅ 4. جلب بيانات المستخدمين
|
| 18525 |
+
// ============================
|
| 18526 |
+
const userIds = allViewers
|
| 18527 |
+
.filter(v => v.viewerId)
|
| 18528 |
+
.map(v => v.viewerId);
|
| 18529 |
+
|
| 18530 |
+
console.log('👤 UserIds found:', userIds.length);
|
| 18531 |
+
|
| 18532 |
let userMap = {};
|
| 18533 |
if (userIds.length > 0) {
|
| 18534 |
const users = await User.find({ _id: { $in: userIds } })
|
| 18535 |
.select('username profile.nickname profile.avatar profile.jobTitle');
|
| 18536 |
+
|
| 18537 |
+
users.forEach(u => {
|
| 18538 |
+
userMap[u._id.toString()] = u;
|
| 18539 |
+
});
|
| 18540 |
+
|
| 18541 |
+
console.log('👤 Users found in DB:', users.length);
|
| 18542 |
}
|
| 18543 |
|
| 18544 |
+
// ============================
|
| 18545 |
+
// ✅ 5. تنسيق النتيجة
|
| 18546 |
+
// ============================
|
| 18547 |
+
let formattedViewers = allViewers.map(item => {
|
|
|
|
| 18548 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18549 |
const user = userMap[item.viewerId.toString()];
|
| 18550 |
+
return {
|
| 18551 |
_id: user._id,
|
| 18552 |
name: user.profile?.nickname || user.username || 'Someone',
|
| 18553 |
nickname: user.profile?.nickname,
|
|
|
|
| 18555 |
avatar: user.profile?.avatar || '/assets/img/default-avatar.png',
|
| 18556 |
title: user.profile?.jobTitle || 'Professional',
|
| 18557 |
viewedAt: item.viewedAt,
|
| 18558 |
+
isGuest: false,
|
| 18559 |
+
source: item.source
|
| 18560 |
+
};
|
| 18561 |
+
}
|
| 18562 |
+
|
| 18563 |
+
if (item.viewerId) {
|
| 18564 |
+
return {
|
| 18565 |
_id: item.viewerId,
|
| 18566 |
name: 'Deleted User',
|
| 18567 |
nickname: null,
|
|
|
|
| 18569 |
avatar: '/assets/img/default-avatar.png',
|
| 18570 |
title: 'Unknown',
|
| 18571 |
viewedAt: item.viewedAt,
|
| 18572 |
+
isGuest: false,
|
| 18573 |
+
source: item.source
|
| 18574 |
+
};
|
| 18575 |
+
}
|
| 18576 |
+
|
| 18577 |
+
let displayName = 'Guest 🌐';
|
| 18578 |
+
let viewerIp = item.viewerIp || 'Unknown';
|
| 18579 |
+
|
| 18580 |
+
if (!item.viewerId && !item.viewerIp) {
|
| 18581 |
+
displayName = 'Guest (Legacy)';
|
| 18582 |
+
viewerIp = 'legacy';
|
| 18583 |
}
|
| 18584 |
+
|
| 18585 |
+
return {
|
| 18586 |
+
_id: null,
|
| 18587 |
+
name: displayName,
|
| 18588 |
+
nickname: null,
|
| 18589 |
+
username: null,
|
| 18590 |
+
avatar: '/assets/img/default-avatar.png',
|
| 18591 |
+
title: 'Guest Visitor',
|
| 18592 |
+
viewedAt: item.viewedAt,
|
| 18593 |
+
isGuest: true,
|
| 18594 |
+
viewerIp: viewerIp,
|
| 18595 |
+
source: item.source
|
| 18596 |
+
};
|
| 18597 |
});
|
| 18598 |
|
| 18599 |
+
console.log('📊 formattedViewers count:', formattedViewers.length);
|
| 18600 |
+
|
| 18601 |
+
// ============================
|
| 18602 |
+
// ✅ 6. التصفية حسب الصلاحية
|
| 18603 |
+
// ============================
|
| 18604 |
+
if (!isOwner) {
|
| 18605 |
+
formattedViewers = formattedViewers.filter(v => !v.isGuest);
|
| 18606 |
+
console.log('📊 formattedViewers after filter:', formattedViewers.length);
|
| 18607 |
+
}
|
| 18608 |
+
|
| 18609 |
+
// ============================
|
| 18610 |
+
// ✅ 7. حساب الإحصائيات
|
| 18611 |
+
// ============================
|
| 18612 |
+
const totalViewers = formattedViewers.length;
|
| 18613 |
+
const registeredCount = formattedViewers.filter(v => !v.isGuest).length;
|
| 18614 |
+
const guestCount = formattedViewers.filter(v => v.isGuest).length;
|
| 18615 |
|
| 18616 |
+
console.log('📊 Final stats:', { totalViewers, registeredCount, guestCount });
|
| 18617 |
+
|
| 18618 |
+
// ============================
|
| 18619 |
+
// ✅ 8. خلط عشوائي
|
| 18620 |
+
// ============================
|
| 18621 |
function shuffleArray(array) {
|
| 18622 |
const shuffled = [...array];
|
| 18623 |
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
|
|
| 18627 |
return shuffled;
|
| 18628 |
}
|
| 18629 |
|
| 18630 |
+
const shuffledViewers = shuffleArray(formattedViewers);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18631 |
|
| 18632 |
+
// ============================
|
| 18633 |
+
// ✅ 9. إرجاع النتيجة
|
| 18634 |
+
// ============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18635 |
res.json({
|
| 18636 |
+
data: shuffledViewers.slice(0, 5),
|
| 18637 |
+
total: totalViewers,
|
| 18638 |
+
registered: registeredCount,
|
| 18639 |
guests: guestCount,
|
| 18640 |
lastUpdated: new Date().toISOString()
|
| 18641 |
});
|