Mark-Lasfar commited on
Commit ·
b071c9f
1
Parent(s): 50e1c5e
Add GraphQL
Browse files- graphql/resolvers.js +268 -126
graphql/resolvers.js
CHANGED
|
@@ -1184,70 +1184,144 @@ const Query = {
|
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
-
|
| 1188 |
-
// 🔹 PROFILE PAGE RESOLVERS
|
| 1189 |
-
// ============================================
|
| 1190 |
|
| 1191 |
// ===== 1. profilePage (مجمعة) =====
|
| 1192 |
-
|
| 1193 |
const userId = context.user?.userId;
|
| 1194 |
|
| 1195 |
// ✅ 1. جلب بيانات البروفايل
|
| 1196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1197 |
if (!profileResult) throw new GraphQLError('User not found');
|
| 1198 |
|
| 1199 |
const profileUserId = profileResult._id.toString();
|
| 1200 |
const isOwner = userId === profileUserId;
|
| 1201 |
|
| 1202 |
// ✅ 2. جلب الإحصائيات
|
| 1203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1204 |
|
| 1205 |
// ✅ 3. جلب التقييمات
|
| 1206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1207 |
|
| 1208 |
// ✅ 4. جلب التفاعلات (Recent Activity)
|
| 1209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1210 |
|
| 1211 |
// ✅ 5. جلب طلبات التوظيف (للمالك فقط)
|
| 1212 |
-
let jobApplications = {
|
|
|
|
|
|
|
|
|
|
| 1213 |
if (isOwner) {
|
| 1214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1215 |
}
|
| 1216 |
|
| 1217 |
// ✅ 6. جلب Contribution Graph
|
| 1218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1219 |
|
| 1220 |
// ✅ 7. جلب الإشعارات (للمالك فقط)
|
| 1221 |
-
let notifications = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1222 |
if (isOwner) {
|
| 1223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1224 |
}
|
| 1225 |
|
| 1226 |
// ✅ 8. جلب التحليلات (للمالك فقط)
|
| 1227 |
let analytics = null;
|
| 1228 |
if (isOwner) {
|
| 1229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1230 |
}
|
| 1231 |
|
| 1232 |
// ✅ 9. جلب حالة الـ AI
|
| 1233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1234 |
|
| 1235 |
// ✅ 10. جلب حالة المتابعة (للمستخدمين المسجلين فقط)
|
| 1236 |
let followStatus = null;
|
| 1237 |
if (userId && !isOwner) {
|
| 1238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1239 |
}
|
| 1240 |
|
| 1241 |
// ✅ 11. جلب "شاهد أيضاً"
|
| 1242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1243 |
|
| 1244 |
// ✅ 12. جلب اقتراحات الصفحات
|
| 1245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1246 |
|
| 1247 |
// ✅ 13. جلب عدد الإشعارات غير المقروءة (للمالك فقط)
|
| 1248 |
let unreadCount = 0;
|
| 1249 |
if (isOwner) {
|
| 1250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1251 |
}
|
| 1252 |
|
| 1253 |
// ✅ 14. جلب إعدادات المتجر (للمالك فقط)
|
|
@@ -1256,12 +1330,23 @@ const Query = {
|
|
| 1256 |
try {
|
| 1257 |
storeSettings = await Query.storeSettings(parent, {}, context);
|
| 1258 |
} catch (e) {
|
| 1259 |
-
|
| 1260 |
}
|
| 1261 |
}
|
| 1262 |
|
| 1263 |
// ✅ 15. جلب حالة الاشتراك
|
| 1264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1265 |
|
| 1266 |
return {
|
| 1267 |
profile: profileResult,
|
|
@@ -1280,7 +1365,7 @@ const Query = {
|
|
| 1280 |
storeSettings,
|
| 1281 |
subscriptionStatus
|
| 1282 |
};
|
| 1283 |
-
|
| 1284 |
|
| 1285 |
// ===== 2. contributionGraph (جديد) =====
|
| 1286 |
contributionGraph: async (parent, { userId, year }) => {
|
|
@@ -7439,130 +7524,160 @@ const Mutation = {
|
|
| 7439 |
|
| 7440 |
// ✅ جلب التفاعلات (Recent Activity)
|
| 7441 |
async function getProfileInteractions(userId, page = 1, limit = 4) {
|
| 7442 |
-
|
| 7443 |
|
| 7444 |
-
const
|
| 7445 |
-
.populate('projectId', 'title image')
|
| 7446 |
-
.sort({ timestamp: -1 })
|
| 7447 |
-
.skip(skip)
|
| 7448 |
-
.limit(limit)
|
| 7449 |
-
.lean();
|
| 7450 |
|
| 7451 |
-
|
| 7452 |
-
|
| 7453 |
-
|
| 7454 |
-
|
| 7455 |
-
|
| 7456 |
-
|
| 7457 |
-
|
| 7458 |
-
|
| 7459 |
-
|
| 7460 |
-
|
| 7461 |
-
|
| 7462 |
-
|
| 7463 |
-
|
| 7464 |
-
|
| 7465 |
-
|
| 7466 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7467 |
}
|
| 7468 |
|
| 7469 |
// ✅ جلب Contribution Graph
|
| 7470 |
async function getContributionGraph(userId, year) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7471 |
const targetYear = year || new Date().getFullYear();
|
| 7472 |
const startDate = new Date(targetYear, 0, 1);
|
| 7473 |
const endDate = new Date(targetYear, 11, 31);
|
| 7474 |
|
| 7475 |
-
|
| 7476 |
-
|
| 7477 |
-
|
| 7478 |
-
.
|
| 7479 |
-
|
| 7480 |
-
|
| 7481 |
-
.
|
| 7482 |
-
|
| 7483 |
-
|
| 7484 |
-
|
| 7485 |
-
|
| 7486 |
-
|
| 7487 |
-
|
| 7488 |
-
|
| 7489 |
-
|
| 7490 |
-
|
| 7491 |
-
|
| 7492 |
-
|
| 7493 |
-
|
| 7494 |
-
|
| 7495 |
-
|
| 7496 |
-
.
|
| 7497 |
-
|
| 7498 |
-
|
| 7499 |
-
|
| 7500 |
-
|
| 7501 |
-
|
| 7502 |
-
|
| 7503 |
-
|
| 7504 |
-
const
|
| 7505 |
-
|
| 7506 |
-
activitiesMap.set(dateStr, { count: 0, activities: [] });
|
| 7507 |
-
}
|
| 7508 |
-
const entry = activitiesMap.get(dateStr);
|
| 7509 |
-
entry.count++;
|
| 7510 |
-
entry.activities.push({ type, id, ...extra });
|
| 7511 |
-
};
|
| 7512 |
-
|
| 7513 |
-
posts.forEach(p => addActivity(p.createdAt, 'post', p._id));
|
| 7514 |
-
comments.forEach(c => addActivity(c.timestamp, 'comment', c._id, { projectId: c.projectId }));
|
| 7515 |
-
likes.forEach(l => addActivity(l.createdAt, 'like', l.postId, { postId: l.postId }));
|
| 7516 |
-
shares.forEach(s => addActivity(s.createdAt, 'share', s.postId, { postId: s.postId }));
|
| 7517 |
-
jobApplications.forEach(j => addActivity(j.appliedAt, 'job_application', j._id, { jobId: j.jobId }));
|
| 7518 |
-
|
| 7519 |
-
// بناء الأسبوعيات
|
| 7520 |
-
const weeks = [];
|
| 7521 |
-
let currentDate = new Date(startDate);
|
| 7522 |
-
currentDate.setDate(currentDate.getDate() - currentDate.getDay());
|
| 7523 |
-
|
| 7524 |
-
while (currentDate <= endDate) {
|
| 7525 |
-
const week = [];
|
| 7526 |
-
for (let i = 0; i < 7; i++) {
|
| 7527 |
-
const date = new Date(currentDate);
|
| 7528 |
const dateStr = date.toISOString().split('T')[0];
|
| 7529 |
-
|
| 7530 |
-
|
| 7531 |
-
|
| 7532 |
-
|
| 7533 |
-
|
| 7534 |
-
|
| 7535 |
-
|
| 7536 |
-
|
| 7537 |
-
|
| 7538 |
-
|
| 7539 |
-
|
| 7540 |
-
|
| 7541 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7542 |
}
|
| 7543 |
-
|
| 7544 |
-
|
| 7545 |
-
|
| 7546 |
-
|
| 7547 |
-
|
| 7548 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7549 |
}
|
| 7550 |
-
|
| 7551 |
-
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
| 7552 |
-
const weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
| 7553 |
-
|
| 7554 |
-
return { total, weeks, year: targetYear, months, weekDays };
|
| 7555 |
}
|
| 7556 |
|
| 7557 |
// ✅ جلب "شاهد أيضاً"
|
| 7558 |
async function getAlsoViewed(userId) {
|
|
|
|
|
|
|
| 7559 |
try {
|
| 7560 |
const views = await ProfileView.find({ targetUserId: userId })
|
| 7561 |
.sort({ viewedAt: -1 })
|
| 7562 |
.limit(10)
|
| 7563 |
.lean();
|
| 7564 |
|
| 7565 |
-
|
|
|
|
|
|
|
| 7566 |
if (viewerIds.length === 0) return [];
|
| 7567 |
|
| 7568 |
const users = await User.find({ _id: { $in: viewerIds } })
|
|
@@ -7573,9 +7688,10 @@ const Mutation = {
|
|
| 7573 |
_id: u._id,
|
| 7574 |
username: u.username,
|
| 7575 |
profile: u.profile || {},
|
| 7576 |
-
followersCount: 0
|
| 7577 |
}));
|
| 7578 |
} catch (error) {
|
|
|
|
| 7579 |
return [];
|
| 7580 |
}
|
| 7581 |
}
|
|
@@ -7588,21 +7704,35 @@ const Mutation = {
|
|
| 7588 |
.limit(limit)
|
| 7589 |
.lean();
|
| 7590 |
|
|
|
|
|
|
|
| 7591 |
return pages.map(p => ({
|
| 7592 |
_id: p._id,
|
| 7593 |
-
name: p.title || p.slug,
|
| 7594 |
logo: p.thumbnail || null,
|
| 7595 |
followersCount: p.followersCount || 0,
|
| 7596 |
isFollowing: false,
|
| 7597 |
-
url: `/page/${p.slug}`
|
| 7598 |
}));
|
| 7599 |
} catch (error) {
|
|
|
|
| 7600 |
return [];
|
| 7601 |
}
|
| 7602 |
}
|
| 7603 |
|
| 7604 |
// ✅ جلب التحليلات (للمالك)
|
| 7605 |
async function getProfileAnalytics(userId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7606 |
try {
|
| 7607 |
const analytics = await ProfileAnalytics.findOne({ userId });
|
| 7608 |
|
|
@@ -7623,6 +7753,7 @@ const Mutation = {
|
|
| 7623 |
lastUpdated: analytics?.updatedAt || new Date()
|
| 7624 |
};
|
| 7625 |
} catch (error) {
|
|
|
|
| 7626 |
return {
|
| 7627 |
views: 0,
|
| 7628 |
uniqueViews: 0,
|
|
@@ -7636,6 +7767,16 @@ const Mutation = {
|
|
| 7636 |
|
| 7637 |
// ✅ جلب حالة الاشتراك
|
| 7638 |
async function getSubscriptionStatus(userId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7639 |
try {
|
| 7640 |
const subscription = await UserSubscription.findOne({
|
| 7641 |
userId,
|
|
@@ -7664,6 +7805,7 @@ const Mutation = {
|
|
| 7664 |
daysLeft: 0
|
| 7665 |
};
|
| 7666 |
} catch (error) {
|
|
|
|
| 7667 |
return {
|
| 7668 |
hasActiveSubscription: false,
|
| 7669 |
canCreateStore: false,
|
|
|
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
+
// ============================================
|
| 1188 |
+
// 🔹 PROFILE PAGE RESOLVERS
|
| 1189 |
+
// ============================================
|
| 1190 |
|
| 1191 |
// ===== 1. profilePage (مجمعة) =====
|
| 1192 |
+
profilePage: async (parent, { nickname }, context) => {
|
| 1193 |
const userId = context.user?.userId;
|
| 1194 |
|
| 1195 |
// ✅ 1. جلب بيانات البروفايل
|
| 1196 |
+
let profileResult = null;
|
| 1197 |
+
try {
|
| 1198 |
+
profileResult = await Query.profile(parent, { nickname }, context);
|
| 1199 |
+
} catch (e) {
|
| 1200 |
+
console.error('Error loading profile:', e);
|
| 1201 |
+
throw new GraphQLError('User not found');
|
| 1202 |
+
}
|
| 1203 |
+
|
| 1204 |
if (!profileResult) throw new GraphQLError('User not found');
|
| 1205 |
|
| 1206 |
const profileUserId = profileResult._id.toString();
|
| 1207 |
const isOwner = userId === profileUserId;
|
| 1208 |
|
| 1209 |
// ✅ 2. جلب الإحصائيات
|
| 1210 |
+
let stats = { posts: 0, projects: 0, jobs: 0, followers: 0, following: 0, applications: 0, savedPosts: 0, savedJobs: 0, totalLikes: 0 };
|
| 1211 |
+
try {
|
| 1212 |
+
stats = await Query.userStats(parent, { userId: profileUserId });
|
| 1213 |
+
} catch (e) {
|
| 1214 |
+
console.error('Error loading stats:', e);
|
| 1215 |
+
}
|
| 1216 |
|
| 1217 |
// ✅ 3. جلب التقييمات
|
| 1218 |
+
let ratings = {
|
| 1219 |
+
data: [],
|
| 1220 |
+
pagination: { page: 1, limit: 1, total: 0, pages: 0, hasNext: false, hasPrev: false },
|
| 1221 |
+
averageRating: 0,
|
| 1222 |
+
totalRatings: 0
|
| 1223 |
+
};
|
| 1224 |
+
try {
|
| 1225 |
+
ratings = await Query.userRatings(parent, { userId: profileUserId, page: 1, limit: 1 });
|
| 1226 |
+
} catch (e) {
|
| 1227 |
+
console.error('Error loading ratings:', e);
|
| 1228 |
+
}
|
| 1229 |
|
| 1230 |
// ✅ 4. جلب التفاعلات (Recent Activity)
|
| 1231 |
+
let interactions = [];
|
| 1232 |
+
try {
|
| 1233 |
+
interactions = await getProfileInteractions(profileUserId, 1, 4);
|
| 1234 |
+
} catch (e) {
|
| 1235 |
+
console.error('Error loading interactions:', e);
|
| 1236 |
+
}
|
| 1237 |
|
| 1238 |
// ✅ 5. جلب طلبات التوظيف (للمالك فقط)
|
| 1239 |
+
let jobApplications = {
|
| 1240 |
+
data: [],
|
| 1241 |
+
pagination: { page: 1, limit: 5, total: 0, pages: 0, hasNext: false, hasPrev: false }
|
| 1242 |
+
};
|
| 1243 |
if (isOwner) {
|
| 1244 |
+
try {
|
| 1245 |
+
jobApplications = await Query.myJobApplications(parent, { page: 1, limit: 5 }, context);
|
| 1246 |
+
} catch (e) {
|
| 1247 |
+
console.error('Error loading job applications:', e);
|
| 1248 |
+
}
|
| 1249 |
}
|
| 1250 |
|
| 1251 |
// ✅ 6. جلب Contribution Graph
|
| 1252 |
+
let contributionGraph = { total: 0, weeks: [], year: 2026, months: [], weekDays: [] };
|
| 1253 |
+
try {
|
| 1254 |
+
contributionGraph = await getContributionGraph(profileUserId, 2026);
|
| 1255 |
+
} catch (e) {
|
| 1256 |
+
console.error('Error loading contribution graph:', e);
|
| 1257 |
+
}
|
| 1258 |
|
| 1259 |
// ✅ 7. جلب الإشعارات (للمالك فقط)
|
| 1260 |
+
let notifications = {
|
| 1261 |
+
data: [],
|
| 1262 |
+
pagination: { page: 1, limit: 10, total: 0, pages: 0, hasNext: false, hasPrev: false },
|
| 1263 |
+
unreadCount: 0
|
| 1264 |
+
};
|
| 1265 |
if (isOwner) {
|
| 1266 |
+
try {
|
| 1267 |
+
notifications = await Query.notifications(parent, { page: 1, limit: 10 }, context);
|
| 1268 |
+
} catch (e) {
|
| 1269 |
+
console.error('Error loading notifications:', e);
|
| 1270 |
+
}
|
| 1271 |
}
|
| 1272 |
|
| 1273 |
// ✅ 8. جلب التحليلات (للمالك فقط)
|
| 1274 |
let analytics = null;
|
| 1275 |
if (isOwner) {
|
| 1276 |
+
try {
|
| 1277 |
+
analytics = await getProfileAnalytics(profileUserId);
|
| 1278 |
+
} catch (e) {
|
| 1279 |
+
console.error('Error loading analytics:', e);
|
| 1280 |
+
}
|
| 1281 |
}
|
| 1282 |
|
| 1283 |
// ✅ 9. جلب حالة الـ AI
|
| 1284 |
+
let aiStatus = { enabled: false };
|
| 1285 |
+
try {
|
| 1286 |
+
aiStatus = await Query.aiStatus(parent, { nickname });
|
| 1287 |
+
} catch (e) {
|
| 1288 |
+
console.error('Error loading AI status:', e);
|
| 1289 |
+
}
|
| 1290 |
|
| 1291 |
// ✅ 10. جلب حالة المتابعة (للمستخدمين المسجلين فقط)
|
| 1292 |
let followStatus = null;
|
| 1293 |
if (userId && !isOwner) {
|
| 1294 |
+
try {
|
| 1295 |
+
followStatus = await Query.followStatus(parent, { userId: profileUserId }, context);
|
| 1296 |
+
} catch (e) {
|
| 1297 |
+
console.error('Error loading follow status:', e);
|
| 1298 |
+
}
|
| 1299 |
}
|
| 1300 |
|
| 1301 |
// ✅ 11. جلب "شاهد أيضاً"
|
| 1302 |
+
let alsoViewed = [];
|
| 1303 |
+
try {
|
| 1304 |
+
alsoViewed = await getAlsoViewed(profileUserId);
|
| 1305 |
+
} catch (e) {
|
| 1306 |
+
console.error('Error loading also viewed:', e);
|
| 1307 |
+
}
|
| 1308 |
|
| 1309 |
// ✅ 12. جلب اقتراحات الصفحات
|
| 1310 |
+
let pageSuggestions = [];
|
| 1311 |
+
try {
|
| 1312 |
+
pageSuggestions = await getPageSuggestions(2);
|
| 1313 |
+
} catch (e) {
|
| 1314 |
+
console.error('Error loading page suggestions:', e);
|
| 1315 |
+
}
|
| 1316 |
|
| 1317 |
// ✅ 13. جلب عدد الإشعارات غير المقروءة (للمالك فقط)
|
| 1318 |
let unreadCount = 0;
|
| 1319 |
if (isOwner) {
|
| 1320 |
+
try {
|
| 1321 |
+
unreadCount = await Query.unreadNotificationsCount(parent, {}, context);
|
| 1322 |
+
} catch (e) {
|
| 1323 |
+
console.error('Error loading unread count:', e);
|
| 1324 |
+
}
|
| 1325 |
}
|
| 1326 |
|
| 1327 |
// ✅ 14. جلب إعدادات المتجر (للمالك فقط)
|
|
|
|
| 1330 |
try {
|
| 1331 |
storeSettings = await Query.storeSettings(parent, {}, context);
|
| 1332 |
} catch (e) {
|
| 1333 |
+
console.error('Error loading store settings:', e);
|
| 1334 |
}
|
| 1335 |
}
|
| 1336 |
|
| 1337 |
// ✅ 15. جلب حالة الاشتراك
|
| 1338 |
+
let subscriptionStatus = {
|
| 1339 |
+
hasActiveSubscription: false,
|
| 1340 |
+
canCreateStore: false,
|
| 1341 |
+
message: 'No active subscription',
|
| 1342 |
+
planName: null,
|
| 1343 |
+
daysLeft: 0
|
| 1344 |
+
};
|
| 1345 |
+
try {
|
| 1346 |
+
subscriptionStatus = await getSubscriptionStatus(profileUserId);
|
| 1347 |
+
} catch (e) {
|
| 1348 |
+
console.error('Error loading subscription status:', e);
|
| 1349 |
+
}
|
| 1350 |
|
| 1351 |
return {
|
| 1352 |
profile: profileResult,
|
|
|
|
| 1365 |
storeSettings,
|
| 1366 |
subscriptionStatus
|
| 1367 |
};
|
| 1368 |
+
},
|
| 1369 |
|
| 1370 |
// ===== 2. contributionGraph (جديد) =====
|
| 1371 |
contributionGraph: async (parent, { userId, year }) => {
|
|
|
|
| 7524 |
|
| 7525 |
// ✅ جلب التفاعلات (Recent Activity)
|
| 7526 |
async function getProfileInteractions(userId, page = 1, limit = 4) {
|
| 7527 |
+
if (!userId) return [];
|
| 7528 |
|
| 7529 |
+
const skip = (page - 1) * limit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7530 |
|
| 7531 |
+
try {
|
| 7532 |
+
const comments = await Comment.find({ userId })
|
| 7533 |
+
.populate('projectId', 'title image userId')
|
| 7534 |
+
.sort({ timestamp: -1 })
|
| 7535 |
+
.skip(skip)
|
| 7536 |
+
.limit(limit)
|
| 7537 |
+
.lean();
|
| 7538 |
+
|
| 7539 |
+
// ✅ إذا مفيش تعليقات، أرجع array فاضي
|
| 7540 |
+
if (!comments || comments.length === 0) return [];
|
| 7541 |
+
|
| 7542 |
+
return comments.map(c => {
|
| 7543 |
+
// ✅ التأكد من وجود projectId
|
| 7544 |
+
const project = c.projectId || {};
|
| 7545 |
+
const projectId = project._id || c.projectId || null;
|
| 7546 |
+
|
| 7547 |
+
return {
|
| 7548 |
+
_id: c._id || null,
|
| 7549 |
+
commentId: c._id || null,
|
| 7550 |
+
projectId: projectId,
|
| 7551 |
+
projectTitle: project.title || 'Unknown Project',
|
| 7552 |
+
projectNickname: project.userId?.profile?.nickname || null,
|
| 7553 |
+
projectImage: project.image || null,
|
| 7554 |
+
rating: c.rating || 0,
|
| 7555 |
+
text: c.text || '',
|
| 7556 |
+
timestamp: c.timestamp || c.createdAt || new Date(),
|
| 7557 |
+
replies: [],
|
| 7558 |
+
likes: c.likes || [],
|
| 7559 |
+
isEdited: c.isEdited || false,
|
| 7560 |
+
parentCommentId: c.parentCommentId || null,
|
| 7561 |
+
visibility: c.visibility || 'public'
|
| 7562 |
+
};
|
| 7563 |
+
});
|
| 7564 |
+
} catch (error) {
|
| 7565 |
+
console.error('Error in getProfileInteractions:', error);
|
| 7566 |
+
return [];
|
| 7567 |
+
}
|
| 7568 |
}
|
| 7569 |
|
| 7570 |
// ✅ جلب Contribution Graph
|
| 7571 |
async function getContributionGraph(userId, year) {
|
| 7572 |
+
if (!userId) {
|
| 7573 |
+
return { total: 0, weeks: [], year: year || new Date().getFullYear(), months: [], weekDays: [] };
|
| 7574 |
+
}
|
| 7575 |
+
|
| 7576 |
const targetYear = year || new Date().getFullYear();
|
| 7577 |
const startDate = new Date(targetYear, 0, 1);
|
| 7578 |
const endDate = new Date(targetYear, 11, 31);
|
| 7579 |
|
| 7580 |
+
try {
|
| 7581 |
+
// جلب الأنشطة
|
| 7582 |
+
const [posts, comments, likes, shares, jobApplications] = await Promise.all([
|
| 7583 |
+
Post.find({ userId, createdAt: { $gte: startDate, $lte: endDate } })
|
| 7584 |
+
.select('_id createdAt')
|
| 7585 |
+
.lean(),
|
| 7586 |
+
Comment.find({ userId, timestamp: { $gte: startDate, $lte: endDate } })
|
| 7587 |
+
.select('_id projectId timestamp')
|
| 7588 |
+
.lean(),
|
| 7589 |
+
Post.aggregate([
|
| 7590 |
+
{ $match: { 'likes.userId': userId, createdAt: { $gte: startDate, $lte: endDate } } },
|
| 7591 |
+
{ $unwind: '$likes' },
|
| 7592 |
+
{ $match: { 'likes.userId': userId } },
|
| 7593 |
+
{ $project: { postId: '$_id', createdAt: '$likes.likedAt' } }
|
| 7594 |
+
]),
|
| 7595 |
+
Post.aggregate([
|
| 7596 |
+
{ $match: { 'shares.userId': userId, createdAt: { $gte: startDate, $lte: endDate } } },
|
| 7597 |
+
{ $unwind: '$shares' },
|
| 7598 |
+
{ $match: { 'shares.userId': userId } },
|
| 7599 |
+
{ $project: { postId: '$_id', createdAt: '$shares.sharedAt' } }
|
| 7600 |
+
]),
|
| 7601 |
+
JobApplication.find({ applicantId: userId, appliedAt: { $gte: startDate, $lte: endDate } })
|
| 7602 |
+
.select('_id jobId appliedAt')
|
| 7603 |
+
.lean()
|
| 7604 |
+
]);
|
| 7605 |
+
|
| 7606 |
+
// تجميع الأنشطة
|
| 7607 |
+
const activitiesMap = new Map();
|
| 7608 |
+
|
| 7609 |
+
const addActivity = (date, type, id, extra = {}) => {
|
| 7610 |
+
if (!date) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7611 |
const dateStr = date.toISOString().split('T')[0];
|
| 7612 |
+
if (!activitiesMap.has(dateStr)) {
|
| 7613 |
+
activitiesMap.set(dateStr, { count: 0, activities: [] });
|
| 7614 |
+
}
|
| 7615 |
+
const entry = activitiesMap.get(dateStr);
|
| 7616 |
+
entry.count++;
|
| 7617 |
+
entry.activities.push({ type, id, ...extra });
|
| 7618 |
+
};
|
| 7619 |
+
|
| 7620 |
+
posts.forEach(p => addActivity(p.createdAt, 'post', p._id));
|
| 7621 |
+
comments.forEach(c => addActivity(c.timestamp, 'comment', c._id, { projectId: c.projectId }));
|
| 7622 |
+
likes.forEach(l => addActivity(l.createdAt, 'like', l.postId, { postId: l.postId }));
|
| 7623 |
+
shares.forEach(s => addActivity(s.createdAt, 'share', s.postId, { postId: s.postId }));
|
| 7624 |
+
jobApplications.forEach(j => addActivity(j.appliedAt, 'job_application', j._id, { jobId: j.jobId }));
|
| 7625 |
+
|
| 7626 |
+
// بناء الأسبوعيات
|
| 7627 |
+
const weeks = [];
|
| 7628 |
+
let currentDate = new Date(startDate);
|
| 7629 |
+
currentDate.setDate(currentDate.getDate() - currentDate.getDay());
|
| 7630 |
+
|
| 7631 |
+
while (currentDate <= endDate) {
|
| 7632 |
+
const week = [];
|
| 7633 |
+
for (let i = 0; i < 7; i++) {
|
| 7634 |
+
const date = new Date(currentDate);
|
| 7635 |
+
const dateStr = date.toISOString().split('T')[0];
|
| 7636 |
+
const data = activitiesMap.get(dateStr) || { count: 0, activities: [] };
|
| 7637 |
+
let level = 0;
|
| 7638 |
+
if (data.count > 0) level = 1;
|
| 7639 |
+
if (data.count > 3) level = 2;
|
| 7640 |
+
if (data.count > 7) level = 3;
|
| 7641 |
+
if (data.count > 15) level = 4;
|
| 7642 |
+
week.push({
|
| 7643 |
+
date: dateStr,
|
| 7644 |
+
count: data.count,
|
| 7645 |
+
level,
|
| 7646 |
+
activities: data.activities || []
|
| 7647 |
+
});
|
| 7648 |
+
currentDate.setDate(currentDate.getDate() + 1);
|
| 7649 |
+
}
|
| 7650 |
+
weeks.push(week);
|
| 7651 |
}
|
| 7652 |
+
|
| 7653 |
+
let total = 0;
|
| 7654 |
+
for (const [_, data] of activitiesMap) {
|
| 7655 |
+
total += data.count;
|
| 7656 |
+
}
|
| 7657 |
+
|
| 7658 |
+
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
| 7659 |
+
const weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
| 7660 |
+
|
| 7661 |
+
return { total, weeks, year: targetYear, months, weekDays };
|
| 7662 |
+
} catch (error) {
|
| 7663 |
+
console.error('Error in getContributionGraph:', error);
|
| 7664 |
+
return { total: 0, weeks: [], year: targetYear, months: [], weekDays: [] };
|
| 7665 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7666 |
}
|
| 7667 |
|
| 7668 |
// ✅ جلب "شاهد أيضاً"
|
| 7669 |
async function getAlsoViewed(userId) {
|
| 7670 |
+
if (!userId) return [];
|
| 7671 |
+
|
| 7672 |
try {
|
| 7673 |
const views = await ProfileView.find({ targetUserId: userId })
|
| 7674 |
.sort({ viewedAt: -1 })
|
| 7675 |
.limit(10)
|
| 7676 |
.lean();
|
| 7677 |
|
| 7678 |
+
if (!views || views.length === 0) return [];
|
| 7679 |
+
|
| 7680 |
+
const viewerIds = views.map(v => v.viewerId).filter(id => id);
|
| 7681 |
if (viewerIds.length === 0) return [];
|
| 7682 |
|
| 7683 |
const users = await User.find({ _id: { $in: viewerIds } })
|
|
|
|
| 7688 |
_id: u._id,
|
| 7689 |
username: u.username,
|
| 7690 |
profile: u.profile || {},
|
| 7691 |
+
followersCount: u.followersCount || 0
|
| 7692 |
}));
|
| 7693 |
} catch (error) {
|
| 7694 |
+
console.error('Error in getAlsoViewed:', error);
|
| 7695 |
return [];
|
| 7696 |
}
|
| 7697 |
}
|
|
|
|
| 7704 |
.limit(limit)
|
| 7705 |
.lean();
|
| 7706 |
|
| 7707 |
+
if (!pages || pages.length === 0) return [];
|
| 7708 |
+
|
| 7709 |
return pages.map(p => ({
|
| 7710 |
_id: p._id,
|
| 7711 |
+
name: p.title || p.slug || 'Unknown',
|
| 7712 |
logo: p.thumbnail || null,
|
| 7713 |
followersCount: p.followersCount || 0,
|
| 7714 |
isFollowing: false,
|
| 7715 |
+
url: `/page/${p.slug || p._id}`
|
| 7716 |
}));
|
| 7717 |
} catch (error) {
|
| 7718 |
+
console.error('Error in getPageSuggestions:', error);
|
| 7719 |
return [];
|
| 7720 |
}
|
| 7721 |
}
|
| 7722 |
|
| 7723 |
// ✅ جلب التحليلات (للمالك)
|
| 7724 |
async function getProfileAnalytics(userId) {
|
| 7725 |
+
if (!userId) {
|
| 7726 |
+
return {
|
| 7727 |
+
views: 0,
|
| 7728 |
+
uniqueViews: 0,
|
| 7729 |
+
weeklyViews: 0,
|
| 7730 |
+
impressions: 0,
|
| 7731 |
+
searches: 0,
|
| 7732 |
+
lastUpdated: new Date()
|
| 7733 |
+
};
|
| 7734 |
+
}
|
| 7735 |
+
|
| 7736 |
try {
|
| 7737 |
const analytics = await ProfileAnalytics.findOne({ userId });
|
| 7738 |
|
|
|
|
| 7753 |
lastUpdated: analytics?.updatedAt || new Date()
|
| 7754 |
};
|
| 7755 |
} catch (error) {
|
| 7756 |
+
console.error('Error in getProfileAnalytics:', error);
|
| 7757 |
return {
|
| 7758 |
views: 0,
|
| 7759 |
uniqueViews: 0,
|
|
|
|
| 7767 |
|
| 7768 |
// ✅ جلب حالة الاشتراك
|
| 7769 |
async function getSubscriptionStatus(userId) {
|
| 7770 |
+
if (!userId) {
|
| 7771 |
+
return {
|
| 7772 |
+
hasActiveSubscription: false,
|
| 7773 |
+
canCreateStore: false,
|
| 7774 |
+
message: 'No user ID provided',
|
| 7775 |
+
planName: null,
|
| 7776 |
+
daysLeft: 0
|
| 7777 |
+
};
|
| 7778 |
+
}
|
| 7779 |
+
|
| 7780 |
try {
|
| 7781 |
const subscription = await UserSubscription.findOne({
|
| 7782 |
userId,
|
|
|
|
| 7805 |
daysLeft: 0
|
| 7806 |
};
|
| 7807 |
} catch (error) {
|
| 7808 |
+
console.error('Error in getSubscriptionStatus:', error);
|
| 7809 |
return {
|
| 7810 |
hasActiveSubscription: false,
|
| 7811 |
canCreateStore: false,
|