Mark-Lasfar commited on
Commit ·
10fd934
1
Parent(s): f20ebe9
Add GraphQL
Browse files
server.js
CHANGED
|
@@ -18391,7 +18391,7 @@ 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', async (req, res) => {
|
| 18397 |
try {
|
|
@@ -18401,49 +18401,36 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 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 |
// ============================
|
| 18410 |
-
// ✅ 1. جلب المشاهدات من AlsoViewed
|
| 18411 |
// ============================
|
| 18412 |
const alsoViewed = await AlsoViewed.find({ targetUserId: userId })
|
| 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 |
-
|
| 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();
|
| 18444 |
} else if (view.viewerIp) {
|
| 18445 |
key = view.viewerIp;
|
| 18446 |
} else {
|
|
|
|
| 18447 |
key = `legacy_${index}_${Date.now()}`;
|
| 18448 |
}
|
| 18449 |
|
|
@@ -18455,14 +18442,18 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 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 |
// ============================
|
|
@@ -18471,6 +18462,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18471 |
const allViewers = [];
|
| 18472 |
const processedIds = new Set();
|
| 18473 |
|
|
|
|
| 18474 |
for (const item of alsoViewed) {
|
| 18475 |
let key = null;
|
| 18476 |
if (item.viewerId) {
|
|
@@ -18492,8 +18484,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18492 |
}
|
| 18493 |
}
|
| 18494 |
|
| 18495 |
-
|
| 18496 |
-
|
| 18497 |
for (const item of historicalViews) {
|
| 18498 |
let key = null;
|
| 18499 |
if (item.viewerId) {
|
|
@@ -18515,19 +18506,16 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18515 |
}
|
| 18516 |
}
|
| 18517 |
|
| 18518 |
-
|
| 18519 |
-
|
| 18520 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18521 |
|
| 18522 |
// ============================
|
| 18523 |
-
// ✅ 4. جلب بيانات المستخدمين
|
| 18524 |
// ============================
|
| 18525 |
const userIds = allViewers
|
| 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,14 +18524,13 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18536 |
users.forEach(u => {
|
| 18537 |
userMap[u._id.toString()] = u;
|
| 18538 |
});
|
| 18539 |
-
|
| 18540 |
-
console.log('👤 Users found in DB:', users.length);
|
| 18541 |
}
|
| 18542 |
|
| 18543 |
// ============================
|
| 18544 |
-
// ✅ 5. تنسيق النتيجة
|
| 18545 |
// ============================
|
| 18546 |
let formattedViewers = allViewers.map(item => {
|
|
|
|
| 18547 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18548 |
const user = userMap[item.viewerId.toString()];
|
| 18549 |
return {
|
|
@@ -18559,6 +18546,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18559 |
};
|
| 18560 |
}
|
| 18561 |
|
|
|
|
| 18562 |
if (item.viewerId) {
|
| 18563 |
return {
|
| 18564 |
_id: item.viewerId,
|
|
@@ -18573,6 +18561,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18573 |
};
|
| 18574 |
}
|
| 18575 |
|
|
|
|
| 18576 |
let displayName = 'Guest 🌐';
|
| 18577 |
let viewerIp = item.viewerIp || 'Unknown';
|
| 18578 |
|
|
@@ -18595,9 +18584,15 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18595 |
};
|
| 18596 |
});
|
| 18597 |
|
| 18598 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18599 |
|
| 18600 |
-
|
| 18601 |
// ============================
|
| 18602 |
// ✅ 7. حساب الإحصائيات
|
| 18603 |
// ============================
|
|
@@ -18605,10 +18600,8 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 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 |
// ============================
|
| 18613 |
function shuffleArray(array) {
|
| 18614 |
const shuffled = [...array];
|
|
|
|
| 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 |
const isOwner = req.user && req.user.userId === userId;
|
| 18406 |
|
| 18407 |
// ============================
|
| 18408 |
+
// ✅ 1. جلب المشاهدات من AlsoViewed (جديدة)
|
| 18409 |
// ============================
|
| 18410 |
const alsoViewed = await AlsoViewed.find({ targetUserId: userId })
|
| 18411 |
.sort({ viewedAt: -1 })
|
| 18412 |
.limit(20);
|
| 18413 |
|
|
|
|
|
|
|
| 18414 |
// ============================
|
| 18415 |
+
// ✅ 2. جلب المشاهدات من ProfileAnalytics (للمشاهدات القديمة)
|
| 18416 |
// ============================
|
| 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 |
+
// ✅ مفتاح فريد لكل مشاهدة
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18426 |
let key = null;
|
| 18427 |
+
|
| 18428 |
if (view.viewerId) {
|
| 18429 |
key = view.viewerId.toString();
|
| 18430 |
} else if (view.viewerIp) {
|
| 18431 |
key = view.viewerIp;
|
| 18432 |
} else {
|
| 18433 |
+
// ✅ للمشاهدات القديمة جداً (بدون viewerId ولا viewerIp)
|
| 18434 |
key = `legacy_${index}_${Date.now()}`;
|
| 18435 |
}
|
| 18436 |
|
|
|
|
| 18442 |
count: 1,
|
| 18443 |
isLegacy: !view.viewerId && !view.viewerIp
|
| 18444 |
});
|
| 18445 |
+
} else {
|
| 18446 |
+
const existing = viewerMap.get(key);
|
| 18447 |
+
if (view.viewedAt > existing.viewedAt) {
|
| 18448 |
+
existing.viewedAt = view.viewedAt;
|
| 18449 |
+
}
|
| 18450 |
+
existing.count++;
|
| 18451 |
}
|
| 18452 |
});
|
| 18453 |
|
| 18454 |
historicalViews = Array.from(viewerMap.values())
|
| 18455 |
.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt))
|
| 18456 |
.slice(0, 20);
|
|
|
|
|
|
|
| 18457 |
}
|
| 18458 |
|
| 18459 |
// ============================
|
|
|
|
| 18462 |
const allViewers = [];
|
| 18463 |
const processedIds = new Set();
|
| 18464 |
|
| 18465 |
+
// ✅ أضف المشاهدات من AlsoViewed أولاً
|
| 18466 |
for (const item of alsoViewed) {
|
| 18467 |
let key = null;
|
| 18468 |
if (item.viewerId) {
|
|
|
|
| 18484 |
}
|
| 18485 |
}
|
| 18486 |
|
| 18487 |
+
// ✅ أضف المشاهدات التاريخية
|
|
|
|
| 18488 |
for (const item of historicalViews) {
|
| 18489 |
let key = null;
|
| 18490 |
if (item.viewerId) {
|
|
|
|
| 18506 |
}
|
| 18507 |
}
|
| 18508 |
|
| 18509 |
+
// ✅ ترتيب حسب الأحدث
|
|
|
|
| 18510 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18511 |
|
| 18512 |
// ============================
|
| 18513 |
+
// ✅ 4. جلب بيانات المستخدمين (للمشاهدات التي لها viewerId)
|
| 18514 |
// ============================
|
| 18515 |
const userIds = allViewers
|
| 18516 |
.filter(v => v.viewerId)
|
| 18517 |
.map(v => v.viewerId);
|
| 18518 |
|
|
|
|
|
|
|
| 18519 |
let userMap = {};
|
| 18520 |
if (userIds.length > 0) {
|
| 18521 |
const users = await User.find({ _id: { $in: userIds } })
|
|
|
|
| 18524 |
users.forEach(u => {
|
| 18525 |
userMap[u._id.toString()] = u;
|
| 18526 |
});
|
|
|
|
|
|
|
| 18527 |
}
|
| 18528 |
|
| 18529 |
// ============================
|
| 18530 |
+
// ✅ 5. تنسيق النتيجة النهائية
|
| 18531 |
// ============================
|
| 18532 |
let formattedViewers = allViewers.map(item => {
|
| 18533 |
+
// ✅ إذا كان viewerId موجود وتم جلب بياناته
|
| 18534 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18535 |
const user = userMap[item.viewerId.toString()];
|
| 18536 |
return {
|
|
|
|
| 18546 |
};
|
| 18547 |
}
|
| 18548 |
|
| 18549 |
+
// ✅ إذا كان viewerId موجود لكن لم يتم جلب بياناته (مستخدم محذوف)
|
| 18550 |
if (item.viewerId) {
|
| 18551 |
return {
|
| 18552 |
_id: item.viewerId,
|
|
|
|
| 18561 |
};
|
| 18562 |
}
|
| 18563 |
|
| 18564 |
+
// ✅ زائر (غير مسجل)
|
| 18565 |
let displayName = 'Guest 🌐';
|
| 18566 |
let viewerIp = item.viewerIp || 'Unknown';
|
| 18567 |
|
|
|
|
| 18584 |
};
|
| 18585 |
});
|
| 18586 |
|
| 18587 |
+
// ============================
|
| 18588 |
+
// ✅ 6. التصفية حسب الصلاحية
|
| 18589 |
+
// ============================
|
| 18590 |
+
// 🔥 للمالك: يرى كل المشاهدات (مسجلين + زوار)
|
| 18591 |
+
// 🔥 للزائر: يرى فقط المستخدمين المسجلين (يزيل الزوار)
|
| 18592 |
+
if (!isOwner) {
|
| 18593 |
+
formattedViewers = formattedViewers.filter(v => !v.isGuest);
|
| 18594 |
+
}
|
| 18595 |
|
|
|
|
| 18596 |
// ============================
|
| 18597 |
// ✅ 7. حساب الإحصائيات
|
| 18598 |
// ============================
|
|
|
|
| 18600 |
const registeredCount = formattedViewers.filter(v => !v.isGuest).length;
|
| 18601 |
const guestCount = formattedViewers.filter(v => v.isGuest).length;
|
| 18602 |
|
|
|
|
|
|
|
| 18603 |
// ============================
|
| 18604 |
+
// ✅ 8. خلط عشوائي (يتغير مع كل ريلود)
|
| 18605 |
// ============================
|
| 18606 |
function shuffleArray(array) {
|
| 18607 |
const shuffled = [...array];
|