import {z} from 'zod' export type CallDirection = 'INBOUND' | 'OUTBOUND'; export type CallStatus = 'CREATED' | 'ONGOING' | 'TRANSFERRED' | 'DISCONNECTED' | 'FAILED' | 'FINISHED' | 'VOICEMAIL'; export type GetApiCommsCallListArgs = {TENANT: string, 'API-KEY': string, page?: (number | null) | undefined, per_page?: (number | null) | undefined, sort_by?: (string | null) | undefined, sort_direction?: ('asc' | 'desc' | null) | undefined, location_id?: (string | null) | undefined, id?: (string | null) | undefined, patient_id?: (string | null) | undefined, user_id?: (string | null) | undefined, user_name?: (string | null) | undefined, created_date?: (Array | null) | undefined, duration?: (number | null) | undefined, direction?: (CallDirection | null) | undefined, status?: (Array | null) | undefined, transcript_contains?: (string | null) | undefined, tags?: (Array | null) | undefined, phone_number?: (string | null) | undefined, tz: string}; export type GetApiCommsCallListOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const userType = z.enum(["PROVIDER", "PATIENT", "ASSISTANT"]); export const userTiny = z.object({id: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), email: z.string().nullable().optional(), type: userType, locationId: z.string().nullable().optional()}); export const callDirection = z.enum(["INBOUND", "OUTBOUND"]); export const callStatus = z.enum(["CREATED", "ONGOING", "TRANSFERRED", "DISCONNECTED", "FAILED", "FINISHED", "VOICEMAIL"]); export const toolCallSummary = z.object({id: z.string(), callId: z.string().nullable().optional(), messageId: z.string().nullable().optional(), alertId: z.string().nullable().optional(), toolName: z.string(), toolArguments: z.object({}), toolCallId: z.string().nullable().optional(), result: z.object({}).nullable().optional(), toolCallDescription: z.string().nullable().optional(), hasBeenCalled: z.boolean().optional(), createdDate: z.string()}); export const tagSummary = z.object({name: z.string(), description: z.string().nullable().optional(), useForAlerts: z.boolean(), useForCalls: z.boolean(), id: z.string()}); export const callType = z.enum(["MANUAL", "AI"]); export const callTable = z.object({id: z.string(), sender: userTiny.nullable().optional(), receiver: userTiny, chatId: z.string(), direction: callDirection, status: callStatus, completedDate: z.string().nullable().optional(), retellCallId: z.string().nullable().optional(), disconnectionReason: z.string().nullable().optional(), pathToAudio: z.string().nullable().optional(), summary: z.string().nullable().optional(), context: z.object({}).nullable().optional(), createdDate: z.string().nullable().optional(), toolCalls: z.array(toolCallSummary).optional(), tags: z.array(tagSummary).optional(), invoiceId: z.string().nullable().optional(), type: callType.optional(), locationId: z.string().nullable().optional()}); export const pageCallTable = z.object({data: z.array(callTable), total: z.number().int()}); 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 }; /** * List Calls * * GET /comms/call/list */ export const getApiCommsCallList = async (args: GetApiCommsCallListArgs, options: GetApiCommsCallListOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.location_id != null) query.append("location_id", String(args.location_id)) if (args.id != null) query.append("id", String(args.id)) if (args.patient_id != null) query.append("patient_id", String(args.patient_id)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.user_name != null) query.append("user_name", String(args.user_name)) for (const item of args.created_date ?? []) query.append("created_date", String(item)) if (args.duration != null) query.append("duration", String(args.duration)) if (args.direction != null) query.append("direction", String(args.direction)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.transcript_contains != null) query.append("transcript_contains", String(args.transcript_contains)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.phone_number != null) query.append("phone_number", String(args.phone_number)) if (args.tz != null) query.append("tz", String(args.tz)) const url = `${baseUrl.replace(/\/+$/, '')}/comms/call/list${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(pageCallTable, await res.json()) };