Mark-Lasfar commited on
Commit ·
ed2a9e1
1
Parent(s): d27b740
Add GraphQL
Browse files- graphql/resolvers.js +97 -47
graphql/resolvers.js
CHANGED
|
@@ -447,54 +447,104 @@ const resolvers = {
|
|
| 447 |
// 🔹 POST RESOLVERS - ✅ الحل النهائي
|
| 448 |
// ============================================
|
| 449 |
Post: {
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
}
|
| 476 |
-
return parent.saves.length;
|
| 477 |
-
},
|
| 478 |
-
isLiked: (parent, args, context) => {
|
| 479 |
-
if (!context.user) return false;
|
| 480 |
-
if (!parent || !parent.likes || !Array.isArray(parent.likes)) return false;
|
| 481 |
-
return parent.likes.some(l => l.userId?.toString() === context.user.userId);
|
| 482 |
-
},
|
| 483 |
-
isSaved: (parent, args, context) => {
|
| 484 |
-
if (!context.user) return false;
|
| 485 |
-
if (!parent || !parent.saves || !Array.isArray(parent.saves)) return false;
|
| 486 |
-
return parent.saves.some(s => s.userId?.toString() === context.user.userId);
|
| 487 |
-
},
|
| 488 |
-
comments: async (parent) => {
|
| 489 |
-
// جلب التعليقات مع isLiked
|
| 490 |
-
const comments = parent.comments || [];
|
| 491 |
-
return comments.map(c => ({
|
| 492 |
-
...c,
|
| 493 |
-
likesCount: c.likes?.length || 0,
|
| 494 |
-
isLiked: c.likes?.some(l => l.userId?.toString() === context.user?.userId) || false
|
| 495 |
-
}));
|
| 496 |
-
}
|
| 497 |
-
|
| 498 |
},
|
| 499 |
|
| 500 |
// ============================================
|
|
|
|
| 447 |
// 🔹 POST RESOLVERS - ✅ الحل النهائي
|
| 448 |
// ============================================
|
| 449 |
Post: {
|
| 450 |
+
userId: async (parent) => {
|
| 451 |
+
return await User.findById(parent.userId);
|
| 452 |
+
},
|
| 453 |
+
likesCount: (parent) => {
|
| 454 |
+
if (!parent || !parent.likes || !Array.isArray(parent.likes)) {
|
| 455 |
+
return 0;
|
| 456 |
+
}
|
| 457 |
+
return parent.likes.length;
|
| 458 |
+
},
|
| 459 |
+
commentsCount: (parent) => {
|
| 460 |
+
if (!parent || !parent.comments || !Array.isArray(parent.comments)) {
|
| 461 |
+
return 0;
|
| 462 |
+
}
|
| 463 |
+
return parent.comments.length;
|
| 464 |
+
},
|
| 465 |
+
sharesCount: (parent) => {
|
| 466 |
+
if (!parent || !parent.shares || !Array.isArray(parent.shares)) {
|
| 467 |
+
return 0;
|
| 468 |
+
}
|
| 469 |
+
return parent.shares.length;
|
| 470 |
+
},
|
| 471 |
+
savesCount: (parent) => {
|
| 472 |
+
if (!parent || !parent.saves || !Array.isArray(parent.saves)) {
|
| 473 |
+
return 0;
|
| 474 |
+
}
|
| 475 |
+
return parent.saves.length;
|
| 476 |
+
},
|
| 477 |
+
isLiked: (parent, args, context) => {
|
| 478 |
+
if (!context?.user) return false;
|
| 479 |
+
if (!parent || !parent.likes || !Array.isArray(parent.likes)) return false;
|
| 480 |
+
return parent.likes.some(l => l.userId?.toString() === context.user.userId);
|
| 481 |
+
},
|
| 482 |
+
isSaved: (parent, args, context) => {
|
| 483 |
+
if (!context?.user) return false;
|
| 484 |
+
if (!parent || !parent.saves || !Array.isArray(parent.saves)) return false;
|
| 485 |
+
return parent.saves.some(s => s.userId?.toString() === context.user.userId);
|
| 486 |
+
},
|
| 487 |
+
comments: async (parent, args, context) => {
|
| 488 |
+
const comments = parent.comments || [];
|
| 489 |
+
const userId = context?.user?.userId;
|
| 490 |
+
|
| 491 |
+
if (comments.length === 0) return [];
|
| 492 |
+
|
| 493 |
+
// ✅ جلب جميع معرفات المستخدمين
|
| 494 |
+
const userIds = new Set();
|
| 495 |
+
comments.forEach(c => {
|
| 496 |
+
if (c.userId) userIds.add(c.userId.toString());
|
| 497 |
+
if (c.replies && c.replies.length > 0) {
|
| 498 |
+
c.replies.forEach(r => {
|
| 499 |
+
if (r.userId) userIds.add(r.userId.toString());
|
| 500 |
+
});
|
| 501 |
+
}
|
| 502 |
+
});
|
| 503 |
+
|
| 504 |
+
// ✅ جلب جميع المستخدمين دفعة واحدة
|
| 505 |
+
const users = await User.find({
|
| 506 |
+
_id: { $in: Array.from(userIds) }
|
| 507 |
+
}).select('username profile.nickname profile.avatar profile.jobTitle').lean();
|
| 508 |
+
|
| 509 |
+
const usersMap = {};
|
| 510 |
+
users.forEach(u => {
|
| 511 |
+
usersMap[u._id.toString()] = u;
|
| 512 |
+
});
|
| 513 |
+
|
| 514 |
+
// ✅ معالجة التعليقات
|
| 515 |
+
return comments.map(c => {
|
| 516 |
+
const user = c.userId ? usersMap[c.userId.toString()] : null;
|
| 517 |
+
|
| 518 |
+
let replies = [];
|
| 519 |
+
if (c.replies && c.replies.length > 0) {
|
| 520 |
+
replies = c.replies.map(r => {
|
| 521 |
+
const replyUser = r.userId ? usersMap[r.userId.toString()] : null;
|
| 522 |
+
return {
|
| 523 |
+
...r,
|
| 524 |
+
userId: replyUser || {
|
| 525 |
+
_id: r.userId,
|
| 526 |
+
username: 'Unknown',
|
| 527 |
+
profile: { nickname: 'Unknown', avatar: '/assets/img/default-avatar.png', jobTitle: '' }
|
| 528 |
+
},
|
| 529 |
+
likesCount: r.likes?.length || 0,
|
| 530 |
+
isLiked: r.likes?.some(l => l.userId?.toString() === userId) || false
|
| 531 |
+
};
|
| 532 |
+
});
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
return {
|
| 536 |
+
...c,
|
| 537 |
+
userId: user || {
|
| 538 |
+
_id: c.userId,
|
| 539 |
+
username: 'Unknown',
|
| 540 |
+
profile: { nickname: 'Unknown', avatar: '/assets/img/default-avatar.png', jobTitle: '' }
|
| 541 |
+
},
|
| 542 |
+
likesCount: c.likes?.length || 0,
|
| 543 |
+
isLiked: c.likes?.some(l => l.userId?.toString() === userId) || false,
|
| 544 |
+
replies: replies
|
| 545 |
+
};
|
| 546 |
+
});
|
| 547 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
},
|
| 549 |
|
| 550 |
// ============================================
|