chat implemented
need testing
This commit is contained in:
1 parent
c90f8cb1fa
commit
9cbdc35903
40 files changed
+7068
-10
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
import { ensureUserSynced } from "@/lib/sync-user";
|
||||
import { getDatabase } from "@/lib/database";
|
||||
import { isChatError, listThreadsForUser } from "@/lib/chat";
|
||||
import log from "@/lib/logger";
|
||||
|
||||
interface Params {
|
||||
params: Promise<{ id: string }>;
|
||||
}
|
||||
|
||||
export async function GET(_request: Request, { params }: Params) {
|
||||
try {
|
||||
const { userId } = await auth();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { id: threadId } = await params;
|
||||
const db = await getDatabase();
|
||||
const currentUser = await ensureUserSynced(userId, db);
|
||||
if (!currentUser) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const threads = await listThreadsForUser({
|
||||
id: currentUser.id,
|
||||
role: currentUser.role,
|
||||
gymId: currentUser.gymId,
|
||||
});
|
||||
|
||||
const thread = threads.find((item) => item.id === threadId);
|
||||
if (!thread) {
|
||||
return NextResponse.json({ error: "Thread not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ thread });
|
||||
} catch (error) {
|
||||
if (isChatError(error)) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message },
|
||||
{ status: error.status },
|
||||
);
|
||||
}
|
||||
|
||||
log.error("Failed to fetch chat thread", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user