261022_03_ENH: 예약 취소 추가
This commit is contained in:
@@ -2,17 +2,48 @@ 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 {SendCancelResponse} from "@kri/common/noti-hub";
|
||||
import type {MessageSendChannel, SendSingleResponse} from "@/types/message";
|
||||
|
||||
const isChannel = (value: string): value is MessageSendChannel =>
|
||||
value === "sms" || value === "email" || value === "kko";
|
||||
|
||||
const BULK_SEND_KEY_MAX_LENGTH = 32;
|
||||
|
||||
const readCancelBody = (body: unknown, target: string): Record<string, number | string> | null => {
|
||||
if (!body || typeof body !== "object") return null;
|
||||
if (target === "single") {
|
||||
const sendIdx = (body as {sendIdx?: unknown}).sendIdx;
|
||||
return typeof sendIdx === "number" && Number.isSafeInteger(sendIdx) && sendIdx > 0 ? {sendIdx} : null;
|
||||
}
|
||||
if (target === "bulk") {
|
||||
const bulkSendKey = (body as {bulkSendKey?: unknown}).bulkSendKey;
|
||||
return typeof bulkSendKey === "string" && bulkSendKey.trim() !== "" && bulkSendKey.length <= BULK_SEND_KEY_MAX_LENGTH
|
||||
? {bulkSendKey}
|
||||
: null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const POST = async (req: NextRequest, context: RouteContext) => {
|
||||
try {
|
||||
const {slug = []} = await context.params;
|
||||
const [channel, action, prjctIdx] = slug;
|
||||
const contactApi = await createContactApi();
|
||||
|
||||
// 예약 취소 (POST /message-send/cancel/{single|bulk}). 채널 구분 없이 공통 API를 사용한다.
|
||||
if (channel === "cancel") {
|
||||
if (action !== "single" && action !== "bulk") {
|
||||
return NextResponse.json({message: "Not found"}, {status: 404});
|
||||
}
|
||||
const cancelBody = readCancelBody(await req.json().catch(() => null), action);
|
||||
if (!cancelBody) {
|
||||
return NextResponse.json({message: "취소 요청 값이 올바르지 않습니다."}, {status: 400});
|
||||
}
|
||||
const result = await contactApi.post<SendCancelResponse>(`/message-send/cancel/${action}`, cancelBody);
|
||||
return NextResponse.json(result, {status: 200});
|
||||
}
|
||||
|
||||
if (!isChannel(channel) || !prjctIdx) {
|
||||
return NextResponse.json({message: "Not found"}, {status: 404});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user