import {z} from 'zod' export type ELeadStatusInputApi = number; export type LeadToUpdateApi = {phoneNumber?: (string | null) | undefined, externalId?: (string | null) | undefined, timeZoneId?: (string | null) | undefined, callData?: ({[key: string]: string} | null) | undefined, status?: (ELeadStatusInputApi | null) | undefined}; export type UpdateApiV2OutboundPearlIdLeadLeadIdArgs = {pearlId: string, leadId: string, body: LeadToUpdateApi}; export type UpdateApiV2OutboundPearlIdLeadLeadIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const eLeadStatusView = z.number().int(); export const eCallStatusView = z.number().int(); export const eChatMessageRoleView = z.number().int(); export const agentChatMessageApiView = z.object({role: eChatMessageRoleView.optional(), content: z.string().nullable().optional(), startTime: z.number().nullable().optional(), endTime: z.number().nullable().optional()}); export const collectedInfoView = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), value: z.unknown()}); export const callCollectedDataApiView = z.object({transcript: z.array(agentChatMessageApiView).nullable().optional(), collectedInfos: z.array(collectedInfoView).nullable().optional()}); export const leadLastCallApiView = z.object({id: z.string().nullable().optional(), startTime: z.string().optional(), pearlId: z.string().nullable().optional(), status: eCallStatusView.optional(), from: z.string().nullable().optional(), name: z.string().nullable().optional(), collectedData: callCollectedDataApiView.nullable().optional(), summary: z.string().nullable().optional(), duration: z.number().int().optional(), recording: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional()}); export const leadApiView = z.object({id: z.string().nullable().optional(), externalId: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), timeZone: z.string().nullable().optional(), status: eLeadStatusView.optional(), created: z.string().optional(), callsId: z.array(z.string()).nullable().optional(), lastCall: leadLastCallApiView.nullable().optional(), callData: z.record(z.string(), z.string()).nullable().optional(), collectedData: z.record(z.string(), z.unknown()).nullable().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 }; /** * Update Lead * * PUT /v2/Outbound/{pearlId}/Lead/{leadId} */ export const updateApiV2OutboundPearlIdLeadLeadId = async (args: UpdateApiV2OutboundPearlIdLeadLeadIdArgs, options?: UpdateApiV2OutboundPearlIdLeadLeadIdOptions) => { const {baseUrl = "https://api.nlpearl.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/Outbound/${encodeURIComponent(String(args.pearlId))}/Lead/${encodeURIComponent(String(args.leadId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(leadApiView, await res.json()) };