Mark-Lasfar commited on
Commit ·
67eff21
1
Parent(s): cedb822
Add GraphQL
Browse files
server.js
CHANGED
|
@@ -18401,12 +18401,15 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18401 |
return res.status(400).json({ error: 'userId is required' });
|
| 18402 |
}
|
| 18403 |
|
|
|
|
|
|
|
|
|
|
| 18404 |
// ============================
|
| 18405 |
// ✅ 1. جلب المشاهدات من AlsoViewed (جديدة)
|
| 18406 |
// ============================
|
| 18407 |
const alsoViewed = await AlsoViewed.find({ targetUserId: userId })
|
| 18408 |
.sort({ viewedAt: -1 })
|
| 18409 |
-
.limit(
|
| 18410 |
|
| 18411 |
// ============================
|
| 18412 |
// ✅ 2. جلب المشاهدات من ProfileAnalytics (للمشاهدات القديمة)
|
|
@@ -18415,10 +18418,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18415 |
let historicalViews = [];
|
| 18416 |
|
| 18417 |
if (analytics && analytics.profileViews && analytics.profileViews.length > 0) {
|
| 18418 |
-
// ✅ استخراج المشاهدات من profileViews
|
| 18419 |
const views = analytics.profileViews;
|
| 18420 |
-
|
| 18421 |
-
// ✅ تجميع المشاهدات حسب viewerId أو IP
|
| 18422 |
const viewerMap = new Map();
|
| 18423 |
|
| 18424 |
views.forEach(view => {
|
|
@@ -18431,7 +18431,6 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18431 |
count: 1
|
| 18432 |
});
|
| 18433 |
} else {
|
| 18434 |
-
// ✅ تحديث آخر وقت مشاهدة
|
| 18435 |
const existing = viewerMap.get(key);
|
| 18436 |
if (view.viewedAt > existing.viewedAt) {
|
| 18437 |
existing.viewedAt = view.viewedAt;
|
|
@@ -18440,19 +18439,17 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18440 |
}
|
| 18441 |
});
|
| 18442 |
|
| 18443 |
-
// ✅ تحويل إلى Array وترتيب حسب الأحدث
|
| 18444 |
historicalViews = Array.from(viewerMap.values())
|
| 18445 |
.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt))
|
| 18446 |
-
.slice(0,
|
| 18447 |
}
|
| 18448 |
|
| 18449 |
// ============================
|
| 18450 |
-
// ✅ 3. دمج المشاهدات
|
| 18451 |
// ============================
|
| 18452 |
const allViewers = [];
|
| 18453 |
const processedIds = new Set();
|
| 18454 |
|
| 18455 |
-
// ✅ أضف المشاهدات من AlsoViewed أولاً (الأحدث)
|
| 18456 |
for (const item of alsoViewed) {
|
| 18457 |
const key = item.viewerId ? item.viewerId.toString() : item.viewerIp;
|
| 18458 |
if (!processedIds.has(key)) {
|
|
@@ -18466,7 +18463,6 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18466 |
}
|
| 18467 |
}
|
| 18468 |
|
| 18469 |
-
// ✅ أضف المشاهدات التاريخية (اللي مش موجودة في AlsoViewed)
|
| 18470 |
for (const item of historicalViews) {
|
| 18471 |
const key = item.viewerId ? item.viewerId.toString() : item.viewerIp;
|
| 18472 |
if (!processedIds.has(key)) {
|
|
@@ -18503,7 +18499,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18503 |
// ============================
|
| 18504 |
// ✅ 5. تنسيق النتيجة النهائية
|
| 18505 |
// ============================
|
| 18506 |
-
|
| 18507 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18508 |
const user = userMap[item.viewerId.toString()];
|
| 18509 |
return {
|
|
@@ -18518,10 +18514,10 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18518 |
source: item.source
|
| 18519 |
};
|
| 18520 |
} else {
|
| 18521 |
-
// ✅ زائر
|
| 18522 |
return {
|
| 18523 |
_id: null,
|
| 18524 |
-
name:
|
| 18525 |
nickname: null,
|
| 18526 |
username: null,
|
| 18527 |
avatar: '/assets/img/default-avatar.png',
|
|
@@ -18534,15 +18530,51 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18534 |
}
|
| 18535 |
});
|
| 18536 |
|
| 18537 |
-
//
|
| 18538 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18539 |
|
| 18540 |
} catch (error) {
|
| 18541 |
logger.error(`Error fetching also viewed: ${error.message}`);
|
| 18542 |
res.status(500).json({ error: 'Failed to fetch also viewed data' });
|
| 18543 |
}
|
| 18544 |
});
|
| 18545 |
-
|
| 18546 |
// ============================================
|
| 18547 |
// GET /api/pages/suggestions - جلب اقتراحات الصفحات
|
| 18548 |
// ============================================
|
|
|
|
| 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 (للمشاهدات القديمة)
|
|
|
|
| 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 => {
|
|
|
|
| 18431 |
count: 1
|
| 18432 |
});
|
| 18433 |
} else {
|
|
|
|
| 18434 |
const existing = viewerMap.get(key);
|
| 18435 |
if (view.viewedAt > existing.viewedAt) {
|
| 18436 |
existing.viewedAt = view.viewedAt;
|
|
|
|
| 18439 |
}
|
| 18440 |
});
|
| 18441 |
|
|
|
|
| 18442 |
historicalViews = Array.from(viewerMap.values())
|
| 18443 |
.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt))
|
| 18444 |
+
.slice(0, 20);
|
| 18445 |
}
|
| 18446 |
|
| 18447 |
// ============================
|
| 18448 |
+
// ✅ 3. دمج المشاهدات
|
| 18449 |
// ============================
|
| 18450 |
const allViewers = [];
|
| 18451 |
const processedIds = new Set();
|
| 18452 |
|
|
|
|
| 18453 |
for (const item of alsoViewed) {
|
| 18454 |
const key = item.viewerId ? item.viewerId.toString() : item.viewerIp;
|
| 18455 |
if (!processedIds.has(key)) {
|
|
|
|
| 18463 |
}
|
| 18464 |
}
|
| 18465 |
|
|
|
|
| 18466 |
for (const item of historicalViews) {
|
| 18467 |
const key = item.viewerId ? item.viewerId.toString() : item.viewerIp;
|
| 18468 |
if (!processedIds.has(key)) {
|
|
|
|
| 18499 |
// ============================
|
| 18500 |
// ✅ 5. تنسيق النتيجة النهائية
|
| 18501 |
// ============================
|
| 18502 |
+
let formattedViewers = allViewers.map(item => {
|
| 18503 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18504 |
const user = userMap[item.viewerId.toString()];
|
| 18505 |
return {
|
|
|
|
| 18514 |
source: item.source
|
| 18515 |
};
|
| 18516 |
} else {
|
| 18517 |
+
// ✅ زائر (غير مسجل)
|
| 18518 |
return {
|
| 18519 |
_id: null,
|
| 18520 |
+
name: 'Guest 🌐',
|
| 18521 |
nickname: null,
|
| 18522 |
username: null,
|
| 18523 |
avatar: '/assets/img/default-avatar.png',
|
|
|
|
| 18530 |
}
|
| 18531 |
});
|
| 18532 |
|
| 18533 |
+
// ============================
|
| 18534 |
+
// ✅ 6. التصفية حسب الصلاحية
|
| 18535 |
+
// ============================
|
| 18536 |
+
if (!isOwner) {
|
| 18537 |
+
// ✅ الزائر: يرى فقط المستخدمين المسجلين (يزيل الزوار)
|
| 18538 |
+
formattedViewers = formattedViewers.filter(v => !v.isGuest);
|
| 18539 |
+
}
|
| 18540 |
+
|
| 18541 |
+
// ============================
|
| 18542 |
+
// ✅ 7. حساب الإحصائيات
|
| 18543 |
+
// ============================
|
| 18544 |
+
const totalViewers = formattedViewers.length;
|
| 18545 |
+
const registeredCount = formattedViewers.filter(v => !v.isGuest).length;
|
| 18546 |
+
const guestCount = formattedViewers.filter(v => v.isGuest).length;
|
| 18547 |
+
|
| 18548 |
+
// ============================
|
| 18549 |
+
// ✅ 8. خلط عشوائي (يتغير مع كل ريلود)
|
| 18550 |
+
// ============================
|
| 18551 |
+
function shuffleArray(array) {
|
| 18552 |
+
const shuffled = [...array];
|
| 18553 |
+
for (let i = shuffled.length - 1; i > 0; i--) {
|
| 18554 |
+
const j = Math.floor(Math.random() * (i + 1));
|
| 18555 |
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
| 18556 |
+
}
|
| 18557 |
+
return shuffled;
|
| 18558 |
+
}
|
| 18559 |
+
|
| 18560 |
+
const shuffledViewers = shuffleArray(formattedViewers);
|
| 18561 |
+
|
| 18562 |
+
// ============================
|
| 18563 |
+
// ✅ 9. إرجاع النتيجة
|
| 18564 |
+
// ============================
|
| 18565 |
+
res.json({
|
| 18566 |
+
data: shuffledViewers.slice(0, 5),
|
| 18567 |
+
total: totalViewers,
|
| 18568 |
+
registered: registeredCount,
|
| 18569 |
+
guests: guestCount,
|
| 18570 |
+
lastUpdated: new Date().toISOString()
|
| 18571 |
+
});
|
| 18572 |
|
| 18573 |
} catch (error) {
|
| 18574 |
logger.error(`Error fetching also viewed: ${error.message}`);
|
| 18575 |
res.status(500).json({ error: 'Failed to fetch also viewed data' });
|
| 18576 |
}
|
| 18577 |
});
|
|
|
|
| 18578 |
// ============================================
|
| 18579 |
// GET /api/pages/suggestions - جلب اقتراحات الصفحات
|
| 18580 |
// ============================================
|