Mark-Lasfar commited on
Commit ·
770f5a7
1
Parent(s): d9746eb
Add GraphQL
Browse files- server.js +370 -1
- services/contribution.service.js +0 -280
server.js
CHANGED
|
@@ -100,7 +100,7 @@ const aiService = require('./services/aiService');
|
|
| 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 عرفته فوق)
|
|
@@ -18435,6 +18435,375 @@ app.get('/api/users/:userId/contributions/:date', async (req, res) => {
|
|
| 18435 |
}
|
| 18436 |
});
|
| 18437 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18438 |
// ============================================
|
| 18439 |
// GET /api/analytics/also-viewed - جلب المستخدمين الذين شاهدوا هذا الملف أيضاً
|
| 18440 |
// ============================================
|
|
|
|
| 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 عرفته فوق)
|
|
|
|
| 18435 |
}
|
| 18436 |
});
|
| 18437 |
|
| 18438 |
+
|
| 18439 |
+
// ============================================
|
| 18440 |
+
// ✅ Service موحدة لبناء Contribution Graph
|
| 18441 |
+
// ============================================
|
| 18442 |
+
async function buildContributionGraph(userId, year) {
|
| 18443 |
+
if (!userId) {
|
| 18444 |
+
return { total: 0, weeks: [], year: year || new Date().getFullYear(), months: [], weekDays: [] };
|
| 18445 |
+
}
|
| 18446 |
+
|
| 18447 |
+
const targetYear = year || new Date().getFullYear();
|
| 18448 |
+
const startDate = new Date(targetYear, 0, 1);
|
| 18449 |
+
const endDate = new Date(targetYear, 11, 31, 23, 59, 59);
|
| 18450 |
+
|
| 18451 |
+
try {
|
| 18452 |
+
// ✅ جلب البيانات المجمعة (بدون countDocuments منفصلة)
|
| 18453 |
+
const [
|
| 18454 |
+
posts,
|
| 18455 |
+
projectComments,
|
| 18456 |
+
postComments,
|
| 18457 |
+
likes,
|
| 18458 |
+
shares,
|
| 18459 |
+
applications
|
| 18460 |
+
] = await Promise.all([
|
| 18461 |
+
getPostsAggregation(userId, startDate, endDate),
|
| 18462 |
+
getCommentsAggregation(userId, startDate, endDate),
|
| 18463 |
+
getPostCommentsAggregation(userId, startDate, endDate),
|
| 18464 |
+
getLikesAggregation(userId, startDate, endDate),
|
| 18465 |
+
getSharesAggregation(userId, startDate, endDate),
|
| 18466 |
+
getApplicationsAggregation(userId, startDate, endDate)
|
| 18467 |
+
]);
|
| 18468 |
+
|
| 18469 |
+
// ✅ دمج البيانات في Map
|
| 18470 |
+
const daily = new Map();
|
| 18471 |
+
|
| 18472 |
+
function merge(data) {
|
| 18473 |
+
data.forEach(item => {
|
| 18474 |
+
const date = item._id;
|
| 18475 |
+
if (!daily.has(date)) daily.set(date, 0);
|
| 18476 |
+
daily.set(date, daily.get(date) + item.count);
|
| 18477 |
+
});
|
| 18478 |
+
}
|
| 18479 |
+
|
| 18480 |
+
merge(posts);
|
| 18481 |
+
merge(projectComments);
|
| 18482 |
+
merge(postComments);
|
| 18483 |
+
merge(likes);
|
| 18484 |
+
merge(shares);
|
| 18485 |
+
merge(applications);
|
| 18486 |
+
|
| 18487 |
+
let total = 0;
|
| 18488 |
+
const weeks = [];
|
| 18489 |
+
let week = [];
|
| 18490 |
+
|
| 18491 |
+
for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
|
| 18492 |
+
const date = d.toISOString().split('T')[0];
|
| 18493 |
+
const count = daily.get(date) || 0;
|
| 18494 |
+
total += count;
|
| 18495 |
+
|
| 18496 |
+
let level = 0;
|
| 18497 |
+
if (count > 0 && count <= 2) level = 1;
|
| 18498 |
+
else if (count <= 5) level = 2;
|
| 18499 |
+
else if (count <= 10) level = 3;
|
| 18500 |
+
else if (count > 10) level = 4;
|
| 18501 |
+
|
| 18502 |
+
// ✅ من غير activities
|
| 18503 |
+
week.push({ date, count, level });
|
| 18504 |
+
|
| 18505 |
+
if (d.getDay() === 6) {
|
| 18506 |
+
weeks.push(week);
|
| 18507 |
+
week = [];
|
| 18508 |
+
}
|
| 18509 |
+
}
|
| 18510 |
+
|
| 18511 |
+
return {
|
| 18512 |
+
total,
|
| 18513 |
+
year: targetYear,
|
| 18514 |
+
weeks,
|
| 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) {
|
| 18520 |
+
console.error('Error in buildContributionGraph:', error);
|
| 18521 |
+
return { total: 0, weeks: [], year: targetYear, months: [], weekDays: [] };
|
| 18522 |
+
}
|
| 18523 |
+
}
|
| 18524 |
+
|
| 18525 |
+
// ============================================
|
| 18526 |
+
// ✅ دوال الـ Aggregation (من غير activities)
|
| 18527 |
+
// ============================================
|
| 18528 |
+
async function getPostsAggregation(userId, startDate, endDate) {
|
| 18529 |
+
return await Post.aggregate([
|
| 18530 |
+
{
|
| 18531 |
+
$match: {
|
| 18532 |
+
userId: new mongoose.Types.ObjectId(userId),
|
| 18533 |
+
createdAt: { $gte: startDate, $lte: endDate }
|
| 18534 |
+
}
|
| 18535 |
+
},
|
| 18536 |
+
{
|
| 18537 |
+
$group: {
|
| 18538 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } },
|
| 18539 |
+
count: { $sum: 1 }
|
| 18540 |
+
}
|
| 18541 |
+
}
|
| 18542 |
+
]);
|
| 18543 |
+
}
|
| 18544 |
+
|
| 18545 |
+
async function getCommentsAggregation(userId, startDate, endDate) {
|
| 18546 |
+
return await Comment.aggregate([
|
| 18547 |
+
{
|
| 18548 |
+
$match: {
|
| 18549 |
+
userId: new mongoose.Types.ObjectId(userId),
|
| 18550 |
+
timestamp: { $gte: startDate, $lte: endDate }
|
| 18551 |
+
}
|
| 18552 |
+
},
|
| 18553 |
+
{
|
| 18554 |
+
$group: {
|
| 18555 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$timestamp" } },
|
| 18556 |
+
count: { $sum: 1 }
|
| 18557 |
+
}
|
| 18558 |
+
}
|
| 18559 |
+
]);
|
| 18560 |
+
}
|
| 18561 |
+
|
| 18562 |
+
|
| 18563 |
+
async function getPostCommentsAggregation(userId, startDate, endDate) {
|
| 18564 |
+
return await Post.aggregate([
|
| 18565 |
+
{
|
| 18566 |
+
$unwind: "$comments"
|
| 18567 |
+
},
|
| 18568 |
+
{
|
| 18569 |
+
$match: {
|
| 18570 |
+
"comments.userId": new mongoose.Types.ObjectId(userId),
|
| 18571 |
+
"comments.createdAt": {
|
| 18572 |
+
$gte: startDate,
|
| 18573 |
+
$lte: endDate
|
| 18574 |
+
}
|
| 18575 |
+
}
|
| 18576 |
+
},
|
| 18577 |
+
{
|
| 18578 |
+
$group: {
|
| 18579 |
+
_id: {
|
| 18580 |
+
$dateToString: {
|
| 18581 |
+
format: "%Y-%m-%d",
|
| 18582 |
+
date: "$comments.createdAt"
|
| 18583 |
+
}
|
| 18584 |
+
},
|
| 18585 |
+
count: {
|
| 18586 |
+
$sum: 1
|
| 18587 |
+
}
|
| 18588 |
+
}
|
| 18589 |
+
}
|
| 18590 |
+
]);
|
| 18591 |
+
}
|
| 18592 |
+
|
| 18593 |
+
async function getLikesAggregation(userId, startDate, endDate) {
|
| 18594 |
+
return await Post.aggregate([
|
| 18595 |
+
{
|
| 18596 |
+
$unwind: "$likes"
|
| 18597 |
+
},
|
| 18598 |
+
{
|
| 18599 |
+
$match: {
|
| 18600 |
+
"likes.userId": new mongoose.Types.ObjectId(userId),
|
| 18601 |
+
"likes.likedAt": {
|
| 18602 |
+
$gte: startDate,
|
| 18603 |
+
$lte: endDate
|
| 18604 |
+
}
|
| 18605 |
+
}
|
| 18606 |
+
},
|
| 18607 |
+
{
|
| 18608 |
+
$group: {
|
| 18609 |
+
_id: {
|
| 18610 |
+
$dateToString: {
|
| 18611 |
+
format: "%Y-%m-%d",
|
| 18612 |
+
date: "$likes.likedAt"
|
| 18613 |
+
}
|
| 18614 |
+
},
|
| 18615 |
+
count: {
|
| 18616 |
+
$sum: 1
|
| 18617 |
+
}
|
| 18618 |
+
}
|
| 18619 |
+
}
|
| 18620 |
+
]);
|
| 18621 |
+
}
|
| 18622 |
+
|
| 18623 |
+
async function getSharesAggregation(userId, startDate, endDate) {
|
| 18624 |
+
return await Post.aggregate([
|
| 18625 |
+
{
|
| 18626 |
+
$match: {
|
| 18627 |
+
'shares.userId': new mongoose.Types.ObjectId(userId),
|
| 18628 |
+
'shares.sharedAt': { $gte: startDate, $lte: endDate }
|
| 18629 |
+
}
|
| 18630 |
+
},
|
| 18631 |
+
{ $unwind: "$shares" },
|
| 18632 |
+
{
|
| 18633 |
+
$match: {
|
| 18634 |
+
'shares.userId': new mongoose.Types.ObjectId(userId)
|
| 18635 |
+
}
|
| 18636 |
+
},
|
| 18637 |
+
{
|
| 18638 |
+
$group: {
|
| 18639 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$shares.sharedAt" } },
|
| 18640 |
+
count: { $sum: 1 }
|
| 18641 |
+
}
|
| 18642 |
+
}
|
| 18643 |
+
]);
|
| 18644 |
+
}
|
| 18645 |
+
|
| 18646 |
+
async function getApplicationsAggregation(userId, startDate, endDate) {
|
| 18647 |
+
return await JobApplication.aggregate([
|
| 18648 |
+
{
|
| 18649 |
+
$match: {
|
| 18650 |
+
applicantId: new mongoose.Types.ObjectId(userId),
|
| 18651 |
+
appliedAt: { $gte: startDate, $lte: endDate }
|
| 18652 |
+
}
|
| 18653 |
+
},
|
| 18654 |
+
{
|
| 18655 |
+
$group: {
|
| 18656 |
+
_id: { $dateToString: { format: "%Y-%m-%d", date: "$appliedAt" } },
|
| 18657 |
+
count: { $sum: 1 }
|
| 18658 |
+
}
|
| 18659 |
+
}
|
| 18660 |
+
]);
|
| 18661 |
+
}
|
| 18662 |
+
|
| 18663 |
+
// ============================================
|
| 18664 |
+
// ✅ جلب الأنشطة ليوم معين (عند الضغط)
|
| 18665 |
+
// ============================================
|
| 18666 |
+
async function getDayActivities(userId, date) {
|
| 18667 |
+
const startDate = new Date(date);
|
| 18668 |
+
const endDate = new Date(date);
|
| 18669 |
+
endDate.setDate(endDate.getDate() + 1);
|
| 18670 |
+
|
| 18671 |
+
const [
|
| 18672 |
+
posts,
|
| 18673 |
+
projectComments,
|
| 18674 |
+
postComments,
|
| 18675 |
+
likes,
|
| 18676 |
+
shares,
|
| 18677 |
+
applications
|
| 18678 |
+
] = await Promise.all([
|
| 18679 |
+
Post.find({
|
| 18680 |
+
userId: new mongoose.Types.ObjectId(userId),
|
| 18681 |
+
createdAt: { $gte: startDate, $lte: endDate }
|
| 18682 |
+
}).select('_id title content').lean(),
|
| 18683 |
+
Comment.find({
|
| 18684 |
+
userId: new mongoose.Types.ObjectId(userId),
|
| 18685 |
+
timestamp: { $gte: startDate, $lte:endDate }
|
| 18686 |
+
}).select('_id text projectId').lean(),
|
| 18687 |
+
|
| 18688 |
+
|
| 18689 |
+
// ✅ Post.comments
|
| 18690 |
+
Post.aggregate([
|
| 18691 |
+
{
|
| 18692 |
+
$unwind: "$comments"
|
| 18693 |
+
},
|
| 18694 |
+
{
|
| 18695 |
+
$match:{
|
| 18696 |
+
"comments.userId": new mongoose.Types.ObjectId(userId),
|
| 18697 |
+
"comments.createdAt":{
|
| 18698 |
+
$gte:startDate,
|
| 18699 |
+
$lte:endDate
|
| 18700 |
+
}
|
| 18701 |
+
}
|
| 18702 |
+
},
|
| 18703 |
+
{
|
| 18704 |
+
$project:{
|
| 18705 |
+
_id:"$comments._id",
|
| 18706 |
+
text:"$comments.text",
|
| 18707 |
+
postId:"$_id"
|
| 18708 |
+
}
|
| 18709 |
+
}
|
| 18710 |
+
]),
|
| 18711 |
+
|
| 18712 |
+
|
| 18713 |
+
// ✅ Likes
|
| 18714 |
+
Post.aggregate([
|
| 18715 |
+
{
|
| 18716 |
+
$unwind:"$likes"
|
| 18717 |
+
},
|
| 18718 |
+
{
|
| 18719 |
+
$match:{
|
| 18720 |
+
"likes.userId": new mongoose.Types.ObjectId(userId),
|
| 18721 |
+
"likes.likedAt":{
|
| 18722 |
+
$gte:startDate,
|
| 18723 |
+
$lte:endDate
|
| 18724 |
+
}
|
| 18725 |
+
}
|
| 18726 |
+
},
|
| 18727 |
+
{
|
| 18728 |
+
$project:{
|
| 18729 |
+
postId:"$_id"
|
| 18730 |
+
}
|
| 18731 |
+
}
|
| 18732 |
+
]),
|
| 18733 |
+
|
| 18734 |
+
Post.aggregate([
|
| 18735 |
+
{
|
| 18736 |
+
$unwind:"$shares"
|
| 18737 |
+
},
|
| 18738 |
+
{
|
| 18739 |
+
$match:{
|
| 18740 |
+
"shares.userId": new mongoose.Types.ObjectId(userId),
|
| 18741 |
+
"shares.sharedAt":{
|
| 18742 |
+
$gte:startDate,
|
| 18743 |
+
$lte:endDate
|
| 18744 |
+
}
|
| 18745 |
+
}
|
| 18746 |
+
},
|
| 18747 |
+
{
|
| 18748 |
+
$project:{
|
| 18749 |
+
postId:"$_id"
|
| 18750 |
+
}
|
| 18751 |
+
}
|
| 18752 |
+
]),
|
| 18753 |
+
|
| 18754 |
+
JobApplication.find({
|
| 18755 |
+
applicantId: new mongoose.Types.ObjectId(userId),
|
| 18756 |
+
appliedAt: { $gte: startDate, $lte: endDate }
|
| 18757 |
+
}).select('_id jobId').lean()
|
| 18758 |
+
]);
|
| 18759 |
+
|
| 18760 |
+
const activities = [];
|
| 18761 |
+
|
| 18762 |
+
posts.forEach(p => {
|
| 18763 |
+
activities.push({
|
| 18764 |
+
type: 'post',
|
| 18765 |
+
id: p._id,
|
| 18766 |
+
title: p.title || p.content?.substring(0, 50) || 'Untitled'
|
| 18767 |
+
});
|
| 18768 |
+
});
|
| 18769 |
+
|
| 18770 |
+
projectComments.forEach(c => {
|
| 18771 |
+
activities.push({
|
| 18772 |
+
type: 'comment',
|
| 18773 |
+
id: c._id,
|
| 18774 |
+
text: c.text?.substring(0, 100) || 'Comment',
|
| 18775 |
+
projectId: c.projectId
|
| 18776 |
+
});
|
| 18777 |
+
});
|
| 18778 |
+
|
| 18779 |
+
postComments.forEach(c => {
|
| 18780 |
+
activities.push({
|
| 18781 |
+
type:'post_comment',
|
| 18782 |
+
id:c._id,
|
| 18783 |
+
text:c.text?.substring(0,100) || 'Comment',
|
| 18784 |
+
postId:c.postId
|
| 18785 |
+
});
|
| 18786 |
+
});
|
| 18787 |
+
|
| 18788 |
+
likes.forEach(l => {
|
| 18789 |
+
activities.push({ type: 'like', postId: l.postId });
|
| 18790 |
+
});
|
| 18791 |
+
|
| 18792 |
+
shares.forEach(s => {
|
| 18793 |
+
activities.push({ type: 'share', postId: s.postId });
|
| 18794 |
+
});
|
| 18795 |
+
|
| 18796 |
+
applications.forEach(j => {
|
| 18797 |
+
activities.push({
|
| 18798 |
+
type: 'job_application',
|
| 18799 |
+
id: j._id,
|
| 18800 |
+
jobId: j.jobId
|
| 18801 |
+
});
|
| 18802 |
+
});
|
| 18803 |
+
|
| 18804 |
+
return activities;
|
| 18805 |
+
}
|
| 18806 |
+
|
| 18807 |
// ============================================
|
| 18808 |
// GET /api/analytics/also-viewed - جلب المستخدمين الذين شاهدوا هذا الملف أيضاً
|
| 18809 |
// ============================================
|
services/contribution.service.js
DELETED
|
@@ -1,280 +0,0 @@
|
|
| 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 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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