Mark-Lasfar commited on
Commit
9587ca7
·
1 Parent(s): 9d077c0

Add GraphQL

Browse files
Files changed (1) hide show
  1. graphql/resolvers.js +38 -102
graphql/resolvers.js CHANGED
@@ -486,108 +486,44 @@ const resolvers = {
486
  return parent.saves.some(s => s.userId?.toString() === context.user.userId);
487
  },
488
  comments: async (parent, args, context) => {
489
- const comments = parent.comments || [];
490
- const userId = context?.user?.userId;
491
-
492
- if (comments.length === 0) return [];
493
-
494
- // ✅ جلب جميع معرفات المستخدمين (تجاهل الـ null)
495
- const userIds = new Set();
496
- comments.forEach(c => {
497
- if (c.userId) userIds.add(c.userId.toString());
498
- if (c.replies && c.replies.length > 0) {
499
- c.replies.forEach(r => {
500
- if (r.userId) userIds.add(r.userId.toString());
501
- });
502
- }
503
- });
504
-
505
- // ✅ جلب جميع المستخدمين دفعة واحدة
506
- let usersMap = {};
507
- if (userIds.size > 0) {
508
- const users = await User.find({
509
- _id: { $in: Array.from(userIds) }
510
- }).select('username profile.nickname profile.avatar profile.jobTitle').lean();
511
-
512
- users.forEach(u => {
513
- usersMap[u._id.toString()] = u;
514
- });
515
- }
516
-
517
- // ✅ معالجة التعليقات
518
- return comments.map(c => {
519
- // ✅ البحث عن المستخدم (مع التحويل إلى string)
520
- let user = null;
521
- if (c.userId) {
522
- const userIdStr = c.userId.toString();
523
- user = usersMap[userIdStr] || null;
524
- }
525
-
526
- // ✅ بناء بيانات المستخدم (مع fallback)
527
- const userData = user ? {
528
- _id: user._id,
529
- username: user.username || 'Unknown',
530
- profile: {
531
- nickname: user.profile?.nickname || user.username || 'Unknown',
532
- avatar: user.profile?.avatar || '/assets/img/default-avatar.png',
533
- jobTitle: user.profile?.jobTitle || ''
534
- }
535
- } : {
536
- _id: c.userId || 'unknown',
537
- username: 'Unknown',
538
- profile: {
539
- nickname: 'Unknown',
540
- avatar: '/assets/img/default-avatar.png',
541
- jobTitle: ''
542
- }
543
- };
544
-
545
- // ✅ معالجة الردود
546
- let replies = [];
547
- if (c.replies && c.replies.length > 0) {
548
- replies = c.replies.map(r => {
549
- let replyUser = null;
550
- if (r.userId) {
551
- const replyUserIdStr = r.userId.toString();
552
- replyUser = usersMap[replyUserIdStr] || null;
553
- }
554
-
555
- const replyUserData = replyUser ? {
556
- _id: replyUser._id,
557
- username: replyUser.username || 'Unknown',
558
- profile: {
559
- nickname: replyUser.profile?.nickname || replyUser.username || 'Unknown',
560
- avatar: replyUser.profile?.avatar || '/assets/img/default-avatar.png',
561
- jobTitle: replyUser.profile?.jobTitle || ''
562
- }
563
- } : {
564
- _id: r.userId || 'unknown',
565
- username: 'Unknown',
566
- profile: {
567
- nickname: 'Unknown',
568
- avatar: '/assets/img/default-avatar.png',
569
- jobTitle: ''
570
- }
571
- };
572
-
573
- return {
574
- ...r,
575
- userId: replyUserData,
576
- likesCount: r.likes?.length || 0,
577
- isLiked: r.likes?.some(l => l.userId?.toString() === userId) || false
578
- };
579
- });
580
- }
581
-
582
- return {
583
- ...c,
584
- userId: userData,
585
- likesCount: c.likes?.length || 0,
586
- isLiked: c.likes?.some(l => l.userId?.toString() === userId) || false,
587
- replies: replies
588
- };
589
- });
590
- }
591
  },
592
 
593
  // ============================================
 
486
  return parent.saves.some(s => s.userId?.toString() === context.user.userId);
487
  },
488
  comments: async (parent, args, context) => {
489
+ if (!parent.comments || parent.comments.length === 0) return [];
490
+
491
+ // ✅ جلب الـ comments مع populate
492
+ const populatedPost = await Post.findById(parent._id)
493
+ .populate({
494
+ path: 'comments.userId',
495
+ select: 'username profile.nickname profile.avatar profile.jobTitle'
496
+ })
497
+ .populate({
498
+ path: 'comments.replies.userId',
499
+ select: 'username profile.nickname profile.avatar profile.jobTitle'
500
+ })
501
+ .lean();
502
+
503
+ if (!populatedPost) return [];
504
+
505
+ // ✅ معالجة التعليقات
506
+ return populatedPost.comments.map(c => ({
507
+ ...c,
508
+ userId: c.userId || {
509
+ _id: c.userId || 'unknown',
510
+ username: 'Unknown',
511
+ profile: { nickname: 'Unknown', avatar: '/assets/img/default-avatar.png', jobTitle: '' }
512
+ },
513
+ likesCount: c.likes?.length || 0,
514
+ isLiked: c.likes?.some(l => l.userId?.toString() === context.user?.userId) || false,
515
+ replies: (c.replies || []).map(r => ({
516
+ ...r,
517
+ userId: r.userId || {
518
+ _id: r.userId || 'unknown',
519
+ username: 'Unknown',
520
+ profile: { nickname: 'Unknown', avatar: '/assets/img/default-avatar.png', jobTitle: '' }
521
+ },
522
+ likesCount: r.likes?.length || 0,
523
+ isLiked: r.likes?.some(l => l.userId?.toString() === context.user?.userId) || false
524
+ }))
525
+ }));
526
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
  },
528
 
529
  // ============================================