39 lines
1.4 KiB
TypeScript
39 lines
1.4 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 {MessageBulkSendResponse} from "@/types/message";
|
|
|
|
const isChannel = (value: string): value is "sms" | "email" => value === "sms" || value === "email";
|
|
|
|
export const POST = async (req: NextRequest, context: RouteContext) => {
|
|
try {
|
|
const {slug = []} = await context.params;
|
|
const [channel, action, prjctIdx] = slug;
|
|
const contactApi = await createContactApi();
|
|
|
|
if (!isChannel(channel) || !prjctIdx) {
|
|
return NextResponse.json({message: "Not found"}, {status: 404});
|
|
}
|
|
|
|
const body = await req.json().catch(() => null);
|
|
if (!body) {
|
|
return NextResponse.json({message: "요청 바디가 없습니다."}, {status: 400});
|
|
}
|
|
|
|
if (action === "single") {
|
|
await contactApi.post(`/message/${channel}/single/${prjctIdx}`, body);
|
|
return new NextResponse(null, {status: 204});
|
|
}
|
|
|
|
if (action === "bulk") {
|
|
const result = await contactApi.post<MessageBulkSendResponse>(`/message/${channel}/bulk/${prjctIdx}`, body);
|
|
return NextResponse.json(result, {status: 200});
|
|
}
|
|
|
|
return NextResponse.json({message: "Not found"}, {status: 404});
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|