import { NextRequest, NextResponse } from "next/server"; import { createContactApi } from "@/lib/http-server"; import type { CmpnInfo } from "@/types/cmpn"; import type { DataVerification } from "@kri/common"; import type { RouteContext } from "@/types/common"; import { errorToResponse } from "@kri/common/http"; export const GET = async (req: NextRequest, context: RouteContext) => { try { const { slug = [] } = await context.params; const [action] = slug; const contactApi = await createContactApi(); if (action === "check-cmpn-id") { const cmpnId = req.nextUrl.searchParams.get("cmpnId") ?? ""; if (!cmpnId) { return NextResponse.json( { message: "cmpnId 파라미터가 없습니다." }, { status: 400 } ); } const result = await contactApi.get("/cmpn/check-cmpn-id", { query: { cmpnId }, }); return NextResponse.json(result, { status: 200 }); } if (slug.length === 1) { const result = await contactApi.get(`/cmpn/${encodeURIComponent(action)}`); return NextResponse.json(result, { status: 200 }); } if (!action) { const result = await contactApi.get("/cmpn"); return NextResponse.json(result, { status: 200 }); } return NextResponse.json({ message: "Not found" }, { status: 404 }); } catch (error: unknown) { return errorToResponse(error); } }; export const PUT = async (req: NextRequest, context: RouteContext) => { try { const { slug = [] } = await context.params; const [action] = slug ?? []; const contactApi = await createContactApi(); if (!action) { const body = await req.json().catch(() => null); if (!body) { return NextResponse.json( { message: "요청 바디가 없습니다." }, { status: 400 } ); } await contactApi.put("/cmpn", body); return new NextResponse(null, { status: 204 }); } return NextResponse.json({ message: "Not found" }, { status: 404 }); } catch (error: unknown) { return errorToResponse(error); } }; export const PATCH = async (req: NextRequest, context: RouteContext) => { try { const { slug = [] } = await context.params; const [action] = slug ?? []; const contactApi = await createContactApi(); if (!action) { const body = await req.json().catch(() => null); if (!body) { return NextResponse.json( { message: "요청 바디가 없습니다." }, { status: 400 } ); } await contactApi.patch("/cmpn", body); return new NextResponse(null, { status: 204 }); } return NextResponse.json({ message: "Not found" }, { status: 404 }); } catch (error: unknown) { return errorToResponse(error); } };