Files
gwcf-review-front/src/app/api/notification/[[...slug]]/route.ts

176 lines
5.4 KiB
TypeScript

import {NextRequest, NextResponse} from "next/server";
import {RouteContext} from "@/types/common";
import {errorToResponse} from "@/lib/http-util";
import {createNotiHubApi} from "@/lib/server-api";
import {
BulkSendResponse,
CreateTemplateRequest,
SaveTemplateRequest,
SendSmsBulkRequest,
Template,
} from "@/types/notification";
export const GET = async (
_req: NextRequest,
context: RouteContext
) => {
try {
const {slug = []} = await context.params;
const [action, second, third, fourth] = slug;
if (action !== "template") {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
const notiHubApi = await createNotiHubApi();
let path: string;
if (second === "list") {
if (slug.length !== 4 || !third || !fourth) {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
path = `/template/list/${encodeURIComponent(third)}/${encodeURIComponent(fourth)}`;
} else {
if (slug.length !== 2 || !second) {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
path = `/template/${encodeURIComponent(second)}`;
}
if (!path) {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
const template = await notiHubApi.get<Template | Template[]>(path);
return NextResponse.json(template, { status: 200 });
} catch (error: unknown) {
return errorToResponse(error);
}
};
export const PUT = async (
req: NextRequest,
context: RouteContext
) => {
try {
const {slug = []} = await context.params;
if (slug.length !== 1 || slug[0] !== "template") {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
const formData = await req.formData();
const templatePart = formData.get("template");
const body = templatePart instanceof Blob
? JSON.parse(await templatePart.text()) as CreateTemplateRequest
: null;
if (!body || !body.itrntPrjctCd || body.tmplatTy == null || !body.tmplatNm || !body.tmplatBody || !body.sendTel || !body.sendNm) {
return NextResponse.json({ message: "Template request body is invalid." }, { status: 400 });
}
const notiHubApi = await createNotiHubApi();
const backendForm = new FormData();
backendForm.append("template", new Blob([JSON.stringify(body)], {type: "application/json"}));
const created = await notiHubApi.putForm<Template>("/template", backendForm);
return NextResponse.json(created, { status: 200 });
} catch (error: unknown) {
return errorToResponse(error);
}
};
export const PATCH = async (
req: NextRequest,
context: RouteContext
) => {
try {
const {slug = []} = await context.params;
const [action] = slug;
if (action !== "template") {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
const formData = await req.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}`);
const next: Template = {
...current,
tmplatNm: body.tmplatNm,
tmplatPurpose: body.tmplatPurpose ?? current.tmplatPurpose ?? 0,
tmplatTitle: body.tmplatTitle,
tmplatBody: body.tmplatBody,
sendTel: body.sendTel,
};
const backendForm = new FormData();
backendForm.append(
"template",
new Blob([JSON.stringify(next)], {type: "application/json"})
);
await notiHubApi.patchForm<void>("/template", backendForm);
return NextResponse.json(next, { status: 200 });
} catch (error: unknown) {
return errorToResponse(error);
}
};
export const POST = async (
req: NextRequest,
context: RouteContext
) => {
try {
const {slug = []} = await context.params;
const [action] = slug;
if (action !== "sms-bulk") {
return NextResponse.json({ message: "Not found" }, { status: 404 });
}
const body = await req.json().catch(() => null) as SendSmsBulkRequest | null;
if (
!body ||
!body.templateIdx ||
!body.reqstCmpn ||
!body.reqstUser ||
!body.reqstSvc ||
!Array.isArray(body.destinations) ||
body.destinations.length === 0
) {
return NextResponse.json({ message: "SMS request body is invalid." }, { status: 400 });
}
const notiHubApi = await createNotiHubApi();
const template = await notiHubApi.get<Template>(`/template/${body.templateIdx}`);
const bulkSendKey = await notiHubApi.put<string>("/send-sms/create/bulk", {
reqstCmpn: body.reqstCmpn,
reqstUser: body.reqstUser,
reqstSvc: body.reqstSvc,
prjctCd: template.itrntPrjctCd,
tmplatIdx: template.tmplatIdx,
resve: false,
trgtCnt: body.destinations.length,
});
const result = await notiHubApi.post<BulkSendResponse>("/send-sms/bulk", {
sendTel: template.sendTel,
sendNm: template.sendNm,
bulkSendKey,
destInfoList: body.destinations,
});
return NextResponse.json(result, { status: 200 });
} catch (error: unknown) {
return errorToResponse(error);
}
};