Mark-Lasfar commited on
Commit
ea613b4
·
1 Parent(s): 8f123d6

Add GraphQL

Browse files
Files changed (1) hide show
  1. graphql/resolvers.js +71 -49
graphql/resolvers.js CHANGED
@@ -1662,36 +1662,81 @@ const Query = {
1662
  },
1663
 
1664
  post: async (parent, { id }, context) => {
1665
- const post = await Post.findById(id)
1666
- .populate('userId', 'username profile.nickname profile.avatar profile.jobTitle profile.bio')
1667
- // ✅ جلب التعليقات مع populate زي ما كنت بتعمل في REST
1668
- .populate({
1669
- path: 'comments.userId',
1670
- select: 'username profile.nickname profile.avatar profile.jobTitle'
1671
- })
1672
- .populate({
1673
- path: 'comments.replies.userId',
1674
- select: 'username profile.nickname profile.avatar profile.jobTitle'
1675
- })
1676
- .lean();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1677
 
1678
- if (!post) throw new GraphQLError('Post not found');
 
 
1679
 
1680
- // التحقق من الخصوصية
1681
- const isOwner = context.user && context.user.userId === post.userId._id.toString();
1682
- const isFollowing = context.user ? await Follow.findOne({
1683
- followerId: context.user.userId,
1684
- followingId: post.userId._id,
1685
- status: 'accepted'
1686
- }) : false;
1687
 
1688
- if (!isOwner && post.visibility === 'only-me') {
1689
- throw new GraphQLError('This post is private');
1690
- }
1691
- if (post.visibility === 'followers' && !isOwner && !isFollowing) {
1692
- throw new GraphQLError('This post is only visible to followers');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1693
  }
1694
 
 
 
 
 
1695
  // ============================================================
1696
  // ✅ جلب البوست الأصلي إذا كان هذا البوست مشاركة
1697
  // ============================================================
@@ -1721,33 +1766,10 @@ post: async (parent, { id }, context) => {
1721
  }
1722
  }
1723
 
1724
- // ✅ معالجة التعليقات (زي ما كنت بتعمل في REST)
1725
- if (post.comments && post.comments.length > 0) {
1726
- post.comments = post.comments.map(c => ({
1727
- ...c,
1728
- userId: c.userId || {
1729
- _id: c.userId || 'unknown',
1730
- username: 'Unknown',
1731
- profile: { nickname: 'Unknown', avatar: '/assets/img/default-avatar.png', jobTitle: '' }
1732
- },
1733
- likesCount: c.likes?.length || 0,
1734
- isLiked: c.likes?.some(l => l.userId?.toString() === context.user?.userId) || false,
1735
- replies: (c.replies || []).map(r => ({
1736
- ...r,
1737
- userId: r.userId || {
1738
- _id: r.userId || 'unknown',
1739
- username: 'Unknown',
1740
- profile: { nickname: 'Unknown', avatar: '/assets/img/default-avatar.png', jobTitle: '' }
1741
- },
1742
- likesCount: r.likes?.length || 0,
1743
- isLiked: r.likes?.some(l => l.userId?.toString() === context.user?.userId) || false
1744
- }))
1745
- }));
1746
- }
1747
-
1748
  // ✅ حساب القيم مباشرة
1749
  return {
1750
  ...post,
 
1751
  sharedFrom,
1752
  likesCount: post.likes?.length || 0,
1753
  commentsCount: post.comments?.length || 0,
 
1662
  },
1663
 
1664
  post: async (parent, { id }, context) => {
1665
+ // ✅ جلب البوست مع التعليقات والردود باستخدام aggregation
1666
+ const result = await Post.aggregate([
1667
+ { $match: { _id: new mongoose.Types.ObjectId(id) } },
1668
+ {
1669
+ $lookup: {
1670
+ from: 'users',
1671
+ localField: 'userId',
1672
+ foreignField: '_id',
1673
+ as: 'userId'
1674
+ }
1675
+ },
1676
+ { $unwind: { path: '$userId', preserveNullAndEmptyArrays: true } },
1677
+ {
1678
+ $lookup: {
1679
+ from: 'users',
1680
+ localField: 'comments.userId',
1681
+ foreignField: '_id',
1682
+ as: 'commentsUser'
1683
+ }
1684
+ },
1685
+ {
1686
+ $lookup: {
1687
+ from: 'users',
1688
+ localField: 'comments.replies.userId',
1689
+ foreignField: '_id',
1690
+ as: 'repliesUser'
1691
+ }
1692
+ }
1693
+ ]);
1694
 
1695
+ if (!result || result.length === 0) {
1696
+ throw new GraphQLError('Post not found');
1697
+ }
1698
 
1699
+ const post = result[0];
 
 
 
 
 
 
1700
 
1701
+ // ✅ معالجة التعليقات والردود
1702
+ if (post.comments && post.comments.length > 0) {
1703
+ post.comments = post.comments.map(c => {
1704
+ // ✅ جلب المستخدم من commentsUser
1705
+ const commentUser = post.commentsUser?.find(u => u._id.toString() === c.userId?.toString());
1706
+
1707
+ // ✅ معالجة الردود
1708
+ const replies = (c.replies || []).map(r => {
1709
+ const replyUser = post.repliesUser?.find(u => u._id.toString() === r.userId?.toString());
1710
+ return {
1711
+ ...r,
1712
+ userId: replyUser || {
1713
+ _id: r.userId || 'unknown',
1714
+ username: 'Unknown',
1715
+ profile: { nickname: 'Unknown', avatar: '/assets/img/default-avatar.png', jobTitle: '' }
1716
+ },
1717
+ likesCount: r.likes?.length || 0,
1718
+ isLiked: r.likes?.some(l => l.userId?.toString() === context.user?.userId) || false
1719
+ };
1720
+ });
1721
+
1722
+ return {
1723
+ ...c,
1724
+ userId: commentUser || {
1725
+ _id: c.userId || 'unknown',
1726
+ username: 'Unknown',
1727
+ profile: { nickname: 'Unknown', avatar: '/assets/img/default-avatar.png', jobTitle: '' }
1728
+ },
1729
+ likesCount: c.likes?.length || 0,
1730
+ isLiked: c.likes?.some(l => l.userId?.toString() === context.user?.userId) || false,
1731
+ replies: replies
1732
+ };
1733
+ });
1734
  }
1735
 
1736
+ // ✅ حذف الحقول المؤقتة
1737
+ delete post.commentsUser;
1738
+ delete post.repliesUser;
1739
+
1740
  // ============================================================
1741
  // ✅ جلب البوست الأصلي إذا كان هذا البوست مشاركة
1742
  // ============================================================
 
1766
  }
1767
  }
1768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1769
  // ✅ حساب القيم مباشرة
1770
  return {
1771
  ...post,
1772
+ userId: post.userId || { _id: post.userId, username: 'Unknown', profile: {} },
1773
  sharedFrom,
1774
  likesCount: post.likes?.length || 0,
1775
  commentsCount: post.comments?.length || 0,