Mark-Lasfar commited on
Commit ·
d9746eb
1
Parent(s): 05b3727
Add GraphQL
Browse files- server.js +23 -110
- services/contribution.service.js +280 -0
server.js
CHANGED
|
@@ -100,6 +100,7 @@ const aiService = require('./services/aiService');
|
|
| 100 |
|
| 101 |
const StoreShortcode = require('./models/StoreShortcode');
|
| 102 |
const TemplateStore = require('./models/TemplateStore');
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
// ❌ احذف هذا السطر لأنه مكرر (logger عرفته فوق)
|
|
@@ -614,6 +615,7 @@ const excludedPaths = [
|
|
| 614 |
'/api/conversations/:conversationId/read',
|
| 615 |
'/api/conversations/unread/count',
|
| 616 |
'/api/users/:userId/contributions',
|
|
|
|
| 617 |
'/api/analytics/also-viewed',
|
| 618 |
'/api/pages/suggestions',
|
| 619 |
'/api/pages/:pageId/follow',
|
|
@@ -18402,118 +18404,11 @@ app.get('/api/users/:userId/contributions', async (req, res) => {
|
|
| 18402 |
const { userId } = req.params;
|
| 18403 |
const { year = new Date().getFullYear() } = req.query;
|
| 18404 |
|
| 18405 |
-
const
|
| 18406 |
-
const endDate = new Date(year, 11, 31, 23, 59, 59);
|
| 18407 |
-
|
| 18408 |
-
// ✅ جلب كل أنواع النشاطات
|
| 18409 |
-
const [posts, comments, likes, shares, jobApplications] = await Promise.all([
|
| 18410 |
-
Post.countDocuments({ userId, createdAt: { $gte: startDate, $lte: endDate } }),
|
| 18411 |
-
Comment.countDocuments({ userId, timestamp: { $gte: startDate, $lte: endDate } }),
|
| 18412 |
-
Post.countDocuments({ 'likes.userId': userId, 'likes.likedAt': { $gte: startDate, $lte: endDate } }),
|
| 18413 |
-
Post.countDocuments({ 'shares.userId': userId, 'shares.sharedAt': { $gte: startDate, $lte: endDate } }),
|
| 18414 |
-
JobApplication.countDocuments({ applicantId: userId, appliedAt: { $gte: startDate, $lte: endDate } })
|
| 18415 |
-
]);
|
| 18416 |
-
|
| 18417 |
-
const total = posts + comments + likes + shares + jobApplications;
|
| 18418 |
-
|
| 18419 |
-
// ✅ تجميع النشاطات حسب اليوم (مع كل الأنواع)
|
| 18420 |
-
const dailyActivity = new Map();
|
| 18421 |
-
|
| 18422 |
-
// 1. تجميع المنشورات
|
| 18423 |
-
const postsByDay = await Post.aggregate([
|
| 18424 |
-
{ $match: { userId: new mongoose.Types.ObjectId(userId), createdAt: { $gte: startDate, $lte: endDate } } },
|
| 18425 |
-
{ $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } }, count: { $sum: 1 }, activities: { $push: { type: "post", id: "$_id" } } } }
|
| 18426 |
-
]);
|
| 18427 |
-
|
| 18428 |
-
// 2. تجميع التعليقات
|
| 18429 |
-
const commentsByDay = await Comment.aggregate([
|
| 18430 |
-
{ $match: { userId: new mongoose.Types.ObjectId(userId), timestamp: { $gte: startDate, $lte: endDate } } },
|
| 18431 |
-
{ $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$timestamp" } }, count: { $sum: 1 }, activities: { $push: { type: "comment", id: "$_id", projectId: "$projectId" } } } }
|
| 18432 |
-
]);
|
| 18433 |
-
|
| 18434 |
-
// 3. تجميع الإعجابات
|
| 18435 |
-
const likesByDay = await Post.aggregate([
|
| 18436 |
-
{ $match: { 'likes.userId': new mongoose.Types.ObjectId(userId), 'likes.likedAt': { $gte: startDate, $lte: endDate } } },
|
| 18437 |
-
{ $unwind: "$likes" },
|
| 18438 |
-
{ $match: { 'likes.userId': new mongoose.Types.ObjectId(userId) } },
|
| 18439 |
-
{ $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$likes.likedAt" } }, count: { $sum: 1 }, activities: { $push: { type: "like", postId: "$_id" } } } }
|
| 18440 |
-
]);
|
| 18441 |
-
|
| 18442 |
-
// 4. تجميع المشاركات
|
| 18443 |
-
const sharesByDay = await Post.aggregate([
|
| 18444 |
-
{ $match: { 'shares.userId': new mongoose.Types.ObjectId(userId), 'shares.sharedAt': { $gte: startDate, $lte: endDate } } },
|
| 18445 |
-
{ $unwind: "$shares" },
|
| 18446 |
-
{ $match: { 'shares.userId': new mongoose.Types.ObjectId(userId) } },
|
| 18447 |
-
{ $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$shares.sharedAt" } }, count: { $sum: 1 }, activities: { $push: { type: "share", postId: "$_id" } } } }
|
| 18448 |
-
]);
|
| 18449 |
-
|
| 18450 |
-
// 5. تجميع طلبات التوظيف
|
| 18451 |
-
const applicationsByDay = await JobApplication.aggregate([
|
| 18452 |
-
{ $match: { applicantId: new mongoose.Types.ObjectId(userId), appliedAt: { $gte: startDate, $lte: endDate } } },
|
| 18453 |
-
{ $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$appliedAt" } }, count: { $sum: 1 }, activities: { $push: { type: "job_application", id: "$_id", jobId: "$jobId" } } } }
|
| 18454 |
-
]);
|
| 18455 |
-
|
| 18456 |
-
// تهيئة كل الأيام
|
| 18457 |
-
const allDays = [];
|
| 18458 |
-
for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
|
| 18459 |
-
const dateStr = d.toISOString().split('T')[0];
|
| 18460 |
-
allDays.push(dateStr);
|
| 18461 |
-
dailyActivity.set(dateStr, { date: dateStr, count: 0, activities: [] });
|
| 18462 |
-
}
|
| 18463 |
-
|
| 18464 |
-
// دمج البيانات مع تخزين الـ activities
|
| 18465 |
-
const mergeActivities = (dayData, sourceActivities) => {
|
| 18466 |
-
if (dailyActivity.has(dayData._id)) {
|
| 18467 |
-
const existing = dailyActivity.get(dayData._id);
|
| 18468 |
-
existing.count += dayData.count;
|
| 18469 |
-
existing.activities.push(...(sourceActivities || []));
|
| 18470 |
-
}
|
| 18471 |
-
};
|
| 18472 |
-
|
| 18473 |
-
postsByDay.forEach(day => mergeActivities(day, day.activities));
|
| 18474 |
-
commentsByDay.forEach(day => mergeActivities(day, day.activities));
|
| 18475 |
-
likesByDay.forEach(day => mergeActivities(day, day.activities));
|
| 18476 |
-
sharesByDay.forEach(day => mergeActivities(day, day.activities));
|
| 18477 |
-
applicationsByDay.forEach(day => mergeActivities(day, day.activities));
|
| 18478 |
-
|
| 18479 |
-
// بناء الأسابيع
|
| 18480 |
-
const weeks = [];
|
| 18481 |
-
let currentWeek = [];
|
| 18482 |
-
|
| 18483 |
-
const firstDay = new Date(year, 0, 1);
|
| 18484 |
-
const lastDay = new Date(year, 11, 31);
|
| 18485 |
-
|
| 18486 |
-
for (let d = new Date(firstDay); d <= lastDay; d.setDate(d.getDate() + 1)) {
|
| 18487 |
-
const dateStr = d.toISOString().split('T')[0];
|
| 18488 |
-
const activity = dailyActivity.get(dateStr) || { date: dateStr, count: 0, activities: [] };
|
| 18489 |
-
const count = activity.count;
|
| 18490 |
-
|
| 18491 |
-
let level = 0;
|
| 18492 |
-
if (count > 0 && count <= 2) level = 1;
|
| 18493 |
-
else if (count > 2 && count <= 5) level = 2;
|
| 18494 |
-
else if (count > 5 && count <= 10) level = 3;
|
| 18495 |
-
else if (count > 10) level = 4;
|
| 18496 |
-
|
| 18497 |
-
currentWeek.push({
|
| 18498 |
-
date: dateStr,
|
| 18499 |
-
count: count,
|
| 18500 |
-
level: level,
|
| 18501 |
-
activities: activity.activities || [] // ✅ خزن الأنشطة لكل يوم
|
| 18502 |
-
});
|
| 18503 |
-
|
| 18504 |
-
if (d.getDay() === 6 || d >= lastDay) {
|
| 18505 |
-
weeks.push(currentWeek);
|
| 18506 |
-
currentWeek = [];
|
| 18507 |
-
}
|
| 18508 |
-
}
|
| 18509 |
|
| 18510 |
res.json({
|
| 18511 |
success: true,
|
| 18512 |
-
|
| 18513 |
-
weeks,
|
| 18514 |
-
year,
|
| 18515 |
-
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
|
| 18516 |
-
weekDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
|
| 18517 |
});
|
| 18518 |
|
| 18519 |
} catch (error) {
|
|
@@ -18522,6 +18417,23 @@ app.get('/api/users/:userId/contributions', async (req, res) => {
|
|
| 18522 |
}
|
| 18523 |
});
|
| 18524 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18525 |
|
| 18526 |
// ============================================
|
| 18527 |
// GET /api/analytics/also-viewed - جلب المستخدمين الذين شاهدوا هذا الملف أيضاً
|
|
@@ -34975,8 +34887,9 @@ function generateStaticSitemap() {
|
|
| 34975 |
{ loc: '/explore.html', changefreq: 'daily', priority: 0.9 },
|
| 34976 |
{ loc: '/profile.html', changefreq: 'daily', priority: 0.9 },
|
| 34977 |
{ loc: '/shop.html', changefreq: 'daily', priority: 0.9 },
|
| 34978 |
-
{ loc: '/network.html', changefreq: 'daily', priority: 0.8 },
|
| 34979 |
{ loc: '/jobs.html', changefreq: 'daily', priority: 0.9 },
|
|
|
|
|
|
|
| 34980 |
{ loc: '/messages.html', changefreq: 'daily', priority: 0.7 },
|
| 34981 |
{ loc: '/product.html', changefreq: 'daily', priority: 0.9 },
|
| 34982 |
{ loc: '/cv.html', changefreq: 'weekly', priority: 0.9 },
|
|
|
|
| 100 |
|
| 101 |
const StoreShortcode = require('./models/StoreShortcode');
|
| 102 |
const TemplateStore = require('./models/TemplateStore');
|
| 103 |
+
const { buildContributionGraph, getDayActivities } = require('./services/contribution.service');
|
| 104 |
|
| 105 |
|
| 106 |
// ❌ احذف هذا السطر لأنه مكرر (logger عرفته فوق)
|
|
|
|
| 615 |
'/api/conversations/:conversationId/read',
|
| 616 |
'/api/conversations/unread/count',
|
| 617 |
'/api/users/:userId/contributions',
|
| 618 |
+
'/api/users/:userId/contributions/:date',
|
| 619 |
'/api/analytics/also-viewed',
|
| 620 |
'/api/pages/suggestions',
|
| 621 |
'/api/pages/:pageId/follow',
|
|
|
|
| 18404 |
const { userId } = req.params;
|
| 18405 |
const { year = new Date().getFullYear() } = req.query;
|
| 18406 |
|
| 18407 |
+
const data = await buildContributionGraph(userId, year);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18408 |
|
| 18409 |
res.json({
|
| 18410 |
success: true,
|
| 18411 |
+
...data
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18412 |
});
|
| 18413 |
|
| 18414 |
} catch (error) {
|
|
|
|
| 18417 |
}
|
| 18418 |
});
|
| 18419 |
|
| 18420 |
+
// ✅ Endpoint جديد للتفاصيل (عند الضغط على يوم)
|
| 18421 |
+
app.get('/api/users/:userId/contributions/:date', async (req, res) => {
|
| 18422 |
+
try {
|
| 18423 |
+
const { userId, date } = req.params;
|
| 18424 |
+
|
| 18425 |
+
const activities = await getDayActivities(userId, date);
|
| 18426 |
+
|
| 18427 |
+
res.json({
|
| 18428 |
+
date,
|
| 18429 |
+
activities
|
| 18430 |
+
});
|
| 18431 |
+
|
| 18432 |
+
} catch (error) {
|
| 18433 |
+
console.error('Error getting day activities:', error);
|
| 18434 |
+
res.status(500).json({ error: 'Failed to get activities' });
|
| 18435 |
+
}
|
| 18436 |
+
});
|
| 18437 |
|
| 18438 |
// ============================================
|
| 18439 |
// GET /api/analytics/also-viewed - جلب المستخدمين الذين شاهدوا هذا الملف أيضاً
|
|
|
|
| 34887 |
{ loc: '/explore.html', changefreq: 'daily', priority: 0.9 },
|
| 34888 |
{ loc: '/profile.html', changefreq: 'daily', priority: 0.9 },
|
| 34889 |
{ loc: '/shop.html', changefreq: 'daily', priority: 0.9 },
|
|
|
|
| 34890 |
{ loc: '/jobs.html', changefreq: 'daily', priority: 0.9 },
|
| 34891 |
+
{ loc: '/benchmark.html', changefreq: 'daily', priority: 0.9 },
|
| 34892 |
+
{ loc: '/network.html', changefreq: 'daily', priority: 0.8 },
|
| 34893 |
{ loc: '/messages.html', changefreq: 'daily', priority: 0.7 },
|
| 34894 |
{ loc: '/product.html', changefreq: 'daily', priority: 0.9 },
|
| 34895 |
{ loc: '/cv.html', changefreq: 'weekly', priority: 0.9 },
|
services/contribution.service.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// services/contribution.service.js
|
| 2 |
+
const mongoose = require('mongoose');
|
| 3 |
+
const { Post, JobApplication, Comment } = require("../server");
|
| 4 |
+
|
| 5 |
+
// ============================================
|
| 6 |
+
// ✅ Service موحدة لبناء Contribution Graph
|
| 7 |
+
// ============================================
|
| 8 |
+
async function buildContributionGraph(userId, year) {
|
| 9 |
+
if (!userId) {
|
| 10 |
+
return { total: 0, weeks: [], year: year || new Date().getFullYear(), months: [], weekDays: [] };
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
const targetYear = year || new Date().getFullYear();
|
| 14 |
+
const startDate = new Date(targetYear, 0, 1);
|
| 15 |
+
const endDate = new Date(targetYear, 11, 31, 23, 59, 59);
|
| 16 |
+
|
| 17 |
+
try {
|
| 18 |
+
// ✅ جلب البيانات المجمعة (بدون countDocuments منفصلة)
|
| 19 |
+
const [posts, comments, likes, shares, applications] = await Promise.all([
|
| 20 |
+
getPostsAggregation(userId, startDate, endDate),
|
| 21 |
+
getCommentsAggregation(userId, startDate, endDate),
|
| 22 |
+
getLikesAggregation(userId, startDate, endDate),
|
| 23 |
+
getSharesAggregation(userId, startDate, endDate),
|
| 24 |
+
getApplicationsAggregation(userId, startDate, endDate)
|
| 25 |
+
]);
|
| 26 |
+
|
| 27 |
+
// ✅ دمج البيانات في Map
|
| 28 |
+
const daily = new Map();
|
| 29 |
+
|
| 30 |
+
function merge(data) {
|
| 31 |
+
data.forEach(item => {
|
| 32 |
+
const date = item._id;
|
| 33 |
+
if (!daily.has(date)) daily.set(date, 0);
|
| 34 |
+
daily.set(date, daily.get(date) + item.count);
|
| 35 |
+
});
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
merge(posts);
|
| 39 |
+
merge(comments);
|
| 40 |
+
merge(likes);
|
| 41 |
+
merge(shares);
|
| 42 |
+
merge(applications);
|
| 43 |
+
|
| 44 |
+
let total = 0;
|
| 45 |
+
const weeks = [];
|
| 46 |
+
let week = [];
|
| 47 |
+
|
| 48 |
+
for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
|
| 49 |
+
const date = d.toISOString().split('T')[0];
|
| 50 |
+
const count = daily.get(date) || 0;
|
| 51 |
+
total += count;
|
| 52 |
+
|
| 53 |
+
let level = 0;
|
| 54 |
+
if (count > 0 && count <= 2) level = 1;
|
| 55 |
+
else if (count <= 5) level = 2;
|
| 56 |
+
else if (count <= 10) level = 3;
|
| 57 |
+
else if (count > 10) level = 4;
|
| 58 |
+
|
| 59 |
+
// ✅ من غير activities
|
| 60 |
+
week.push({ date, count, level });
|
| 61 |
+
|
| 62 |
+
if (d.getDay() === 6) {
|
| 63 |
+
weeks.push(week);
|
| 64 |
+
week = [];
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
total,
|
| 70 |
+
year: targetYear,
|
| 71 |
+
weeks,
|
| 72 |
+
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
|
| 73 |
+
weekDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
} catch (error) {
|
| 77 |
+
console.error('Error in buildContributionGraph:', error);
|
| 78 |
+
return { total: 0, weeks: [], year: targetYear, months: [], weekDays: [] };
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// ============================================
|
| 83 |
+
// ✅ دوال الـ Aggregation (من غير activities)
|
| 84 |
+
// ============================================
|
| 85 |
+
async function getPostsAggregation(userId, startDate, endDate) {
|
| 86 |
+
return await Post.aggregate([
|
| 87 |
+
{
|
| 88 |
+
$match: {
|
| 89 |
+
userId: new mongoose.Types.ObjectId(userId),
|
| 90 |
+
createdAt: { $gte: startDate, $lte: endDate }
|
| 91 |
+
}
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
$group: {
|
| 95 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } },
|
| 96 |
+
count: { $sum: 1 }
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
]);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
async function getCommentsAggregation(userId, startDate, endDate) {
|
| 103 |
+
return await Comment.aggregate([
|
| 104 |
+
{
|
| 105 |
+
$match: {
|
| 106 |
+
userId: new mongoose.Types.ObjectId(userId),
|
| 107 |
+
timestamp: { $gte: startDate, $lte: endDate }
|
| 108 |
+
}
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
$group: {
|
| 112 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$timestamp" } },
|
| 113 |
+
count: { $sum: 1 }
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
]);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
async function getLikesAggregation(userId, startDate, endDate) {
|
| 120 |
+
return await Post.aggregate([
|
| 121 |
+
{
|
| 122 |
+
$match: {
|
| 123 |
+
'likes.userId': new mongoose.Types.ObjectId(userId),
|
| 124 |
+
'likes.likedAt': { $gte: startDate, $lte: endDate }
|
| 125 |
+
}
|
| 126 |
+
},
|
| 127 |
+
{ $unwind: "$likes" },
|
| 128 |
+
{
|
| 129 |
+
$match: {
|
| 130 |
+
'likes.userId': new mongoose.Types.ObjectId(userId)
|
| 131 |
+
}
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
$group: {
|
| 135 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$likes.likedAt" } },
|
| 136 |
+
count: { $sum: 1 }
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
]);
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
async function getSharesAggregation(userId, startDate, endDate) {
|
| 143 |
+
return await Post.aggregate([
|
| 144 |
+
{
|
| 145 |
+
$match: {
|
| 146 |
+
'shares.userId': new mongoose.Types.ObjectId(userId),
|
| 147 |
+
'shares.sharedAt': { $gte: startDate, $lte: endDate }
|
| 148 |
+
}
|
| 149 |
+
},
|
| 150 |
+
{ $unwind: "$shares" },
|
| 151 |
+
{
|
| 152 |
+
$match: {
|
| 153 |
+
'shares.userId': new mongoose.Types.ObjectId(userId)
|
| 154 |
+
}
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
$group: {
|
| 158 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$shares.sharedAt" } },
|
| 159 |
+
count: { $sum: 1 }
|
| 160 |
+
}
|
| 161 |
+
}
|
| 162 |
+
]);
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
async function getApplicationsAggregation(userId, startDate, endDate) {
|
| 166 |
+
return await JobApplication.aggregate([
|
| 167 |
+
{
|
| 168 |
+
$match: {
|
| 169 |
+
applicantId: new mongoose.Types.ObjectId(userId),
|
| 170 |
+
appliedAt: { $gte: startDate, $lte: endDate }
|
| 171 |
+
}
|
| 172 |
+
},
|
| 173 |
+
{
|
| 174 |
+
$group: {
|
| 175 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$appliedAt" } },
|
| 176 |
+
count: { $sum: 1 }
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
]);
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
// ============================================
|
| 183 |
+
// ✅ جلب الأنشطة ليوم معين (عند الضغط)
|
| 184 |
+
// ============================================
|
| 185 |
+
async function getDayActivities(userId, date) {
|
| 186 |
+
const startDate = new Date(date);
|
| 187 |
+
const endDate = new Date(date);
|
| 188 |
+
endDate.setDate(endDate.getDate() + 1);
|
| 189 |
+
|
| 190 |
+
const [posts, comments, likes, shares, applications] = await Promise.all([
|
| 191 |
+
Post.find({
|
| 192 |
+
userId: new mongoose.Types.ObjectId(userId),
|
| 193 |
+
createdAt: { $gte: startDate, $lte: endDate }
|
| 194 |
+
}).select('_id title content').lean(),
|
| 195 |
+
|
| 196 |
+
Comment.find({
|
| 197 |
+
userId: new mongoose.Types.ObjectId(userId),
|
| 198 |
+
timestamp: { $gte: startDate, $lte: endDate }
|
| 199 |
+
}).select('_id text projectId').lean(),
|
| 200 |
+
|
| 201 |
+
Post.aggregate([
|
| 202 |
+
{
|
| 203 |
+
$match: {
|
| 204 |
+
'likes.userId': new mongoose.Types.ObjectId(userId),
|
| 205 |
+
'likes.likedAt': { $gte: startDate, $lte: endDate }
|
| 206 |
+
}
|
| 207 |
+
},
|
| 208 |
+
{ $unwind: '$likes' },
|
| 209 |
+
{
|
| 210 |
+
$match: {
|
| 211 |
+
'likes.userId': new mongoose.Types.ObjectId(userId)
|
| 212 |
+
}
|
| 213 |
+
},
|
| 214 |
+
{ $project: { postId: '$_id' } }
|
| 215 |
+
]),
|
| 216 |
+
|
| 217 |
+
Post.aggregate([
|
| 218 |
+
{
|
| 219 |
+
$match: {
|
| 220 |
+
'shares.userId': new mongoose.Types.ObjectId(userId),
|
| 221 |
+
'shares.sharedAt': { $gte: startDate, $lte: endDate }
|
| 222 |
+
}
|
| 223 |
+
},
|
| 224 |
+
{ $unwind: '$shares' },
|
| 225 |
+
{
|
| 226 |
+
$match: {
|
| 227 |
+
'shares.userId': new mongoose.Types.ObjectId(userId)
|
| 228 |
+
}
|
| 229 |
+
},
|
| 230 |
+
{ $project: { postId: '$_id' } }
|
| 231 |
+
]),
|
| 232 |
+
|
| 233 |
+
JobApplication.find({
|
| 234 |
+
applicantId: new mongoose.Types.ObjectId(userId),
|
| 235 |
+
appliedAt: { $gte: startDate, $lte: endDate }
|
| 236 |
+
}).select('_id jobId').lean()
|
| 237 |
+
]);
|
| 238 |
+
|
| 239 |
+
const activities = [];
|
| 240 |
+
|
| 241 |
+
posts.forEach(p => {
|
| 242 |
+
activities.push({
|
| 243 |
+
type: 'post',
|
| 244 |
+
id: p._id,
|
| 245 |
+
title: p.title || p.content?.substring(0, 50) || 'Untitled'
|
| 246 |
+
});
|
| 247 |
+
});
|
| 248 |
+
|
| 249 |
+
comments.forEach(c => {
|
| 250 |
+
activities.push({
|
| 251 |
+
type: 'comment',
|
| 252 |
+
id: c._id,
|
| 253 |
+
text: c.text?.substring(0, 100) || 'Comment',
|
| 254 |
+
projectId: c.projectId
|
| 255 |
+
});
|
| 256 |
+
});
|
| 257 |
+
|
| 258 |
+
likes.forEach(l => {
|
| 259 |
+
activities.push({ type: 'like', postId: l.postId });
|
| 260 |
+
});
|
| 261 |
+
|
| 262 |
+
shares.forEach(s => {
|
| 263 |
+
activities.push({ type: 'share', postId: s.postId });
|
| 264 |
+
});
|
| 265 |
+
|
| 266 |
+
applications.forEach(j => {
|
| 267 |
+
activities.push({
|
| 268 |
+
type: 'job_application',
|
| 269 |
+
id: j._id,
|
| 270 |
+
jobId: j.jobId
|
| 271 |
+
});
|
| 272 |
+
});
|
| 273 |
+
|
| 274 |
+
return activities;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
module.exports = {
|
| 278 |
+
buildContributionGraph,
|
| 279 |
+
getDayActivities
|
| 280 |
+
};
|