261022_02_ENH: 발송 취소에 대한 타입 추가와 헬퍼 추가
This commit is contained in:
58
src/noti-hub/send-cancellation.ts
Normal file
58
src/noti-hub/send-cancellation.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* 채널과 resultCode로 개별 건의 현재 예약 취소 상태를 판단한다.
|
||||
* 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_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 };
|
||||
};
|
||||
Reference in New Issue
Block a user