import {z} from 'zod' export type SalesLeadWrite = {title: string, details?: string | undefined, qualified?: boolean | undefined, company_id?: string | undefined, company_name?: string | undefined, primary_contact_id?: string | undefined, primary_contact_name?: string | undefined, email?: string | undefined, phone_number?: string | undefined, sales_rep_id?: string | undefined, sales_rep_name?: string | undefined, deal_value_in_dollars?: number | undefined, workflow_state?: string | undefined, next_contact_date?: string | undefined, expected_close_date?: string | undefined, lead_source_id?: string | undefined, lead_source_name?: string | undefined, pipeline_id?: string | undefined, pipeline_name?: string | undefined, pipeline_stage_id?: string | undefined, pipeline_stage_name?: string | undefined, division_id?: string | undefined, lead_source_real_name?: string | undefined, asset?: Array | undefined}; export type UpdateApiSalesLeadsIdArgs = {body: SalesLeadWrite}; export type UpdateApiSalesLeadsIdOptions = {baseUrl?: string | undefined, accountId?: string | undefined, authToken?: string | undefined, fetch?: typeof fetch | undefined}; export const namedRef = z.object({id: z.string().optional(), name: z.string().optional()}); export const party = z.object({id: z.string().optional(), name: z.string().nullable().optional(), initials: z.string().nullable().optional()}); export const salesLead = z.object({id: z.string().optional(), title: z.string().optional(), details: z.string().nullable().optional(), workflowState: z.string().nullable().optional(), nextContactDate: z.string().nullable().optional(), expectedCloseDate: z.string().nullable().optional(), qualified: z.boolean().nullable().optional(), dealValueInDollars: z.number().nullable().optional(), createdAt: z.string().optional(), createdById: z.string().nullable().optional(), updatedById: z.string().nullable().optional(), companyId: z.string().nullable().optional(), company: namedRef.optional(), leadSourceId: z.string().nullable().optional(), leadSource: namedRef.optional(), salesRepId: z.string().nullable().optional(), salesRep: party.optional(), pipelineStageId: z.string().nullable().optional(), pipelineStage: namedRef.optional(), primaryContactId: z.string().nullable().optional(), primaryContact: z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().nullable().optional()}).optional(), pipelineId: z.string().nullable().optional(), pipeline: z.object({id: z.string().optional(), name: z.string().optional(), stages: z.array(z.object({id: z.string().optional(), name: z.string().optional(), position: z.number().int().optional()})).optional()}).optional(), assignedToId: z.string().nullable().optional(), assignedTo: party.optional(), divisionId: z.string().nullable().optional(), division: z.record(z.string(), z.unknown()).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 a sales lead * * PUT /sales_leads/{id} */ export const updateApiSalesLeadsId = async (args: UpdateApiSalesLeadsIdArgs, options?: UpdateApiSalesLeadsIdOptions) => { const {baseUrl = "https://api.shopvox.com/v1", fetch: fetchFn = fetch, accountId, authToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (accountId !== undefined) query.set("account_id", accountId) if (authToken !== undefined) query.set("authToken", authToken) const url = `${baseUrl.replace(/\/+$/, '')}/sales_leads/${encodeURIComponent(String(args.id))}${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(salesLead, await res.json()) };