Files
front-common-library/src/noti-hub/send-cancellation.ts

68 lines
2.8 KiB
TypeScript

import { MessageType } from "./noti-hub-types.js";
import type { SendCancelResponse, SendResultInfo } from "./noti-hub-types.js";
/** 이메일 예약 취소 결과 코드. */
export const EMAIL_RESERVE_CANCELED_RESULT_CODE = 108;
/** 문자(SMS, LMS, MMS)·알림톡(KKO) 예약 취소 결과 코드. */
export const SMS_KKO_RESERVE_CANCELED_RESULT_CODE = 9998;
/**
* 예약 취소 버튼을 활성화할 수 있는지 판단한다. cancelable === true일 때만 true.
* 값이 없으면(구버전 응답) false. cancelDt나 결과 라벨·코드로 추정하지 않는다.
*/
export const isSendCancelable = (item: { cancelable?: boolean }): boolean =>
item.cancelable === true;
/**
* 채널과 resultCode로 개별 건의 현재 예약 취소 상태를 판단한다.
* 현재 취소 상태 표시용이며, 취소 가능 여부 판단에는 isSendCancelable을 사용한다.
* cancelDt는 요청 이력이므로 상태 판정에 사용하지 않는다. 알 수 없는 채널은 false.
* 표시명은 서버의 resultViewLabel/resultLabel을 그대로 사용한다("예약 취소").
* 별도 결과 코드 목록 API는 없다.
*/
export const isReserveCanceledResult = (
result: Pick<SendResultInfo, "sendTy" | "resultCode">,
): boolean => {
switch (result.sendTy) {
case MessageType.EMAIL:
return result.resultCode === EMAIL_RESERVE_CANCELED_RESULT_CODE;
case MessageType.SMS:
case MessageType.LMS:
case MessageType.MMS:
case MessageType.KKO:
return result.resultCode === SMS_KKO_RESERVE_CANCELED_RESULT_CODE;
default:
return false;
}
};
const isCount = (value: unknown): value is number =>
typeof value === "number" && Number.isInteger(value) && value >= 0;
/**
* 취소 응답의 각 건수가 0 이상 정수이고 합계 관계가 맞는지 검증한다.
* HTTP 200이어도 취소 불가 건이 있을 수 있으며, 벌크 대상은 현재 등록된 상세 행 기준이다.
* @throws {TypeError} 응답 형태, 건수 또는 합계가 올바르지 않을 때.
*/
export const parseSendCancelResponse = (value: unknown): SendCancelResponse => {
if (
typeof value !== "object" || value === null || Array.isArray(value) ||
!("targetCount" in value) || !("canceledCount" in value) ||
!("alreadyCanceledCount" in value) || !("notCancelableCount" in value)
) {
throw new TypeError("Invalid send cancellation response");
}
const { targetCount, canceledCount, alreadyCanceledCount, notCancelableCount } = value;
if (
!isCount(targetCount) || !isCount(canceledCount) ||
!isCount(alreadyCanceledCount) || !isCount(notCancelableCount) ||
targetCount !== canceledCount + alreadyCanceledCount + notCancelableCount
) {
throw new TypeError("Invalid send cancellation counts or total");
}
return { targetCount, canceledCount, alreadyCanceledCount, notCancelableCount };
};