Mark-Lasfar commited on
Commit
8b99c44
·
1 Parent(s): ae093cf

Add GraphQL

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