37 lines
1.6 KiB
TypeScript
37 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import { formatSendCancelResult, resolveSendCancelErrorMessage, useCancelSendMutation } from "@/query/message";
|
|
import type { SendCancelRequest } from "@/types/message";
|
|
import { isFieldWorkUser } from "@/utils/userRole";
|
|
|
|
/**
|
|
* 예약 취소 확인 → 요청 → 결과 안내.
|
|
* 최종 취소 가능 여부는 서버 응답 건수로 판단하며, 요청 중에는 isPending으로 중복 요청을 막는다.
|
|
* 면접원(FW 역할 그룹)에게는 취소 기능을 제공하지 않는다(canCancel=false).
|
|
*/
|
|
export function useSendCancel() {
|
|
const { auth } = useAuth();
|
|
const cancelMutation = useCancelSendMutation();
|
|
const canCancel = auth.user != null && !isFieldWorkUser(auth.user.userRole);
|
|
|
|
const cancel = async (request: SendCancelRequest) => {
|
|
if (!canCancel || cancelMutation.isPending) return;
|
|
|
|
const confirmMessage =
|
|
request.type === "bulk"
|
|
? "이 대량발송의 예약 건을 모두 취소하시겠습니까?\n이미 발송이 시작된 건은 취소되지 않습니다."
|
|
: "이 예약 발송을 취소하시겠습니까?\n이미 발송이 시작된 건은 취소되지 않습니다.";
|
|
if (!window.confirm(confirmMessage)) return;
|
|
|
|
try {
|
|
const res = await cancelMutation.mutateAsync(request);
|
|
alert(`예약 취소 요청이 처리되었습니다.\n${formatSendCancelResult(res)}`);
|
|
} catch (error: unknown) {
|
|
alert(resolveSendCancelErrorMessage(error));
|
|
}
|
|
};
|
|
|
|
return { canCancel, cancel, isPending: cancelMutation.isPending } as const;
|
|
}
|