import {z} from 'zod' export type GetApiPhonesCallsHistoryArgs = {take?: (number | unknown) | undefined, skip?: (number | unknown) | undefined}; export type GetApiPhonesCallsHistoryOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const callHistoryDto = z.object({id: z.string().optional(), direction: z.string().optional(), callerNumber: z.string().nullable().optional(), callerName: z.string().nullable().optional(), destinationNumber: z.string().nullable().optional(), startTime: z.string().nullable().optional(), answerTime: z.string().nullable().optional(), endTime: z.string().nullable().optional(), duration: z.union([z.number().int(), z.string()]).optional(), hangupCause: z.string().nullable().optional(), recordingUrl: z.string().nullable().optional(), extensionName: z.string().nullable().optional(), clientId: z.string().nullable().optional(), patientId: z.string().nullable().optional()}); export const callHistoryResponse = z.object({calls: z.array(callHistoryDto).optional(), total: z.union([z.number().int(), z.string()]).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get call history from the VoiceCalls table (Telnyx/Retell calls). * * GET /Phones/calls/history */ export const getApiPhonesCallsHistory = async (args: GetApiPhonesCallsHistoryArgs, options?: GetApiPhonesCallsHistoryOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.take != null) query.append("take", String(args.take)) if (args.skip != null) query.append("skip", String(args.skip)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/Phones/calls/history${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(callHistoryResponse, await res.json()) };