import {z} from 'zod' export type GetApiCallDetailArgs = {limit?: number | undefined, offset?: number | undefined, filter?: Array | undefined, recordURL?: boolean | undefined, fetchComment?: boolean | undefined}; export type GetApiCallDetailOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const callDetailResponse = z.object({call_id: z.number().int().optional(), call_flow_id: z.number().int(), group_id: z.number().int(), disposition: z.enum(["ANSWERED", "BUSY", "FAILED", "NO ANSWER", "NONE", "HANGUP"]).optional(), duration: z.number().int().optional(), caller_id: z.string(), tracking_number: z.string(), ring_to: z.string(), default_ring_to: z.string().optional(), repeat_call: z.boolean().optional(), call_started: z.string(), ai_caller_identity: z.string().optional(), bill_second: z.number().int().optional(), external_id: z.string().optional(), recording_file: z.string().optional(), call_value: z.number().int().optional(), dni_log_id: z.string().optional(), is_outbound: z.boolean().optional(), is_spam: z.boolean().optional(), call_mine_status: z.enum(["not mined", "mined", "pending", "cancel", "none"]).optional(), cdr_source: z.enum(["CE", "API", "BVR", "NONE"]), call_ended: z.string(), call_created: z.string().optional(), mined_timestamp: z.string().optional(), ring_to_name: z.string().optional(), analytic_status: z.string().optional(), channel_id: z.number().int().optional(), channel_category: z.string().optional(), channel_sub_category: z.string().optional(), channel: z.string().optional(), comments: z.array(z.object({comment_id: z.number().int().optional(), call_id: z.string().optional(), comment_text: z.string().optional()})).optional()}); export const getApiCallDetailResponse = z.array(callDetailResponse); 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 of calls including the detail record * * GET /call/detail */ export const getApiCallDetail = async (args: GetApiCallDetailArgs, options?: GetApiCallDetailOptions) => { const {baseUrl = "https://apicfa.conviza.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) for (const item of args.filter ?? []) query.append("filter", String(item)) if (args.recordURL != null) query.append("recordURL", String(args.recordURL)) if (args.fetchComment != null) query.append("fetchComment", String(args.fetchComment)) const url = `${baseUrl.replace(/\/+$/, '')}/call/detail${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(getApiCallDetailResponse, await res.json()) };