1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
Files
inke/apps/web/app/api/collaboration/room/route.ts
2023-11-10 14:59:47 +08:00

108 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { getServerSession } from "next-auth";
import { NextRequest, NextResponse } from "next/server";
import { authOptions } from "../../auth/[...nextauth]/route";
import { getUserByEmail } from "@/lib/db/user";
import { findCollaborationByRoomId } from "@/lib/db/collaboration";
// post页面获取详情时调用需要用户id查询是否已经加入加入才开启协作模式
export async function GET(
req: NextRequest,
{ params }: { params: Record<string, string | string | undefined[]> },
) {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
return NextResponse.json({
code: 401,
msg: "Unauthorized! Please login first",
data: null,
});
}
const user = await getUserByEmail(session.user.email);
if (!user) {
return NextResponse.json({
code: 403,
msg: "Not found user",
data: null,
});
}
const { searchParams } = new URL(req.url);
const id = searchParams.get("roomId");
if (!id) {
return NextResponse.json({
code: 403,
msg: "Empty roomId",
data: null,
});
}
// 查询用户是否已经加入了这个协作已经创建了这个room记录
const res = await findCollaborationByRoomId(id, user.id);
if (res) {
return NextResponse.json({
code: 200,
msg: "Successed!",
data: res,
});
}
return NextResponse.json({
code: 404,
msg: "You haven't joined this collaboration yet",
data: null,
});
} catch (error) {
return NextResponse.json({
code: 500,
msg: error,
data: null,
});
}
}
export async function POST(
req: NextRequest,
{ params }: { params: Record<string, string | string | undefined[]> },
) {
try {
const { roomId } = await req.json();
if (!roomId) {
return NextResponse.json({
code: 403,
msg: "Empty roomId",
data: null,
});
}
const res = await findCollaborationByRoomId(roomId);
if (res) {
return NextResponse.json({
code: 200,
msg: "Successed!",
data: res,
});
}
return NextResponse.json({
code: 404,
msg: "Not found",
data: null,
});
} catch (error) {
return NextResponse.json({
code: 500,
msg: error,
data: null,
});
}
}
// fix error: "DYNAMIC_SERVER_USAGE"
export const dynamic = "force-dynamic";