261001_01_ENH: 템플릿 저장 오류 수정

This commit is contained in:
2026-10-01 09:19:43 +09:00
parent b932da4b57
commit e7711d8479

View File

@@ -2,13 +2,13 @@ import { NextRequest, NextResponse } from "next/server";
import { errorToResponse } from "@kri/common/http";
import { createNotiHubApi } from "@/lib/http-server";
import type { RouteContext } from "@/types/common";
import type {
import {
BulkSendResponse,
EmailDestInfo,
EmailDestInfo, SaveTemplateRequest,
SendEmailBulkRequest,
SendSmsBulkRequest,
SmsDestInfo,
Template,
Template, TMPLAT_PURPOSE,
} from "@/types/notification";
const apiPath = (slug: string[]) => `${slug.length ? `/${slug.map(encodeURIComponent).join("/")}` : ""}`;
@@ -140,18 +140,50 @@ export async function PUT(request: NextRequest, context: RouteContext) {
export async function PATCH(request: NextRequest, context: RouteContext) {
try {
const { slug = [] } = await context.params;
const notiHubApi = await createNotiHubApi();
const contentType = request.headers.get("content-type") ?? "";
const [action] = slug;
if (contentType.includes("multipart/form-data")) {
const query = new URLSearchParams();
request.nextUrl.searchParams.getAll("deleteFileNames").forEach((name) => query.append("deleteFileNames", name));
const path = query.size ? `${apiPath(slug)}?${query}` : apiPath(slug);
return toResponse(await notiHubApi.patchForm<unknown>(path, await request.formData()));
if (action !== "template") {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
const body = await request.json().catch(() => undefined);
return toResponse(await notiHubApi.patch<unknown>(apiPath(slug), body));
const formData = await request.formData();
const templatePart = formData.get("template");
const body = templatePart instanceof Blob
? JSON.parse(await templatePart.text()) as SaveTemplateRequest
: null;
if (!body || !body.tmplatIdx) {
return NextResponse.json({ message: "Template request body is invalid." }, { status: 400 });
}
const notiHubApi = await createNotiHubApi();
const current = await notiHubApi.get<Template>(`/template/${body.tmplatIdx}`);
if (!current) {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
const next: Template = {
...current,
tmplatNm: body.tmplatNm,
tmplatTitle: body.tmplatTitle,
tmplatBody: body.tmplatBody,
};
const backendForm = new FormData();
backendForm.append(
"template",
new Blob([JSON.stringify(next)], {type: "application/json"})
);
for (const [key, value] of formData.entries()) {
if (key === "template") continue;
backendForm.append(key, value);
}
const query = new URLSearchParams();
request.nextUrl.searchParams.getAll("deleteFileNames").forEach((name) => query.append("deleteFileNames", name));
const path = query.size ? `${apiPath(slug)}?${query}` : apiPath(slug);
return toResponse(await notiHubApi.patchForm<unknown>(path, backendForm));
} catch (error: unknown) {
return errorToResponse(error);
}