import { apiJson } from "@/lib/api"; import { prisma } from "@/lib/prisma"; // Latest 50 radio shouts with their author's username/look resolved. Mirrors the // query behind the public /radio/shouts page (radio_shouts ordered by created_at // desc, then joined to users by user_id). export const dynamic = "force-dynamic"; export async function GET(_req: Request) { try { const shouts = await prisma.radioShouts.findMany({ orderBy: { createdAt: "desc" }, take: 50, }); // Resolve author usernames/looks. radio_shouts.user_id is an UnsignedBigInt // while users.id is an Int, so narrow to Number for the lookup. const authorIds = Array.from(new Set(shouts.map((s) => Number(s.userId)))); const authors = authorIds.length ? await prisma.user.findMany({ where: { id: { in: authorIds } }, select: { id: true, username: true, look: true }, }) : []; const authorById = new Map(authors.map((a) => [a.id, a])); const data = shouts.map((s) => { const author = authorById.get(Number(s.userId)); return { id: s.id, userId: s.userId, username: author?.username ?? null, look: author?.look ?? null, message: s.message, createdAt: s.createdAt, }; }); return apiJson({ shouts: data }); } catch { // DB unavailable — serve an empty list rather than a 500. return apiJson({ shouts: [] }, { status: 200 }); } }