Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/integrations/src/admin-telephony-worker.server.ts
T

106 lines
3.4 KiB
TypeScript

import type {
AdminTelephonyRepository,
PersistAdminCallEventResult,
} from "@hivegrid/db/admin-telephony.server";
import {
classifyTelephonyPhoneCandidates,
normalizePhoneNumber,
reduceTelephonyCallEvent,
type TelephonyCallSnapshot,
} from "./telephony/call-processing.js";
import type { TelephonyCallEvent } from "./telephony/provider.js";
export async function processVerifiedAdminTelephonyEvent(input: {
readonly phoneConnectionId: string;
readonly adminUserId: string;
readonly event: TelephonyCallEvent;
readonly repository: AdminTelephonyRepository;
readonly queueRecording?: (input: {
readonly adminUserId: string;
readonly callSessionId: string;
readonly providerRecordingUrl: string;
readonly occurredAt: Date;
}) => Promise<unknown>;
}): Promise<PersistAdminCallEventResult | null> {
const current = await input.repository.getCallSnapshot({
phoneConnectionId: input.phoneConnectionId,
adminUserId: input.adminUserId,
providerCallId: input.event.providerCallId,
});
const snapshot = reduceTelephonyCallEvent(
current ? toProviderNeutralSnapshot(current) : null,
input.event,
);
if (!snapshot) return null;
const externalNumber = normalizePhoneNumber(
input.event.externalNumber ??
(snapshot.direction === "inbound"
? snapshot.fromNumber
: snapshot.toNumber),
);
if (!externalNumber) return null;
const candidates = await input.repository.listPhoneCandidates({
adminUserId: input.adminUserId,
normalizedPhone: externalNumber,
suffix: externalNumber.replace(/\D/gu, "").slice(-8),
});
const classified = classifyTelephonyPhoneCandidates(
externalNumber,
candidates,
);
const persisted = await input.repository.persistCallEvent({
phoneConnectionId: input.phoneConnectionId,
adminUserId: input.adminUserId,
providerCallId: input.event.providerCallId,
dedupeKey: input.event.dedupeKey,
providerEvent: input.event.providerEvent,
occurredAt: new Date(input.event.occurredAtIso),
externalNumber,
snapshot: {
direction: snapshot.direction,
status: snapshot.status,
providerUserId: snapshot.providerUserId,
fromNumber: snapshot.fromNumber,
toNumber: snapshot.toNumber,
startedAt: snapshot.startedAt,
answeredAt: snapshot.answeredAt,
endedAt: snapshot.endedAt,
durationSeconds: snapshot.durationSeconds,
},
candidates: classified.map((candidate) => ({
entityType: candidate.entityType,
entityId: candidate.entityId,
confidenceBasisPoints:
candidate.confidence === "hard" ? 10_000 : 7_500,
matchReason: candidate.confidence === "hard" ? "e164" : "suffix",
})),
});
if (snapshot.recordingUrl && input.queueRecording) {
await input.queueRecording({
adminUserId: input.adminUserId,
callSessionId: persisted.call.id,
providerRecordingUrl: snapshot.recordingUrl,
occurredAt: new Date(input.event.occurredAtIso),
});
}
return persisted;
}
function toProviderNeutralSnapshot(call: {
readonly direction: "inbound" | "outbound";
readonly status: "ringing" | "answered" | "completed" | "missed" | "failed";
readonly providerUserId: string | null;
readonly fromNumber: string;
readonly toNumber: string;
readonly startedAt: Date;
readonly answeredAt: Date | null;
readonly endedAt: Date | null;
readonly durationSeconds: number | null;
}): TelephonyCallSnapshot {
return {
...call,
recordingUrl: null,
};
}