import { NextResponse } from "next/server"; import { atomicUpdateRoom } from "@/lib/store"; import { notify } from "@/lib/roomEvents"; import { apiHandler, ApiError } from "@/lib/api"; export const POST = apiHandler(async (req, { params }) => { const { id } = await params; const { userId, restaurantId } = await req.json(); if (!userId || restaurantId == null) { throw new ApiError("userId and restaurantId are required"); } const rid = String(restaurantId); const updated = await atomicUpdateRoom(id, (data) => { if (data.likes[rid]) { data.likes[rid] = data.likes[rid].filter((u) => u !== userId); if (data.likes[rid].length === 0) { delete data.likes[rid]; } } if (data.match === rid) { data.match = null; } const count = data.swipeCounts[userId] ?? 0; if (count > 0) { data.swipeCounts[userId] = count - 1; } return data; }); if (!updated) { return NextResponse.json( { error: "房间不存在或已过期" }, { status: 404 }, ); } notify(id); return NextResponse.json({ ok: true }); });