Mark-Lasfar commited on
Commit
af4a262
·
1 Parent(s): d606181

Add GraphQL

Browse files
Files changed (1) hide show
  1. graphql/resolvers.js +23 -1
graphql/resolvers.js CHANGED
@@ -1348,6 +1348,23 @@ const Query = {
1348
  .lean(),
1349
  Post.countDocuments(query)
1350
  ]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1351
 
1352
  // ✅ إرجاع كل الحقول مع الحسابات
1353
  return {
@@ -1359,7 +1376,12 @@ const Query = {
1359
  sharesCount: (post.shares || []).length,
1360
  savesCount: (post.saves || []).length,
1361
  isLiked: post.likes?.some(l => l.userId?.toString() === context.user.userId) || false,
1362
- isSaved: post.saves?.some(s => s.userId?.toString() === context.user.userId) || false
 
 
 
 
 
1363
  })),
1364
  pagination: {
1365
  page,
 
1348
  .lean(),
1349
  Post.countDocuments(query)
1350
  ]);
1351
+
1352
+ // ✅ جلب الـ hasStory لكل مستخدم
1353
+ const userIds = posts.map(p => p.userId._id);
1354
+ const usersWithStories = await Story.aggregate([
1355
+ {
1356
+ $match: {
1357
+ userId: { $in: userIds },
1358
+ expiresAt: { $gt: new Date() }
1359
+ }
1360
+ },
1361
+ { $group: { _id: '$userId', count: { $sum: 1 } } }
1362
+ ]);
1363
+
1364
+ const userHasStoryMap = {};
1365
+ usersWithStories.forEach(u => {
1366
+ userHasStoryMap[u._id.toString()] = true;
1367
+ });
1368
 
1369
  // ✅ إرجاع كل الحقول مع الحسابات
1370
  return {
 
1376
  sharesCount: (post.shares || []).length,
1377
  savesCount: (post.saves || []).length,
1378
  isLiked: post.likes?.some(l => l.userId?.toString() === context.user.userId) || false,
1379
+ isSaved: post.saves?.some(s => s.userId?.toString() === context.user.userId) || false,
1380
+ userId: {
1381
+ ...post.userId,
1382
+ hasStory: userHasStoryMap[post.userId._id.toString()] || false
1383
+ }
1384
+
1385
  })),
1386
  pagination: {
1387
  page,