import {NextRequest, NextResponse} from "next/server"; import {RouteContext} from "@/types/common"; import {errorToResponse} from "@/lib/http-util"; import {createBackApi} from "@/lib/server-api"; import {ExcelUploadResult} from "@/types/common"; import {AssignUserCount, MonitoringApplyUser, PerformanceApplyCount, PerformanceAssign} from "@/types/gwcf"; export const GET = async ( req: NextRequest, context: RouteContext ) => { try { void req; const {slug = []} = await context.params; const [action, subAction, performanceId] = slug; const backApi = await createBackApi(); if (action === "monitoring" && subAction === "apply-users" && performanceId === "count" && slug.length === 3) { const result = await backApi.get("/assign/monitoring/apply-users/count"); return NextResponse.json(result, { status: 200 }); } if (action === "monitoring" && subAction === "apply-users" && performanceId && slug.length === 3) { const result = await backApi.get(`/assign/monitoring/apply-users/${encodeURIComponent(performanceId)}`); return NextResponse.json(result, { status: 200 }); } if (action === "users" && subAction === "count" && slug.length === 2) { const result = await backApi.get("/assign/users/count"); return NextResponse.json(result, { status: 200 }); } return NextResponse.json({ message: "Not found" }, { status: 404 }); } catch (error: unknown) { return errorToResponse(error); } }; export const POST = async ( req: NextRequest, context: RouteContext ) => { try { const {slug = []} = await context.params; const [action, second, third] = slug; const backApi = await createBackApi(); if (action === "monitoring" && slug.length === 1) { const body = await req.json(); const result = await backApi.post("/assign/monitoring", body); return NextResponse.json(result, { status: 200 }); } if (action === "confirm" && second && third && slug.length === 3) { const result = await backApi.post( `/assign/confirm/${encodeURIComponent(second)}/${encodeURIComponent(third)}` ); return NextResponse.json(result, { status: 200 }); } if (action === "reject" && second && third && slug.length === 3) { const result = await backApi.post( `/assign/reject/${encodeURIComponent(second)}/${encodeURIComponent(third)}` ); return NextResponse.json(result, { status: 200 }); } if(action === "written" && second === "excel-template"){ const result = await backApi.post( "/assign/written/excel/template", undefined, { expectBinary: true } ); return new NextResponse(result, { status: 200 }); } if (action === "written" && second === "excel-upload" && slug.length === 2) { const formData = await req.formData(); const file = formData.get("file") as File | null; if (!file) { return NextResponse.json({ message: "file field is required." }, { status: 400 }); } const backendForm = new FormData(); backendForm.append("file", file, file.name); const result = await backApi.postForm( "/assign/written/excel/upload", backendForm ); return NextResponse.json(result, { status: 200 }); } return NextResponse.json({ message: "Not found" }, { status: 404 }); } catch (error: unknown) { return errorToResponse(error); } }; export const PATCH = async ( req: NextRequest, context: RouteContext ) => { try { void req; const {slug = []} = await context.params; const [action, performanceId, userId] = slug; const backApi = await createBackApi(); if ((action === "confirm" || action === "cancel") && performanceId && userId && slug.length === 3) { await backApi.patch( `/assign/${action}/${encodeURIComponent(performanceId)}/${encodeURIComponent(userId)}` ); return new NextResponse(null, { status: 204 }); } return NextResponse.json({ message: "Not found" }, { status: 404 }); } catch (error: unknown) { return errorToResponse(error); } };