260728_02_ENH: SID String으로 교체

This commit is contained in:
2026-07-28 11:47:33 +09:00
parent d97015741a
commit d9f53c0c3d
18 changed files with 151 additions and 54 deletions

View File

@@ -22,7 +22,7 @@ type CtiLoginParams = {
type CtiMakeCallParams = {
dialNumber: string;
prjctIdx: number;
sid: number;
sid: string;
listHderId: number;
aniNumber: string;
};
@@ -32,7 +32,7 @@ export type CtiCallPhase = "idle" | "dialing" | "active";
type CtiCallContext = {
prjctIdx: number;
sid: number;
sid: string;
listHderId: number;
};
@@ -42,7 +42,7 @@ type CtiCall = {
};
type PersistedCtiState = {
version: 2;
version: 3;
agentLoginId: string;
agentStatus: string | null;
activeCall: CtiCall | null;
@@ -54,7 +54,25 @@ type PersistedCtiState = {
updatedAt: number;
};
const CTI_STATE_VERSION = 2;
type LegacyCtiCallContext = Omit<CtiCallContext, "sid"> & {
sid: number;
};
type LegacyCtiCall = Omit<CtiCall, "context"> & {
context: LegacyCtiCallContext;
};
type LegacyPersistedCtiState = Omit<
PersistedCtiState,
"version" | "activeCall" | "closingCall" | "lastCallContext"
> & {
version: 2;
activeCall: LegacyCtiCall | null;
closingCall: LegacyCtiCall | null;
lastCallContext: LegacyCtiCallContext | null;
};
const CTI_STATE_VERSION = 3;
const CTI_STATE_TTL_MS = 24 * 60 * 60 * 1000;
let socket: WebSocket | null = null;
@@ -92,6 +110,19 @@ const isCallInProgress = () =>
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 === "string" &&
context.sid.length > 0 &&
typeof context.listHderId === "number" &&
Number.isFinite(context.listHderId)
);
};
const isLegacyCallContext = (value: unknown): value is LegacyCtiCallContext => {
if (typeof value !== "object" || value === null) return false;
const context = value as Partial<LegacyCtiCallContext>;
return (
typeof context.prjctIdx === "number" &&
Number.isFinite(context.prjctIdx) &&
@@ -111,6 +142,15 @@ const isCtiCall = (value: unknown): value is CtiCall => {
);
};
const isLegacyCtiCall = (value: unknown): value is LegacyCtiCall => {
if (typeof value !== "object" || value === null) return false;
const call = value as Partial<LegacyCtiCall>;
return (
isLegacyCallContext(call.context) &&
(call.agentSessionId === null || typeof call.agentSessionId === "string")
);
};
const isPersistedCtiState = (value: unknown): value is PersistedCtiState => {
if (typeof value !== "object" || value === null) return false;
const state = value as Partial<PersistedCtiState>;
@@ -128,6 +168,48 @@ const isPersistedCtiState = (value: unknown): value is PersistedCtiState => {
);
};
const isLegacyPersistedCtiState = (value: unknown): value is LegacyPersistedCtiState => {
if (typeof value !== "object" || value === null) return false;
const state = value as Partial<LegacyPersistedCtiState>;
return (
state.version === 2 &&
typeof state.agentLoginId === "string" &&
(state.agentStatus === null || typeof state.agentStatus === "string") &&
(state.activeCall === null || isLegacyCtiCall(state.activeCall)) &&
(state.closingCall === null || isLegacyCtiCall(state.closingCall)) &&
(state.lastCallContext === null || isLegacyCallContext(state.lastCallContext)) &&
(state.lastCallHistId === null || typeof state.lastCallHistId === "number") &&
typeof state.hangupRequested === "boolean" &&
typeof state.callClosedReceived === "boolean" &&
typeof state.updatedAt === "number"
);
};
const migrateLegacyCallContext = (context: LegacyCtiCallContext): CtiCallContext => ({
...context,
sid: String(context.sid),
});
const migrateLegacyCall = (call: LegacyCtiCall | null): CtiCall | null =>
call
? {
...call,
context: migrateLegacyCallContext(call.context),
}
: null;
const toPersistedCtiState = (value: unknown): PersistedCtiState | null => {
if (isPersistedCtiState(value)) return value;
if (!isLegacyPersistedCtiState(value)) return null;
return {
...value,
version: CTI_STATE_VERSION,
activeCall: migrateLegacyCall(value.activeCall),
closingCall: migrateLegacyCall(value.closingCall),
lastCallContext: value.lastCallContext ? migrateLegacyCallContext(value.lastCallContext) : null,
};
};
const persistCtiState = () => {
if (!currentAgentLoginId) return;
setLocalStorage(StorageKeys.CtiState, {
@@ -146,22 +228,24 @@ const persistCtiState = () => {
const restoreCtiState = (agentLoginId: string) => {
const stored = getLocalStorage(StorageKeys.CtiState);
const persistedState = toPersistedCtiState(stored);
if (
!isPersistedCtiState(stored) ||
stored.agentLoginId !== agentLoginId ||
Date.now() - stored.updatedAt > CTI_STATE_TTL_MS
!persistedState ||
persistedState.agentLoginId !== agentLoginId ||
Date.now() - persistedState.updatedAt > CTI_STATE_TTL_MS
) {
if (stored !== null) removeLocalStorage(StorageKeys.CtiState);
return false;
}
agentStatus = stored.agentStatus;
activeCall = stored.activeCall;
closingCall = stored.closingCall;
lastCallContext = stored.lastCallContext;
lastCallHistId = stored.lastCallHistId;
hangupRequested = stored.hangupRequested;
callClosedReceived = stored.callClosedReceived;
agentStatus = persistedState.agentStatus;
activeCall = persistedState.activeCall;
closingCall = persistedState.closingCall;
lastCallContext = persistedState.lastCallContext;
lastCallHistId = persistedState.lastCallHistId;
hangupRequested = persistedState.hangupRequested;
callClosedReceived = persistedState.callClosedReceived;
if (persistedState !== stored) persistCtiState();
notify();
return true;
};
@@ -324,7 +408,7 @@ const saveCallHistory = (parsed: Record<string, unknown>, phase: "start" | "clos
const action = phase === "start" ? "call-start" : "call-close";
console.log(`[CTI][TEMP_LOG] saving ${action}`, { prjctIdx, sid, body }); // TEMP_CTI_LOG
nextApi
.put<ListCallHist>(`/list-call-hist/${prjctIdx}/${sid}/${action}`, body)
.put<ListCallHist>(`/list-call-hist/${prjctIdx}/${encodeURIComponent(sid)}/${action}`, body)
.then((result) => {
console.log(`[CTI][TEMP_LOG] ${action} saved, listCallHistIdx =`, result?.listCallHistIdx); // TEMP_CTI_LOG
lastCallContext = callContext;
@@ -570,7 +654,7 @@ export const ctiClient = {
},
getConnectionState: () => connectionState,
getCallPhase,
getLastCallHistIdFor: (prjctIdx: number, sid: number) =>
getLastCallHistIdFor: (prjctIdx: number, sid: string) =>
lastCallContext?.prjctIdx === prjctIdx && lastCallContext.sid === sid ? lastCallHistId : null,
makeCall: (params: CtiMakeCallParams) => {
if (!currentAgentLoginId) return;