260722_01_ENH: 버그 수정 및 개선 사항 수정

This commit is contained in:
2026-07-22 16:53:00 +09:00
parent d8b565276d
commit 2dd5b88c6f
67 changed files with 1925 additions and 736 deletions

View File

@@ -1,7 +1,9 @@
import { clientEnv } from "@/lib/env.client";
import { nextApi } from "@/lib/http-client";
import { CTI_AGENT_GROUP, CTI_AGENT_STATUS } from "@/const/ctiConst";
import type { ListCallHist, ListCallHistRequest } from "@/types/list";
import type { ListCallHist, ListCallHistRequest, ListCallHistSession } from "@/types/list";
import { getLocalStorage, removeLocalStorage, setLocalStorage } from "@/store/storage/secure-storage";
import { StorageKeys } from "@/store/storage/storage-keys";
// TEMP_CTI_LOG: 배포 후 CTI 연동 오류를 콘솔에서 바로 확인하기 위한 임시 로그.
// 안정화되면 "TEMP_CTI_LOG"로 검색해서 이 파일의 관련 로그를 전부 제거할 것.
@@ -29,6 +31,20 @@ type CtiCallContext = {
listHderId: number;
};
type PersistedCtiState = {
version: 1;
agentLoginId: string;
agentStatus: string | null;
currentCallContext: CtiCallContext | null;
lastCallContext: CtiCallContext | null;
agentSessionId: string | null;
lastCallHistId: number | null;
updatedAt: number;
};
const CTI_STATE_VERSION = 1;
const CTI_STATE_TTL_MS = 24 * 60 * 60 * 1000;
let socket: WebSocket | null = null;
let connectionState: CtiConnectionState = "idle";
let currentAgentLoginId: string | null = null;
@@ -36,6 +52,8 @@ let currentConnectionKey: string | null = null;
let currentLoginParams: CtiLoginParams | null = null;
let agentStatus: string | null = null;
let currentCallContext: CtiCallContext | null = null;
let lastCallContext: CtiCallContext | null = null;
let currentAgentSessionId: string | null = null;
let lastCallHistId: number | null = null;
const listeners = new Set<() => void>();
@@ -44,6 +62,103 @@ const notify = () => {
listeners.forEach((listener) => listener());
};
const isCallContext = (value: unknown): value is CtiCallContext => {
if (typeof value !== "object" || value === null) return false;
const context = value as Partial<CtiCallContext>;
return (
typeof context.prjctIdx === "number" &&
Number.isFinite(context.prjctIdx) &&
typeof context.sid === "number" &&
Number.isFinite(context.sid) &&
typeof context.listHderId === "number" &&
Number.isFinite(context.listHderId)
);
};
const isPersistedCtiState = (value: unknown): value is PersistedCtiState => {
if (typeof value !== "object" || value === null) return false;
const state = value as Partial<PersistedCtiState>;
return (
state.version === CTI_STATE_VERSION &&
typeof state.agentLoginId === "string" &&
(state.agentStatus === null || typeof state.agentStatus === "string") &&
(state.currentCallContext === null || isCallContext(state.currentCallContext)) &&
(state.lastCallContext === null || isCallContext(state.lastCallContext)) &&
(state.agentSessionId === null || typeof state.agentSessionId === "string") &&
(state.lastCallHistId === null || typeof state.lastCallHistId === "number") &&
typeof state.updatedAt === "number"
);
};
const persistCtiState = () => {
if (!currentAgentLoginId) return;
setLocalStorage(StorageKeys.CtiState, {
version: CTI_STATE_VERSION,
agentLoginId: currentAgentLoginId,
agentStatus,
currentCallContext,
lastCallContext,
agentSessionId: currentAgentSessionId,
lastCallHistId,
updatedAt: Date.now(),
} satisfies PersistedCtiState);
};
const restoreCtiState = (agentLoginId: string) => {
const stored = getLocalStorage(StorageKeys.CtiState);
if (
!isPersistedCtiState(stored) ||
stored.agentLoginId !== agentLoginId ||
Date.now() - stored.updatedAt > CTI_STATE_TTL_MS
) {
if (stored !== null) removeLocalStorage(StorageKeys.CtiState);
return false;
}
agentStatus = stored.agentStatus;
currentCallContext = stored.currentCallContext;
lastCallContext = stored.lastCallContext;
currentAgentSessionId = stored.agentSessionId;
lastCallHistId = stored.lastCallHistId;
notify();
return true;
};
const recoverLastCallHistId = async (agentLoginId: string) => {
const agentSessionId = currentAgentSessionId;
const callContext = currentCallContext ?? lastCallContext;
if (!agentSessionId || lastCallHistId !== null || !callContext) return;
try {
const result = await nextApi.get<ListCallHistSession>(
`/list-call-hist/session/${encodeURIComponent(agentSessionId)}`,
);
const isSameCall =
currentAgentLoginId === agentLoginId &&
currentAgentSessionId === agentSessionId &&
result.agentSessionId === agentSessionId &&
result.prjctIdx === callContext.prjctIdx &&
result.sid === callContext.sid &&
result.listHderId === callContext.listHderId;
if (!isSameCall) {
console.warn("[CTI][TEMP_LOG] recovered call history does not match persisted context", {
agentSessionId,
callContext,
result,
}); // TEMP_CTI_LOG
return;
}
lastCallContext = callContext;
setLastCallHistId(result.listCallHistIdx);
console.log("[CTI][TEMP_LOG] recovered listCallHistIdx ->", result.listCallHistIdx); // TEMP_CTI_LOG
} catch (error) {
console.warn("[CTI][TEMP_LOG] failed to recover call history", { agentSessionId, error }); // TEMP_CTI_LOG
}
};
const setConnectionState = (next: CtiConnectionState) => {
connectionState = next;
notify();
@@ -51,11 +166,13 @@ const setConnectionState = (next: CtiConnectionState) => {
const setAgentStatus = (next: string | null) => {
agentStatus = next;
persistCtiState();
notify();
};
const setLastCallHistId = (next: number | null) => {
lastCallHistId = next;
persistCtiState();
notify();
};
@@ -68,7 +185,8 @@ const saveCallHistory = (parsed: Record<string, unknown>, options: { clearContex
return;
}
const { prjctIdx, sid, listHderId } = currentCallContext;
const callContext = currentCallContext;
const { prjctIdx, sid, listHderId } = callContext;
const startTime = typeof parsed.StartTime === "string" ? parsed.StartTime : "";
const body: ListCallHistRequest = {
@@ -87,20 +205,37 @@ const saveCallHistory = (parsed: Record<string, unknown>, options: { clearContex
recordFileName: String(parsed.RecordFileName ?? ""),
};
currentAgentSessionId = body.agentSessionId || currentAgentSessionId;
persistCtiState();
if (
options.clearContextAfter &&
body.totalTime <= 0 &&
body.talkTime <= 0 &&
body.recordFileName.trim() === ""
) {
console.warn(
"[CTI][TEMP_LOG] CallClosed has no backend endDt marker (totalTime, talkTime, recordFileName)",
parsed,
); // TEMP_CTI_LOG
}
console.log("[CTI][TEMP_LOG] saving call history", { prjctIdx, sid, body }); // TEMP_CTI_LOG
nextApi
.put<ListCallHist>(`/list-call-hist/${prjctIdx}/${sid}`, body)
.then((result) => {
console.log("[CTI][TEMP_LOG] call history saved, listCallHistIdx =", result?.listCallHistIdx); // TEMP_CTI_LOG
lastCallContext = callContext;
setLastCallHistId(result?.listCallHistIdx ?? null);
if (options.clearContextAfter && currentCallContext === callContext) {
currentCallContext = null;
persistCtiState();
}
})
.catch((error) => {
console.error("[CTI][TEMP_LOG] failed to save call history", error); // TEMP_CTI_LOG
});
if (options.clearContextAfter) {
currentCallContext = null;
}
};
// WS 서버 -> 클라이언트로 오는 메시지는 ZCommand가 기준값이다.
@@ -125,11 +260,13 @@ const handleIncomingMessage = (raw: unknown) => {
}
case "StartSessionRecord": {
console.log("[CTI][TEMP_LOG] StartSessionRecord ->", parsed); // TEMP_CTI_LOG
setAgentStatus("2");
saveCallHistory(parsed, { clearContextAfter: false });
break;
}
case "CallClosed": {
console.log("[CTI][TEMP_LOG] CallClosed ->", parsed); // TEMP_CTI_LOG
setAgentStatus(null);
saveCallHistory(parsed, { clearContextAfter: true });
break;
}
@@ -163,11 +300,11 @@ const teardownSocket = () => {
socket = null;
}
setConnectionState("idle");
setAgentStatus(null);
};
const logout = () => {
console.log("[CTI][TEMP_LOG] logout called, currentAgentLoginId =", currentAgentLoginId, "connectionState =", connectionState); // TEMP_CTI_LOG
const shouldClearPersistedState = currentAgentLoginId !== null;
if (currentAgentLoginId && connectionState === "open") {
send({ ZCommand: "Logout", AgentLoginID: currentAgentLoginId });
}
@@ -176,7 +313,11 @@ const logout = () => {
currentConnectionKey = null;
currentLoginParams = null;
currentCallContext = null;
lastCallContext = null;
currentAgentSessionId = null;
setAgentStatus(null);
setLastCallHistId(null);
if (shouldClearPersistedState) removeLocalStorage(StorageKeys.CtiState);
};
const connectAndLogin = (params: CtiLoginParams) => {
@@ -203,6 +344,8 @@ const connectAndLogin = (params: CtiLoginParams) => {
currentAgentLoginId = params.agentLoginId;
currentConnectionKey = key;
currentLoginParams = params;
const restored = restoreCtiState(params.agentLoginId);
if (restored) void recoverLastCallHistId(params.agentLoginId);
setConnectionState("connecting");
console.log("[CTI][TEMP_LOG] opening websocket ->", clientEnv.CTI_WS_URL); // TEMP_CTI_LOG
@@ -252,10 +395,14 @@ export const ctiClient = {
getConnectionState: () => connectionState,
getAgentStatus: () => agentStatus,
getLastCallHistId: () => lastCallHistId,
getLastCallHistIdFor: (prjctIdx: number, sid: number) =>
lastCallContext?.prjctIdx === prjctIdx && lastCallContext.sid === sid ? lastCallHistId : null,
makeCall: (params: CtiMakeCallParams) => {
if (!currentAgentLoginId || !currentLoginParams) return;
console.log("[CTI][TEMP_LOG] makeCall", params); // TEMP_CTI_LOG
currentCallContext = { prjctIdx: params.prjctIdx, sid: params.sid, listHderId: params.listHderId };
lastCallContext = null;
currentAgentSessionId = null;
setLastCallHistId(null); // 새 통화 시작, 이전 통화의 listCallHistIdx는 더 이상 유효하지 않음
send({
ZCommand: "MakeCall",
@@ -265,8 +412,9 @@ export const ctiClient = {
});
},
hangup: requireAgent((agentLoginId) => {
// CTI_HANGUP_PENDING_VENDOR: 외주사와 ZCommand/동작 확인 후 UI 활성화 예정.
console.log("[CTI][TEMP_LOG] hangup"); // TEMP_CTI_LOG
send({ ZCommand: "HangUp", AgentLoginID: agentLoginId });
send({ ZCommand: "Hangup", AgentLoginID: agentLoginId });
}),
setReady: requireAgent((agentLoginId) => {
console.log("[CTI][TEMP_LOG] setReady"); // TEMP_CTI_LOG