Mark-Lasfar commited on
Commit
f96a5a2
·
1 Parent(s): 965981a

Add GraphQL

Browse files
Files changed (1) hide show
  1. graphql/resolvers.js +321 -322
graphql/resolvers.js CHANGED
@@ -1684,335 +1684,334 @@ const Query = {
1684
 
1685
  // ===== Stories =====
1686
 
1687
- // ✅ storiesFeed - قصص المتابعين فقط
1688
- storiesFeed: async (parent, { limit = 20 }, context) => {
1689
- if (!context.user) throw new GraphQLError('Not authenticated');
1690
-
1691
- const following = await Follow.find({
1692
- followerId: context.user.userId,
1693
- status: 'accepted'
1694
- }).select('followingId');
1695
-
1696
- const followingIds = following.map(f => f.followingId);
1697
- followingIds.push(context.user.userId);
1698
-
1699
- const stories = await Story.find({
1700
- userId: { $in: followingIds },
1701
- expiresAt: { $gt: new Date() },
1702
- $or: [
1703
- { visibility: 'public' },
1704
- { visibility: 'followers' },
1705
- { visibility: 'only-me', userId: context.user.userId }
1706
- ]
1707
- })
1708
- .sort({ createdAt: -1 })
1709
- .limit(limit * 2)
1710
- .populate('userId') // ✅ بترجع كل بيانات المستخدم
1711
- .lean();
1712
-
1713
- const storiesByUser = {};
1714
- stories.forEach(story => {
1715
- const userId = story.userId._id.toString();
1716
- if (!storiesByUser[userId]) {
1717
- storiesByUser[userId] = {
1718
- user: story.userId,
1719
- stories: []
1720
- };
1721
- }
1722
- const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
1723
- storiesByUser[userId].stories.push({
1724
- ...story,
1725
- viewsCount: (story.views || []).length,
1726
- reactionsCount: (story.reactions || []).length,
1727
- hasViewed: hasViewed
1728
- });
1729
- });
1730
-
1731
- return Object.values(storiesByUser);
1732
- },
1733
-
1734
 
1735
- // ✅ storiesAll - كل القصص (للاكتشاف + المتابعين)
1736
- storiesAll: async (parent, { page = 1, limit = 20 }, context) => {
1737
- if (!context.user) throw new GraphQLError('Not authenticated');
1738
-
1739
- const skip = (page - 1) * limit;
1740
-
1741
- const following = await Follow.find({
1742
- followerId: context.user.userId,
1743
- status: 'accepted'
1744
- }).select('followingId');
1745
-
1746
- const followingIds = following.map(f => f.followingId.toString());
1747
-
1748
- const allStories = await Story.find({
1749
- expiresAt: { $gt: new Date() },
1750
- $or: [
1751
- { visibility: 'public' },
1752
- { userId: { $in: followingIds }, visibility: { $in: ['public', 'followers'] } },
1753
- { userId: context.user.userId }
1754
- ]
1755
- })
1756
- .sort({ createdAt: -1 })
1757
- .skip(skip)
1758
- .limit(limit)
1759
- .populate('userId') // ✅ بترجع كل بيانات المستخدم
1760
- .lean();
1761
-
1762
- const storiesMap = {};
1763
- const userIds = new Set();
1764
-
1765
- allStories.forEach(story => {
1766
- const userId = story.userId._id.toString();
1767
- userIds.add(userId);
1768
-
1769
- if (!storiesMap[userId]) {
1770
- storiesMap[userId] = {
1771
- user: story.userId,
1772
- stories: [],
1773
- isFollowing: followingIds.includes(userId),
1774
- isCurrentUser: userId === context.user.userId
1775
- };
1776
- }
1777
- const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
1778
- storiesMap[userId].stories.push({
1779
- ...story,
1780
- viewsCount: (story.views || []).length,
1781
- reactionsCount: (story.reactions || []).length,
1782
- hasViewed: hasViewed
1783
- });
1784
- });
1785
-
1786
- const sortedGroups = Object.values(storiesMap).sort((a, b) => {
1787
- if (a.isFollowing && !b.isFollowing) return -1;
1788
- if (!a.isFollowing && b.isFollowing) return 1;
1789
- if (a.isCurrentUser && !b.isCurrentUser) return -1;
1790
- if (!a.isCurrentUser && b.isCurrentUser) return 1;
1791
- return b.stories.length - a.stories.length;
1792
- });
1793
-
1794
- const paginatedData = sortedGroups.slice(0, limit);
1795
- const total = sortedGroups.length;
1796
- const totalStories = allStories.length;
1797
- const followingStories = allStories.filter(s =>
1798
- followingIds.includes(s.userId._id.toString())
1799
- ).length;
1800
-
1801
- return {
1802
- data: paginatedData,
1803
- pagination: {
1804
- page,
1805
- limit,
1806
- total,
1807
- pages: Math.ceil(total / limit),
1808
- hasNext: skip + limit < total,
1809
- hasPrev: page > 1
1810
- },
1811
- stats: {
1812
- totalStories,
1813
- myStories: allStories.filter(s => s.userId._id.toString() === context.user.userId).length,
1814
- followingStories,
1815
- discoverStories: totalStories - followingStories,
1816
- followingCount: followingIds.length
1817
- }
1818
- };
1819
- },
1820
 
1821
- // ✅ storiesDiscover - قصص جديدة للاكتشاف
1822
- storiesDiscover: async (parent, { limit = 10 }, context) => {
1823
- if (!context.user) throw new GraphQLError('Not authenticated');
1824
-
1825
- const following = await Follow.find({
1826
- followerId: context.user.userId,
1827
- status: 'accepted'
1828
- }).select('followingId');
1829
-
1830
- const followingIds = following.map(f => f.followingId);
1831
- followingIds.push(context.user.userId);
1832
-
1833
- const discoverStories = await Story.find({
1834
- userId: { $nin: followingIds },
1835
- visibility: 'public',
1836
- expiresAt: { $gt: new Date() }
1837
- })
1838
- .sort({ createdAt: -1 })
1839
- .limit(parseInt(limit))
1840
- .populate('userId') // ✅ بترجع كل بيانات المستخدم
1841
- .lean();
1842
-
1843
- const storiesByUser = {};
1844
- discoverStories.forEach(story => {
1845
- const userId = story.userId._id.toString();
1846
- if (!storiesByUser[userId]) {
1847
- storiesByUser[userId] = {
1848
- user: story.userId,
1849
- stories: [],
1850
- isFollowing: false
1851
- };
1852
- }
1853
- const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
1854
- storiesByUser[userId].stories.push({
1855
- ...story,
1856
- viewsCount: (story.views || []).length,
1857
- reactionsCount: (story.reactions || []).length,
1858
- hasViewed: hasViewed
1859
- });
1860
- });
1861
-
1862
- return Object.values(storiesByUser);
1863
- },
1864
 
1865
- // ✅ storyStats - إحصائيات القصص
1866
- storyStats: async (parent, args, context) => {
1867
- if (!context.user) throw new GraphQLError('Not authenticated');
1868
-
1869
- const following = await Follow.find({
1870
- followerId: context.user.userId,
1871
- status: 'accepted'
1872
- }).select('followingId');
1873
-
1874
- const followingIds = following.map(f => f.followingId);
1875
-
1876
- const [totalStories, myStories, followingStories, discoverStories] = await Promise.all([
1877
- Story.countDocuments({ expiresAt: { $gt: new Date() } }), // ✅ كل القصص
1878
- Story.countDocuments({ userId: context.user.userId, expiresAt: { $gt: new Date() } }),
1879
- Story.countDocuments({
1880
- userId: { $in: followingIds },
1881
- expiresAt: { $gt: new Date() },
1882
- visibility: { $in: ['public', 'followers'] }
1883
- }),
1884
- Story.countDocuments({
1885
- userId: { $nin: [...followingIds, context.user.userId] },
1886
- expiresAt: { $gt: new Date() },
1887
- visibility: 'public'
1888
- })
1889
- ]);
1890
-
1891
- return {
1892
- totalStories,
1893
- myStories,
1894
- followingStories,
1895
- discoverStories,
1896
- followingCount: followingIds.length
1897
- };
1898
- },
1899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1900
 
1901
- // ✅ userStories - قصص مستخدم معين
1902
- userStories: async (parent, { userId }, context) => {
1903
- if (!context.user) throw new GraphQLError('Not authenticated');
1904
-
1905
- const isOwner = userId === context.user.userId;
1906
- const isFollowing = await Follow.exists({
1907
- followerId: context.user.userId,
1908
- followingId: userId,
1909
- status: 'accepted'
1910
- });
1911
-
1912
- let visibilityQuery = { userId, expiresAt: { $gt: new Date() } };
1913
-
1914
- if (!isOwner) {
1915
- visibilityQuery.$or = [
1916
- { visibility: 'public' },
1917
- { visibility: 'followers' }
1918
- ];
1919
- if (!isFollowing) {
1920
- visibilityQuery.visibility = 'public';
1921
- }
1922
- }
1923
-
1924
- const stories = await Story.find(visibilityQuery)
1925
- .sort({ createdAt: -1 })
1926
- .populate('userId') // ✅ بترجع كل بيانات المستخدم
1927
- .lean();
1928
-
1929
- return stories.map(story => ({
1930
- ...story,
1931
- viewsCount: (story.views || []).length,
1932
- reactionsCount: (story.reactions || []).length,
1933
- hasViewed: story.views?.some(v => v.userId?.toString() === context.user.userId) || false,
1934
- userId: story.userId // ✅ تأكد من وجود userId كامل
1935
- }));
1936
- },
1937
- // ✅ storyViewers - جلب مشاهدين القصة (للمالك فقط)
1938
- storyViewers: async (parent, { storyId, page = 1, limit = 20 }, context) => {
1939
- if (!context.user) throw new GraphQLError('Not authenticated');
1940
-
1941
- const story = await Story.findById(storyId);
1942
- if (!story) throw new GraphQLError('Story not found');
1943
-
1944
- if (story.userId.toString() !== context.user.userId) {
1945
- throw new GraphQLError('You are not authorized to view viewers of this story');
1946
- }
1947
-
1948
- const skip = (page - 1) * limit;
1949
- const views = story.views || [];
1950
- const total = views.length;
1951
-
1952
- const viewerIds = views.map(v => v.userId);
1953
- const paginatedIds = viewerIds.slice(skip, skip + limit);
1954
-
1955
- const viewers = await User.find({
1956
- _id: { $in: paginatedIds }
1957
- }); // ✅ بدون select عشان يجيب كل البيانات
1958
-
1959
- const viewersWithTime = paginatedIds.map((id) => {
1960
- const user = viewers.find(u => u._id.toString() === id.toString());
1961
- const view = views.find(v => v.userId.toString() === id.toString());
1962
- return {
1963
- userId: user || { _id: id, username: 'Unknown', profile: {} },
1964
- viewedAt: view?.viewedAt || new Date()
1965
- };
1966
- });
1967
-
1968
- return {
1969
- data: viewersWithTime,
1970
- total,
1971
- pagination: {
1972
- page,
1973
- limit,
1974
- total,
1975
- pages: Math.ceil(total / limit),
1976
- hasNext: skip + limit < total,
1977
- hasPrev: page > 1
1978
- }
1979
- };
1980
- },
1981
 
1982
  // ✅ story - قصة واحدة
1983
- story: async (parent, { id }, context) => {
1984
- const story = await Story.findById(id)
1985
- .populate('userId') // ✅ بترجع كل بيانات المستخدم
1986
- .lean();
1987
-
1988
- if (!story) throw new GraphQLError('Story not found');
1989
-
1990
- if (story.expiresAt && new Date(story.expiresAt) < new Date()) {
1991
- throw new GraphQLError('Story has expired');
1992
- }
1993
-
1994
- const isOwner = context.user && context.user.userId === story.userId._id.toString();
1995
- const isFollowing = context.user ? await Follow.exists({
1996
- followerId: context.user.userId,
1997
- followingId: story.userId._id,
1998
- status: 'accepted'
1999
- }) : false;
2000
-
2001
- if (!isOwner && story.visibility === 'only-me') {
2002
- throw new GraphQLError('This story is private');
2003
- }
2004
- if (story.visibility === 'followers' && !isOwner && !isFollowing) {
2005
- throw new GraphQLError('This story is only visible to followers');
2006
- }
2007
-
2008
- return {
2009
- ...story,
2010
- viewsCount: (story.views || []).length,
2011
- reactionsCount: (story.reactions || []).length,
2012
- hasViewed: story.views?.some(v => v.userId?.toString() === context.user.userId) || false,
2013
- userId: story.userId // ✅ تأكد من وجود userId كامل
2014
- };
2015
- },
2016
  // ===== Jobs =====
2017
  jobs: async (parent, { page = 1, limit = 20, search, category, jobType, location }) => {
2018
  const skip = (page - 1) * limit;
 
1684
 
1685
  // ===== Stories =====
1686
 
1687
+ // ✅ storiesFeed - قصص المتابعين فقط
1688
+ storiesFeed: async (parent, { limit = 20 }, context) => {
1689
+ if (!context.user) throw new GraphQLError('Not authenticated');
1690
+
1691
+ const following = await Follow.find({
1692
+ followerId: context.user.userId,
1693
+ status: 'accepted'
1694
+ }).select('followingId');
1695
+
1696
+ const followingIds = following.map(f => f.followingId);
1697
+ followingIds.push(context.user.userId);
1698
+
1699
+ const stories = await Story.find({
1700
+ userId: { $in: followingIds },
1701
+ expiresAt: { $gt: new Date() },
1702
+ $or: [
1703
+ { visibility: 'public' },
1704
+ { visibility: 'followers' },
1705
+ { visibility: 'only-me', userId: context.user.userId }
1706
+ ]
1707
+ })
1708
+ .sort({ createdAt: -1 })
1709
+ .limit(limit * 2)
1710
+ .populate('userId')
1711
+ .lean();
1712
+
1713
+ const storiesByUser = {};
1714
+ stories.forEach(story => {
1715
+ const userId = story.userId._id.toString();
1716
+ if (!storiesByUser[userId]) {
1717
+ storiesByUser[userId] = {
1718
+ user: story.userId,
1719
+ stories: []
1720
+ };
1721
+ }
1722
+ const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
1723
+ storiesByUser[userId].stories.push({
1724
+ ...story,
1725
+ viewsCount: (story.views || []).length,
1726
+ reactionsCount: (story.reactions || []).length,
1727
+ hasViewed: hasViewed
1728
+ });
1729
+ });
1730
+
1731
+ return Object.values(storiesByUser);
1732
+ },
 
1733
 
1734
+ // ✅ storiesAll - كل القصص (للاكتشاف + المتابعين)
1735
+ storiesAll: async (parent, { page = 1, limit = 20 }, context) => {
1736
+ if (!context.user) throw new GraphQLError('Not authenticated');
1737
+
1738
+ const skip = (page - 1) * limit;
1739
+
1740
+ const following = await Follow.find({
1741
+ followerId: context.user.userId,
1742
+ status: 'accepted'
1743
+ }).select('followingId');
1744
+
1745
+ const followingIds = following.map(f => f.followingId.toString());
1746
+
1747
+ const allStories = await Story.find({
1748
+ expiresAt: { $gt: new Date() },
1749
+ $or: [
1750
+ { visibility: 'public' },
1751
+ { userId: { $in: followingIds }, visibility: { $in: ['public', 'followers'] } },
1752
+ { userId: context.user.userId }
1753
+ ]
1754
+ })
1755
+ .sort({ createdAt: -1 })
1756
+ .skip(skip)
1757
+ .limit(limit)
1758
+ .populate('userId')
1759
+ .lean();
1760
+
1761
+ const storiesMap = {};
1762
+ const userIds = new Set();
1763
+
1764
+ allStories.forEach(story => {
1765
+ const userId = story.userId._id.toString();
1766
+ userIds.add(userId);
1767
+
1768
+ if (!storiesMap[userId]) {
1769
+ storiesMap[userId] = {
1770
+ user: story.userId,
1771
+ stories: [],
1772
+ isFollowing: followingIds.includes(userId),
1773
+ isCurrentUser: userId === context.user.userId
1774
+ };
1775
+ }
1776
+ const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
1777
+ storiesMap[userId].stories.push({
1778
+ ...story,
1779
+ viewsCount: (story.views || []).length,
1780
+ reactionsCount: (story.reactions || []).length,
1781
+ hasViewed: hasViewed
1782
+ });
1783
+ });
1784
+
1785
+ const sortedGroups = Object.values(storiesMap).sort((a, b) => {
1786
+ if (a.isFollowing && !b.isFollowing) return -1;
1787
+ if (!a.isFollowing && b.isFollowing) return 1;
1788
+ if (a.isCurrentUser && !b.isCurrentUser) return -1;
1789
+ if (!a.isCurrentUser && b.isCurrentUser) return 1;
1790
+ return b.stories.length - a.stories.length;
1791
+ });
1792
+
1793
+ const paginatedData = sortedGroups.slice(0, limit);
1794
+ const total = sortedGroups.length;
1795
+ const totalStories = allStories.length;
1796
+ const followingStories = allStories.filter(s =>
1797
+ followingIds.includes(s.userId._id.toString())
1798
+ ).length;
1799
+
1800
+ return {
1801
+ data: paginatedData,
1802
+ pagination: {
1803
+ page,
1804
+ limit,
1805
+ total,
1806
+ pages: Math.ceil(total / limit),
1807
+ hasNext: skip + limit < total,
1808
+ hasPrev: page > 1
1809
+ },
1810
+ stats: {
1811
+ totalStories,
1812
+ myStories: allStories.filter(s => s.userId._id.toString() === context.user.userId).length,
1813
+ followingStories,
1814
+ discoverStories: totalStories - followingStories,
1815
+ followingCount: followingIds.length
1816
+ }
1817
+ };
1818
+ },
1819
 
1820
+ // ✅ storiesDiscover - قصص جديدة للاكتشاف
1821
+ storiesDiscover: async (parent, { limit = 10 }, context) => {
1822
+ if (!context.user) throw new GraphQLError('Not authenticated');
1823
+
1824
+ const following = await Follow.find({
1825
+ followerId: context.user.userId,
1826
+ status: 'accepted'
1827
+ }).select('followingId');
1828
+
1829
+ const followingIds = following.map(f => f.followingId);
1830
+ followingIds.push(context.user.userId);
1831
+
1832
+ const discoverStories = await Story.find({
1833
+ userId: { $nin: followingIds },
1834
+ visibility: 'public',
1835
+ expiresAt: { $gt: new Date() }
1836
+ })
1837
+ .sort({ createdAt: -1 })
1838
+ .limit(parseInt(limit))
1839
+ .populate('userId')
1840
+ .lean();
1841
+
1842
+ const storiesByUser = {};
1843
+ discoverStories.forEach(story => {
1844
+ const userId = story.userId._id.toString();
1845
+ if (!storiesByUser[userId]) {
1846
+ storiesByUser[userId] = {
1847
+ user: story.userId,
1848
+ stories: [],
1849
+ isFollowing: false
1850
+ };
1851
+ }
1852
+ const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
1853
+ storiesByUser[userId].stories.push({
1854
+ ...story,
1855
+ viewsCount: (story.views || []).length,
1856
+ reactionsCount: (story.reactions || []).length,
1857
+ hasViewed: hasViewed
1858
+ });
1859
+ });
1860
+
1861
+ return Object.values(storiesByUser);
1862
+ },
1863
 
1864
+ // ✅ storyStats - إحصائيات القصص
1865
+ storyStats: async (parent, args, context) => {
1866
+ if (!context.user) throw new GraphQLError('Not authenticated');
1867
+
1868
+ const following = await Follow.find({
1869
+ followerId: context.user.userId,
1870
+ status: 'accepted'
1871
+ }).select('followingId');
1872
+
1873
+ const followingIds = following.map(f => f.followingId);
1874
+
1875
+ const [totalStories, myStories, followingStories, discoverStories] = await Promise.all([
1876
+ Story.countDocuments({ expiresAt: { $gt: new Date() } }),
1877
+ Story.countDocuments({ userId: context.user.userId, expiresAt: { $gt: new Date() } }),
1878
+ Story.countDocuments({
1879
+ userId: { $in: followingIds },
1880
+ expiresAt: { $gt: new Date() },
1881
+ visibility: { $in: ['public', 'followers'] }
1882
+ }),
1883
+ Story.countDocuments({
1884
+ userId: { $nin: [...followingIds, context.user.userId] },
1885
+ expiresAt: { $gt: new Date() },
1886
+ visibility: 'public'
1887
+ })
1888
+ ]);
1889
+
1890
+ return {
1891
+ totalStories,
1892
+ myStories,
1893
+ followingStories,
1894
+ discoverStories,
1895
+ followingCount: followingIds.length
1896
+ };
1897
+ },
1898
 
1899
+ // ✅ userStories - قصص مستخدم معين
1900
+ userStories: async (parent, { userId }, context) => {
1901
+ if (!context.user) throw new GraphQLError('Not authenticated');
1902
+
1903
+ const isOwner = userId === context.user.userId;
1904
+ const isFollowing = await Follow.exists({
1905
+ followerId: context.user.userId,
1906
+ followingId: userId,
1907
+ status: 'accepted'
1908
+ });
1909
+
1910
+ let visibilityQuery = { userId, expiresAt: { $gt: new Date() } };
1911
+
1912
+ if (!isOwner) {
1913
+ visibilityQuery.$or = [
1914
+ { visibility: 'public' },
1915
+ { visibility: 'followers' }
1916
+ ];
1917
+ if (!isFollowing) {
1918
+ visibilityQuery.visibility = 'public';
1919
+ }
1920
+ }
1921
+
1922
+ const stories = await Story.find(visibilityQuery)
1923
+ .sort({ createdAt: -1 })
1924
+ .populate('userId')
1925
+ .lean();
1926
+
1927
+ return stories.map(story => ({
1928
+ ...story,
1929
+ viewsCount: (story.views || []).length,
1930
+ reactionsCount: (story.reactions || []).length,
1931
+ hasViewed: story.views?.some(v => v.userId?.toString() === context.user.userId) || false,
1932
+ userId: story.userId
1933
+ }));
1934
+ },
1935
 
1936
+ // ✅ storyViewers - جلب مشاهدين القصة (للمالك فقط)
1937
+ storyViewers: async (parent, { storyId, page = 1, limit = 20 }, context) => {
1938
+ if (!context.user) throw new GraphQLError('Not authenticated');
1939
+
1940
+ const story = await Story.findById(storyId);
1941
+ if (!story) throw new GraphQLError('Story not found');
1942
+
1943
+ if (story.userId.toString() !== context.user.userId) {
1944
+ throw new GraphQLError('You are not authorized to view viewers of this story');
1945
+ }
1946
+
1947
+ const skip = (page - 1) * limit;
1948
+ const views = story.views || [];
1949
+ const total = views.length;
1950
+
1951
+ const viewerIds = views.map(v => v.userId);
1952
+ const paginatedIds = viewerIds.slice(skip, skip + limit);
1953
+
1954
+ const viewers = await User.find({
1955
+ _id: { $in: paginatedIds }
1956
+ });
1957
+
1958
+ const viewersWithTime = paginatedIds.map((id) => {
1959
+ const user = viewers.find(u => u._id.toString() === id.toString());
1960
+ const view = views.find(v => v.userId.toString() === id.toString());
1961
+ return {
1962
+ userId: user || { _id: id, username: 'Unknown', profile: {} },
1963
+ viewedAt: view?.viewedAt || new Date()
1964
+ };
1965
+ });
1966
+
1967
+ return {
1968
+ data: viewersWithTime,
1969
+ total,
1970
+ pagination: {
1971
+ page,
1972
+ limit,
1973
+ total,
1974
+ pages: Math.ceil(total / limit),
1975
+ hasNext: skip + limit < total,
1976
+ hasPrev: page > 1
1977
+ }
1978
+ };
1979
+ },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1980
 
1981
  // ✅ story - قصة واحدة
1982
+ story: async (parent, { id }, context) => {
1983
+ const story = await Story.findById(id)
1984
+ .populate('userId')
1985
+ .lean();
1986
+
1987
+ if (!story) throw new GraphQLError('Story not found');
1988
+
1989
+ if (story.expiresAt && new Date(story.expiresAt) < new Date()) {
1990
+ throw new GraphQLError('Story has expired');
1991
+ }
1992
+
1993
+ const isOwner = context.user && context.user.userId === story.userId._id.toString();
1994
+ const isFollowing = context.user ? await Follow.exists({
1995
+ followerId: context.user.userId,
1996
+ followingId: story.userId._id,
1997
+ status: 'accepted'
1998
+ }) : false;
1999
+
2000
+ if (!isOwner && story.visibility === 'only-me') {
2001
+ throw new GraphQLError('This story is private');
2002
+ }
2003
+ if (story.visibility === 'followers' && !isOwner && !isFollowing) {
2004
+ throw new GraphQLError('This story is only visible to followers');
2005
+ }
2006
+
2007
+ return {
2008
+ ...story,
2009
+ viewsCount: (story.views || []).length,
2010
+ reactionsCount: (story.reactions || []).length,
2011
+ hasViewed: story.views?.some(v => v.userId?.toString() === context.user.userId) || false,
2012
+ userId: story.userId
2013
+ };
2014
+ },
2015
  // ===== Jobs =====
2016
  jobs: async (parent, { page = 1, limit = 20, search, category, jobType, location }) => {
2017
  const skip = (page - 1) * limit;