Rebranded for Thunderbird Portal (THBD): numbering prefix EL->TH, all "ELMI"/"ЭЛМИ" UI strings genericized, ELMI's real contract templates and company legal/bank details removed (not applicable to this company), fresh VAPID keypair and encryption key, fresh single-migration schema history. Same architecture as elmi-portal: Web Push notifications, unread-chat app badge, cookie-session auth. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gh2UXUQUVBWroWEnn1FLFG
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
export const runtime = "nodejs";
|
|
|
|
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { requireSession } from "@/lib/auth/require";
|
|
import { listConversationsForUser, getOrCreateDirectConversation, createGroupConversation } from "@/lib/chat/service";
|
|
|
|
export async function GET() {
|
|
const { session, response } = await requireSession();
|
|
if (!session) return response;
|
|
return NextResponse.json(await listConversationsForUser(session.user.id));
|
|
}
|
|
|
|
const schema = z.union([
|
|
z.object({ peerId: z.string().min(1) }),
|
|
z.object({ name: z.string().min(1), participantIds: z.array(z.string().min(1)).min(1) }),
|
|
]);
|
|
|
|
export async function POST(request: Request) {
|
|
const { session, response } = await requireSession();
|
|
if (!session) return response;
|
|
|
|
const parsed = schema.safeParse(await request.json().catch(() => null));
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: "Некорректные данные" }, { status: 400 });
|
|
}
|
|
|
|
if ("peerId" in parsed.data) {
|
|
const conv = await getOrCreateDirectConversation(session.user.id, parsed.data.peerId);
|
|
return NextResponse.json(conv, { status: 201 });
|
|
}
|
|
|
|
const conv = await createGroupConversation(session.user.id, parsed.data.participantIds, parsed.data.name);
|
|
return NextResponse.json(conv, { status: 201 });
|
|
}
|