fix: unify panic room code format and validate room join id
This commit is contained in:
@@ -74,11 +74,11 @@ describe("POST /api/room/[id]/join", () => {
|
||||
it("returns 404 when room not found", async () => {
|
||||
mockAtomicUpdate.mockResolvedValue(null);
|
||||
|
||||
const req = createRequest("/api/room/NONEXIST/join", {
|
||||
const req = createRequest("/api/room/ABC123/join", {
|
||||
method: "POST",
|
||||
body: { userId: "user-1" },
|
||||
});
|
||||
const ctx = createRouteContext({ id: "NONEXIST" });
|
||||
const ctx = createRouteContext({ id: "ABC123" });
|
||||
const res = await POST(req, ctx);
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
@@ -92,4 +92,14 @@ describe("POST /api/room/[id]/join", () => {
|
||||
const res = await POST(req, ctx);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it("returns 400 when room id format is invalid", async () => {
|
||||
const req = createRequest("/api/room/1234/join", {
|
||||
method: "POST",
|
||||
body: { userId: "user-1" },
|
||||
});
|
||||
const ctx = createRouteContext({ id: "1234" });
|
||||
const res = await POST(req, ctx);
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,14 +2,16 @@ import { NextResponse } from "next/server";
|
||||
import { atomicUpdateRoom } from "@/lib/roomRepository";
|
||||
import { notify } from "@/lib/roomEvents";
|
||||
import { apiHandler, ApiError, requireUserId } from "@/lib/api";
|
||||
import { validatePanicRoomId } from "@/lib/validation";
|
||||
|
||||
export const POST = apiHandler(async (req, { params }) => {
|
||||
const { id } = await params;
|
||||
const roomId = validatePanicRoomId(id);
|
||||
const { userId } = await req.json();
|
||||
|
||||
requireUserId(userId);
|
||||
|
||||
const updated = await atomicUpdateRoom(id, (data) => {
|
||||
const updated = await atomicUpdateRoom(roomId, (data) => {
|
||||
if (data.kickedUsers.includes(userId)) {
|
||||
throw new ApiError("你已被移出该房间", 403);
|
||||
}
|
||||
@@ -24,10 +26,10 @@ export const POST = apiHandler(async (req, { params }) => {
|
||||
|
||||
if (!updated) throw new ApiError("房间不存在或已过期", 404);
|
||||
|
||||
notify(id);
|
||||
notify(roomId);
|
||||
|
||||
return NextResponse.json({
|
||||
roomId: id,
|
||||
roomId,
|
||||
userCount: updated.users.length,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user