import { NextResponse } from "next/server"; import { atomicUpdateRoom } from "@/lib/store"; export async function POST( req: Request, { params }: { params: Promise<{ id: string }> }, ) { const { id } = await params; try { const { userId, restaurantId, action } = await req.json(); if (!userId || restaurantId == null || !action) { return NextResponse.json( { error: "userId, restaurantId, and action are required" }, { status: 400 }, ); } const rid = String(restaurantId); const updated = await atomicUpdateRoom(id, (data) => { if (action === "like") { if (!data.likes[rid]) { data.likes[rid] = []; } if (!data.likes[rid].includes(userId)) { data.likes[rid].push(userId); } if ( data.users.length > 1 && data.likes[rid].length === data.users.length ) { data.match = rid; } } data.swipeCounts[userId] = (data.swipeCounts[userId] ?? 0) + 1; return data; }); if (!updated) { return NextResponse.json( { error: "房间不存在或已过期" }, { status: 404 }, ); } return NextResponse.json({ match: updated.match, likeCount: updated.likes[rid]?.length ?? 0, }); } catch (e) { console.error("Failed to process swipe:", e); return NextResponse.json( { error: "操作失败" }, { status: 500 }, ); } }