Files
T

68 lines
2.8 KiB
TypeScript

import {
registerIntegrationAdapter,
type AdapterResult,
type AdapterSyncOutcome,
type IntegrationAdapter,
} from "@hivegrid/foundation";
import type { TelephonyProviderInterface } from "../telephony/provider.js";
import { probeHalloPetraApiKey } from "./client.js";
import { HalloPetraError, type HalloPetraCredentials } from "./types.js";
import {
getHalloPetraRecordingUrl,
normalizeHalloPetraWebhook,
parseHalloPetraWebhookEvent,
validateHalloPetraWebhook,
} from "./webhook.js";
function healthFromError(error: unknown): AdapterResult {
if (error instanceof HalloPetraError) {
if (error.code === "auth_token_invalid") return { ok: false, health: "broken", messageKey: "telephony.health.tokenInvalid" };
if (error.code === "rate_limit_exceeded") return { ok: true, health: "degraded", messageKey: "telephony.health.rateLimited" };
if (error.code === "provider_outage") return { ok: false, health: "degraded", messageKey: "telephony.health.providerOutage" };
}
return { ok: false, health: "broken", messageKey: "telephony.health.unknown" };
}
async function probe(credentials: HalloPetraCredentials): Promise<AdapterResult> {
try {
await probeHalloPetraApiKey(credentials);
return { ok: true, health: "active", messageKey: "telephony.health.active" };
} catch (error) {
return healthFromError(error);
}
}
export const hallopetraAdapter: IntegrationAdapter<HalloPetraCredentials> = registerIntegrationAdapter<HalloPetraCredentials>({
key: "hallopetra",
connectionType: "api_key",
validateConnection: probe,
healthCheck: probe,
sync: (): Promise<AdapterSyncOutcome> =>
Promise.resolve({ items: [], summaryKey: "telephony.sync.summary.webhookEngine" }),
});
/**
* HalloPetra is an inbound assistant, not a click-to-call carrier. Provider-side
* webhook registration is manual in its dashboard; unsupported interface
* methods fail explicitly instead of pretending that a remote mutation ran.
*/
export const hallopetraTelephonyProvider: TelephonyProviderInterface<HalloPetraCredentials> = {
key: "hallopetra",
connect: probeHalloPetraApiKey,
disconnect: () => Promise.resolve(),
probe: probeHalloPetraApiKey,
listUsers: () => Promise.resolve([]),
listProviderUsers: () => Promise.resolve([]),
listUserNumbers: () => Promise.resolve([]),
startCall: () => Promise.reject(new HalloPetraError("capability_unsupported")),
triggerOutboundCall: () => Promise.reject(new HalloPetraError("capability_unsupported")),
createWebhook: () => Promise.reject(new HalloPetraError("capability_unsupported")),
subscribeToWebhooks: () => Promise.reject(new HalloPetraError("capability_unsupported")),
deleteWebhook: () => Promise.resolve(),
validateWebhook: validateHalloPetraWebhook,
parseWebhookEvent: parseHalloPetraWebhookEvent,
normalizeWebhook: normalizeHalloPetraWebhook,
getRecordingUrl: getHalloPetraRecordingUrl,
};