141 lines
4.7 KiB
TypeScript
141 lines
4.7 KiB
TypeScript
import {NextRequest, NextResponse} from "next/server";
|
|
import {RouteContext} from "@/types/common";
|
|
import {createContactApi} from "@/lib/http-server";
|
|
import {errorToResponse} from "@kri/common/http";
|
|
import type { KkoTemplateDetail, KkoTemplateListResponse, Template, TemplateSummary } from "@kri/common/noti-hub";
|
|
|
|
// 외부 알림톡 템플릿 목록·상세 조회. JSON 본문을 contact-back에 그대로 전달한다.
|
|
export const POST = async (req: NextRequest, context: RouteContext) => {
|
|
try{
|
|
const {slug = []} = await context.params;
|
|
const [channel, action] = slug;
|
|
if (channel !== "kko" || slug.length !== 2 || (action !== "list" && action !== "detail")) {
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
}
|
|
|
|
const body = await req.json().catch(() => null);
|
|
if (!body || typeof body !== "object") {
|
|
return NextResponse.json({ message: "요청 바디가 없습니다." }, { status: 400 });
|
|
}
|
|
|
|
const contactApi = await createContactApi();
|
|
const result = action === "list"
|
|
? await contactApi.post<KkoTemplateListResponse>(`/message-template/kko/list`, body)
|
|
: await contactApi.post<KkoTemplateDetail>(`/message-template/kko/detail`, body);
|
|
return NextResponse.json(result, { status: 200 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
}
|
|
|
|
export const GET = async (req: NextRequest, context: RouteContext) => {
|
|
try{
|
|
const { searchParams } = new URL(req.url);
|
|
const {slug = []} = await context.params;
|
|
const [action] = slug;
|
|
const contactApi = await createContactApi();
|
|
|
|
if(slug.length === 2 && slug[1] === "attachment"){
|
|
const fileName = searchParams.get("fileName");
|
|
if (!fileName) {
|
|
return NextResponse.json({ message: "fileName은 필수입니다." }, { status: 400 });
|
|
}
|
|
|
|
const result = await contactApi.get<ArrayBuffer>(
|
|
`/message-template/${slug[0]}/attachment?fileName=${encodeURIComponent(fileName)}`,
|
|
{ expectBinary: true },
|
|
);
|
|
return new NextResponse(result, { status: 200, headers: { "Content-Type": "application/octet-stream" } });
|
|
}
|
|
|
|
if(slug.length === 1){
|
|
const result = await contactApi.get<Template>(
|
|
`/message-template/${slug[0]}`,
|
|
);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
if(action === "list" && slug.length === 3){
|
|
const result = await contactApi.get<TemplateSummary[]>(
|
|
`/message-template/list/${slug[1]}/${slug[2]}`,
|
|
);
|
|
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 === "body-image" && slug.length === 2){
|
|
const itrntPrjctCd = slug[1];
|
|
|
|
const formData = await req.formData();
|
|
const file = formData.get("file") as File | null;
|
|
|
|
if (!file) {
|
|
return NextResponse.json(
|
|
{ message: "file 필드가 없습니다." },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const result = await contactApi.putForm<string>(
|
|
`/message-template/body-image/${itrntPrjctCd}`,
|
|
formData
|
|
);
|
|
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
if(!action){
|
|
const formData = await req.formData().catch(() => null);
|
|
if (!formData) {
|
|
return NextResponse.json(
|
|
{ message: "요청 바디가 없습니다." },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const result = await contactApi.putForm<Template>(`/message-template`, formData);
|
|
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{
|
|
const {slug = []} = await context.params;
|
|
const [action] = slug;
|
|
const contactApi = await createContactApi();
|
|
|
|
if(!action){
|
|
const formData = await req.formData().catch(() => null);
|
|
if (!formData) {
|
|
return NextResponse.json(
|
|
{ message: "요청 바디가 없습니다." },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const result = await contactApi.request<Template>(`/message-template`, { method: "PATCH", form: formData });
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
}
|