chahinebrini 5264dba257 fix(social): compute postsCount + followingCount live (were hardcoded 0)
Endpoint /api/social/profile/[userId] returned (profile as any).postsCount ?? 0
und (profile as any).followingCount ?? 0 — Profile-schema hat aber weder
postsCount noch followingCount columns. Daher zeigte UI immer 0 obwohl User
Posts hatte.

Fix: 2 zusätzliche COUNT-queries in Promise.all:
- usePrisma().communityPost.count({ userId, isModerated: false }) → postsCount
- usePrisma().userFollow.count({ followerId: userId }) → followingCount

followersCount bleibt unverändert (wird via trigger denormalisiert in profile-row).

Tests: backend/tests/social/profile-counts.test.ts — 4 Cases
  (posts>0, posts=0, following count, followers passthrough). 4/4 grün.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 21:40:42 +02:00

78 lines
2.4 KiB
TypeScript

import { getProfile } from "../../../db/profile";
import { getUserScore } from "../../../db/scores";
import { getFollowRelation } from "../../../db/social";
import { getUsersMeta } from "../../../utils/getUsersMeta";
import { usePrisma } from "../../../utils/prisma";
/** GET /api/social/profile/[userId] */
export default defineEventHandler(async (event) => {
const targetUserId = getRouterParam(event, "userId");
if (!targetUserId)
throw createError({ statusCode: 400, message: "userId fehlt" });
// Auth-User optional
let currentUserId: string | null = null;
try {
const u = await requireUser(event);
currentUserId = u.id;
} catch {}
const [profile, score, followRelation, recentPosts, metaMap, postsCount, followingCount] =
await Promise.all([
getProfile(targetUserId),
getUserScore(targetUserId),
currentUserId && currentUserId !== targetUserId
? getFollowRelation(currentUserId, targetUserId)
: Promise.resolve(null),
usePrisma().communityPost.findMany({
where: { userId: targetUserId, isModerated: false },
orderBy: { createdAt: "desc" },
take: 5,
select: {
id: true,
category: true,
content: true,
likesCount: true,
commentsCount: true,
createdAt: true,
},
}),
getUsersMeta([targetUserId]),
usePrisma().communityPost.count({
where: { userId: targetUserId, isModerated: false },
}),
usePrisma().userFollow.count({
where: { followerId: targetUserId },
}),
]);
if (!profile)
throw createError({ statusCode: 404, message: "Profil nicht gefunden" });
const meta = metaMap[targetUserId] ?? { nickname: null, avatar: null };
return {
id: profile.id,
username: profile.username,
nickname: meta.nickname ?? profile.username,
avatar: meta.avatar,
bio: (profile as any).bio ?? null,
followersCount: profile.followersCount ?? 0,
followingCount,
postsCount,
tier: score?.tier ?? "beginner",
totalPoints: score?.totalPoints ?? 0,
isFollowing: !!followRelation,
isSelf: currentUserId === targetUserId,
joinedAt: profile.createdAt,
recentPosts: recentPosts.map((p) => ({
id: p.id,
category: p.category,
content: p.content.slice(0, 120),
likesCount: p.likesCount ?? 0,
commentsCount: p.commentsCount ?? 0,
createdAt: p.createdAt,
})),
};
});