116 lines
4.3 KiB
TypeScript
116 lines
4.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { RouteContext } from "@/types/common";
|
|
import { createContactApiWithApiKey } from "@/lib/http-server";
|
|
import { errorToResponse } from "@kri/common/http";
|
|
import { ListSearchExternal } from "@/types/list";
|
|
|
|
const getApiKey = (req: NextRequest) => req.headers.get("x-api-key");
|
|
|
|
// backend가 제공하는 메소드/경로 그대로 사용 (path variable)
|
|
export const GET = async (req: NextRequest, context: RouteContext) => {
|
|
try {
|
|
const apiKey = getApiKey(req);
|
|
if (!apiKey) {
|
|
return NextResponse.json({ message: "X-API-Key는 필수입니다." }, { status: 401 });
|
|
}
|
|
|
|
const { slug = [] } = await context.params;
|
|
const [action, prjctCd, sid] = slug;
|
|
const contactApi = await createContactApiWithApiKey(apiKey);
|
|
|
|
if (action === "header" && prjctCd) {
|
|
const result = await contactApi.get<string[]>(`/list-external/header/${encodeURIComponent(prjctCd)}`);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
if (action === "search" && prjctCd && sid) {
|
|
const result = await contactApi.get<ListSearchExternal>(
|
|
`/list-external/search/${encodeURIComponent(prjctCd)}/${encodeURIComponent(sid)}`,
|
|
);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|
|
|
|
// backend가 제공하는 메소드/경로 그대로 사용 (path variable)
|
|
export const PATCH = async (req: NextRequest, context: RouteContext) => {
|
|
try {
|
|
const apiKey = getApiKey(req);
|
|
if (!apiKey) {
|
|
return NextResponse.json({ message: "X-API-Key는 필수입니다." }, { status: 401 });
|
|
}
|
|
|
|
const { slug = [] } = await context.params;
|
|
const [action, prjctCd, sid] = slug;
|
|
|
|
if (action === "update" && prjctCd && sid) {
|
|
const body = await req.json().catch(() => null);
|
|
const contactApi = await createContactApiWithApiKey(apiKey);
|
|
const result = await contactApi.patch<number>(
|
|
`/list-external/update/${encodeURIComponent(prjctCd)}/${encodeURIComponent(sid)}`,
|
|
body,
|
|
);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|
|
|
|
// 외부 연동 호환용: 항상 POST, path variable을 모두 url 쿼리 파람으로 받아 backend path variable로 전달
|
|
export const POST = async (req: NextRequest, context: RouteContext) => {
|
|
try {
|
|
const apiKey = getApiKey(req);
|
|
if (!apiKey) {
|
|
return NextResponse.json({ message: "X-API-Key는 필수입니다." }, { status: 401 });
|
|
}
|
|
|
|
const { slug = [] } = await context.params;
|
|
const [action] = slug;
|
|
const { searchParams } = req.nextUrl;
|
|
const prjctCd = searchParams.get("prjctCd");
|
|
const sid = searchParams.get("sid");
|
|
const contactApi = await createContactApiWithApiKey(apiKey);
|
|
|
|
if (action === "header") {
|
|
if (!prjctCd) {
|
|
return NextResponse.json({ message: "prjctCd는 필수입니다." }, { status: 400 });
|
|
}
|
|
const result = await contactApi.get<string[]>(`/list-external/header/${encodeURIComponent(prjctCd)}`);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
if (action === "search") {
|
|
if (!prjctCd || !sid) {
|
|
return NextResponse.json({ message: "prjctCd, sid는 필수입니다." }, { status: 400 });
|
|
}
|
|
const result = await contactApi.get<ListSearchExternal>(
|
|
`/list-external/search/${encodeURIComponent(prjctCd)}/${encodeURIComponent(sid)}`,
|
|
);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
if (action === "update") {
|
|
if (!prjctCd || !sid) {
|
|
return NextResponse.json({ message: "prjctCd, sid는 필수입니다." }, { status: 400 });
|
|
}
|
|
const body = await req.json().catch(() => null);
|
|
const result = await contactApi.patch<number>(
|
|
`/list-external/update/${encodeURIComponent(prjctCd)}/${encodeURIComponent(sid)}`,
|
|
body,
|
|
);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|