import {z} from 'zod' export type GetApiV2CallsIdArgs = {id: string, Authorization: string}; export type GetApiV2CallsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseCallInsightsV2Dto = z.object({sentiment: z.enum(["great", "positive", "neutral", "negative"]).nullable().optional(), intent: z.enum(["booking", "callbackRequest", "existingBooking", "spam", "other"]).nullable().optional(), dispatcherScore: z.number().nullable().optional(), objection: z.enum(["price", "serviceNotOffered", "outOfServiceArea", "noAvailability", "Dispatcher experience issue", "notReadyToBook", "Callback needed", "disconnected", "other"]).nullable().optional(), pricingDiscussed: z.boolean().optional(), booked: z.boolean().optional()}); export const responseOneCallV2Dto = z.object({id: z.string().optional(), direction: z.enum(["inbound", "outgoing"]).optional(), from: z.string().optional(), to: z.string().optional(), callMaskingNumber: z.string().nullable().optional(), duration: z.number().optional(), status: z.enum(["answered", "missed", "voicemail", "active", "geniusAnswering"]).optional(), hasRecording: z.boolean().optional(), flowName: z.string().nullable().optional(), adGroupId: z.string().nullable().optional(), clientId: z.string().nullable().optional(), clientName: z.string().nullable().optional(), jobId: z.string().nullable().optional(), leadId: z.string().nullable().optional(), answeredByUserId: z.string().nullable().optional(), answeredByUserName: z.string().nullable().optional(), calledByUserId: z.string().nullable().optional(), calledByUserName: z.string().nullable().optional(), tags: z.array(z.string()).optional(), insights: responseCallInsightsV2Dto.nullable().optional(), updatedAt: z.string().nullable().optional(), createdAt: 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 }; /** * Retrieve a single Call * * GET /v2/calls/{id} */ export const getApiV2CallsId = async (args: GetApiV2CallsIdArgs, options?: GetApiV2CallsIdOptions) => { const {baseUrl = "https://api.workiz.com/crm/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/calls/${encodeURIComponent(String(args.id))}${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(responseOneCallV2Dto, await res.json()) };