118 lines
3.3 KiB
TypeScript
118 lines
3.3 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,
|
|
SaveTemplateRequest,
|
|
SendSmsBulkRequest,
|
|
Template,
|
|
} from "@/types/notification";
|
|
|
|
export const GET = async (
|
|
_req: NextRequest,
|
|
context: RouteContext
|
|
) => {
|
|
try {
|
|
const {slug = []} = await context.params;
|
|
const [action, templateIdx] = slug;
|
|
|
|
if (action !== "template" || !templateIdx) {
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
}
|
|
|
|
const notiHubApi = await createNotiHubApi();
|
|
const template = await notiHubApi.get<Template>(`/template/${encodeURIComponent(templateIdx)}`);
|
|
|
|
return NextResponse.json(template, { 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 body = await req.json().catch(() => null) 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,
|
|
tmplatTitle: body.tmplatTitle,
|
|
tmplatBody: body.tmplatBody,
|
|
sendTel: body.sendTel,
|
|
};
|
|
|
|
await notiHubApi.patch<void>("/template", next);
|
|
|
|
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);
|
|
}
|
|
};
|