72 lines
2.3 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] =
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]),
]);
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: (profile as any).followingCount ?? 0,
postsCount: (profile as any).postsCount ?? 0,
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,
})),
};
});