Mark-Lasfar commited on
Commit
ea0b187
·
1 Parent(s): 6fb1d7a

Add GraphQL

Browse files
Files changed (2) hide show
  1. graphql/resolvers.js +69 -17
  2. graphql/schema.js +10 -3
graphql/resolvers.js CHANGED
@@ -1715,25 +1715,77 @@ const Query = {
1715
  Post.countDocuments({ userId, ...visibilityFilter })
1716
  ]);
1717
 
1718
- // ✅ حساب القيم مباشرة
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1719
  return {
1720
- data: posts.map(post => ({
1721
- ...post,
1722
- sharedFrom: post.sharedFrom || null,
1723
- likesCount: (post.likes || []).length,
1724
- commentsCount: (post.comments || []).length,
1725
- sharesCount: (post.shares || []).length,
1726
- savesCount: (post.saves || []).length,
1727
- isLiked: post.likes?.some(l => l.userId?.toString() === context.user.userId) || false,
1728
- isSaved: post.saves?.some(s => s.userId?.toString() === context.user.userId) || false
1729
- })),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1730
  pagination: {
1731
- page,
1732
- limit,
1733
- total,
1734
- pages: Math.ceil(total / limit),
1735
- hasNext: skip + limit < total,
1736
- hasPrev: page > 1
1737
  }
1738
  };
1739
  },
 
1715
  Post.countDocuments({ userId, ...visibilityFilter })
1716
  ]);
1717
 
1718
+ // ✅ جلب البوستات الأصلية للمشاركات
1719
+ const sharedPostIds = posts
1720
+ .filter(p => p.sharedFrom?.originalPostId)
1721
+ .map(p => p.sharedFrom.originalPostId);
1722
+
1723
+ let originalPostsMap = new Map();
1724
+ if (sharedPostIds.length > 0) {
1725
+ const originalPosts = await Post.find({
1726
+ _id: { $in: sharedPostIds }
1727
+ })
1728
+ .populate('userId', 'username profile.nickname profile.avatar profile.jobTitle')
1729
+ .lean();
1730
+
1731
+ originalPosts.forEach(op => {
1732
+ originalPostsMap.set(op._id.toString(), {
1733
+ _id: op._id,
1734
+ content: op.content || '',
1735
+ images: op.images || [],
1736
+ video: op.video || null,
1737
+ userId: op.userId,
1738
+ createdAt: op.createdAt,
1739
+ likesCount: op.likes?.length || 0,
1740
+ commentsCount: op.comments?.length || 0,
1741
+ sharesCount: op.shares?.length || 0
1742
+ });
1743
+ });
1744
+ }
1745
+
1746
+ // ✅ حساب القيم مع إضافة بيانات البوست الأصلي
1747
  return {
1748
+ data: posts.map(post => {
1749
+ let sharedFrom = post.sharedFrom || null;
1750
+
1751
+ // إضافة بيانات البوست الأصلي للمشاركات
1752
+ if (sharedFrom && sharedFrom.originalPostId) {
1753
+ const originalPost = originalPostsMap.get(sharedFrom.originalPostId.toString());
1754
+ if (originalPost) {
1755
+ sharedFrom = {
1756
+ ...sharedFrom,
1757
+ originalAuthorId: originalPost.userId?._id || null,
1758
+ originalAuthorAvatar: originalPost.userId?.profile?.avatar || null,
1759
+ originalAuthorJobTitle: originalPost.userId?.profile?.jobTitle || null,
1760
+ originalContent: originalPost.content || '',
1761
+ originalImages: originalPost.images || [],
1762
+ originalVideo: originalPost.video || null,
1763
+ originalCreatedAt: originalPost.createdAt || null,
1764
+ originalLikesCount: originalPost.likesCount || 0,
1765
+ originalCommentsCount: originalPost.commentsCount || 0,
1766
+ originalSharesCount: originalPost.sharesCount || 0
1767
+ };
1768
+ }
1769
+ }
1770
+
1771
+ return {
1772
+ ...post,
1773
+ sharedFrom,
1774
+ likesCount: post.likes?.length || 0,
1775
+ commentsCount: post.comments?.length || 0,
1776
+ sharesCount: post.shares?.length || 0,
1777
+ savesCount: post.saves?.length || 0,
1778
+ isLiked: post.likes?.some(l => l.userId?.toString() === context.user?.userId) || false,
1779
+ isSaved: post.saves?.some(s => s.userId?.toString() === context.user?.userId) || false
1780
+ };
1781
+ }),
1782
  pagination: {
1783
+ page,
1784
+ limit,
1785
+ total,
1786
+ pages: Math.ceil(total / limit),
1787
+ hasNext: skip + limit < total,
1788
+ hasPrev: page > 1
1789
  }
1790
  };
1791
  },
graphql/schema.js CHANGED
@@ -712,14 +712,21 @@ const typeDefs = gql`
712
  username: String
713
  position: Int
714
  }
715
-
716
  type SharedFrom {
717
  originalPostId: ID
718
- originalAuthorId: User
719
  originalAuthorName: String
 
 
 
 
 
 
 
 
 
720
  sharedAt: Date
721
  }
722
-
723
  # ============================================
724
  # 🔹 STORY TYPES
725
  # ============================================
 
712
  username: String
713
  position: Int
714
  }
 
715
  type SharedFrom {
716
  originalPostId: ID
717
+ originalAuthorId: ID
718
  originalAuthorName: String
719
+ originalAuthorAvatar: String
720
+ originalAuthorJobTitle: String
721
+ originalContent: String
722
+ originalImages: [PostImage!]!
723
+ originalVideo: PostVideo
724
+ originalCreatedAt: Date
725
+ originalLikesCount: Int
726
+ originalCommentsCount: Int
727
+ originalSharesCount: Int
728
  sharedAt: Date
729
  }
 
730
  # ============================================
731
  # 🔹 STORY TYPES
732
  # ============================================