Merge pull request '261001_01_ENH: 템플릿 저장 오류 수정' (#3) from develop into master
Reviewed-on: #3
This commit is contained in:
@@ -2,13 +2,13 @@ import { NextRequest, NextResponse } from "next/server";
|
|||||||
import { errorToResponse } from "@kri/common/http";
|
import { errorToResponse } from "@kri/common/http";
|
||||||
import { createNotiHubApi } from "@/lib/http-server";
|
import { createNotiHubApi } from "@/lib/http-server";
|
||||||
import type { RouteContext } from "@/types/common";
|
import type { RouteContext } from "@/types/common";
|
||||||
import type {
|
import {
|
||||||
BulkSendResponse,
|
BulkSendResponse,
|
||||||
EmailDestInfo,
|
EmailDestInfo, SaveTemplateRequest,
|
||||||
SendEmailBulkRequest,
|
SendEmailBulkRequest,
|
||||||
SendSmsBulkRequest,
|
SendSmsBulkRequest,
|
||||||
SmsDestInfo,
|
SmsDestInfo,
|
||||||
Template,
|
Template, TMPLAT_PURPOSE,
|
||||||
} from "@/types/notification";
|
} from "@/types/notification";
|
||||||
|
|
||||||
const apiPath = (slug: string[]) => `${slug.length ? `/${slug.map(encodeURIComponent).join("/")}` : ""}`;
|
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) {
|
export async function PATCH(request: NextRequest, context: RouteContext) {
|
||||||
try {
|
try {
|
||||||
const { slug = [] } = await context.params;
|
const { slug = [] } = await context.params;
|
||||||
const notiHubApi = await createNotiHubApi();
|
const [action] = slug;
|
||||||
const contentType = request.headers.get("content-type") ?? "";
|
|
||||||
|
if (action !== "template") {
|
||||||
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
if (contentType.includes("multipart/form-data")) {
|
|
||||||
const query = new URLSearchParams();
|
const query = new URLSearchParams();
|
||||||
request.nextUrl.searchParams.getAll("deleteFileNames").forEach((name) => query.append("deleteFileNames", name));
|
request.nextUrl.searchParams.getAll("deleteFileNames").forEach((name) => query.append("deleteFileNames", name));
|
||||||
const path = query.size ? `${apiPath(slug)}?${query}` : apiPath(slug);
|
const path = query.size ? `${apiPath(slug)}?${query}` : apiPath(slug);
|
||||||
return toResponse(await notiHubApi.patchForm<unknown>(path, await request.formData()));
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = await request.json().catch(() => undefined);
|
return toResponse(await notiHubApi.patchForm<unknown>(path, backendForm));
|
||||||
return toResponse(await notiHubApi.patch<unknown>(apiPath(slug), body));
|
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return errorToResponse(error);
|
return errorToResponse(error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user