import {z} from 'zod' export type CreateUpdateAddressDetailsV2Dto = {country?: string | undefined, state?: string | undefined, city?: string | undefined, zipCode?: string | undefined, address?: string | undefined, unit?: string | undefined}; export type UpdateClientInfoV2Dto = {firstName?: string | undefined, lastName?: string | undefined, email?: string | undefined, companyName?: string | undefined, primaryExt?: string | undefined, primaryPhone?: string | undefined, secondaryExt?: string | undefined, secondaryPhone?: string | undefined, addressDetails?: CreateUpdateAddressDetailsV2Dto | undefined}; export type CustomFieldValueV2Dto = {id: string, value: string | number | boolean}; export type UpdateLeadV2Dto = {status?: ('new' | 'scheduled' | 'in progress' | 'estimated' | 'approved' | 'and any user-defined status') | undefined, date?: string | undefined, endDate?: string | undefined, isAllDay?: boolean | undefined, isScheduled?: boolean | undefined, description?: string | undefined, timeZone?: string | undefined, jobTypeId?: string | undefined, adGroupId?: string | undefined, clientInfo?: UpdateClientInfoV2Dto | undefined, createdBy?: string | undefined, tags?: Array | undefined, customFields?: Array | undefined}; export type UpdateApiV2LeadsIdArgs = {id: string, Authorization: string, body: UpdateLeadV2Dto}; export type UpdateApiV2LeadsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseGenericJobTypeV2Dto = z.object({id: z.string().optional(), name: z.string().optional()}); export const responseGenericAdGroupV2Dto = z.object({id: z.string().optional(), name: z.string().optional()}); export const responseJobLeadAddressV2Dto = z.object({country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zipCode: z.string().optional(), address: z.string().optional(), unit: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), locationKey: z.string().optional()}); export const responseGenericServiceAreaV2Dto = z.object({id: z.string().optional(), name: z.string().optional()}); export const responseJobLeadDetailsV2Dto = z.object({clientId: z.string().nullable().optional(), serialId: z.number().optional(), firstName: z.string().optional(), lastName: z.string().optional(), companyName: z.string().optional(), email: z.string().optional(), primaryExt: z.string().optional(), primaryPhone: z.string().optional(), secondaryExt: z.string().optional(), secondaryPhone: z.string().optional(), addressDetails: responseJobLeadAddressV2Dto.optional(), serviceArea: responseGenericServiceAreaV2Dto.optional()}); export const responseGenericTechV2Dto = z.object({id: z.string().optional(), name: z.string().optional()}); export const responseGenericTagV2Dto = z.object({id: z.string().optional(), name: z.string().optional()}); export const responseGenericCustomFieldV2Dto = z.object({id: z.string().optional(), fieldName: z.string().optional(), value: z.union([z.string(), z.number(), z.boolean(), z.array(z.object({name: z.string().optional(), url: z.string().optional()}))]).optional()}); export const responseJobLeadNoteV2Dto = z.object({id: z.string().optional(), techName: z.string().optional(), note: z.string().optional(), timestamp: z.string().optional()}); export const responseGenericAttachmentV2Dto = z.object({id: z.string().optional(), name: z.string().optional(), fileUrl: z.string().optional()}); export const responseOneLeadV2Dto = z.object({id: z.string().optional(), uuid: z.string().optional(), serialId: z.number().optional(), status: z.string().optional(), date: z.string().nullable().optional(), endDate: z.string().nullable().optional(), isAllDay: z.boolean().optional(), isScheduled: z.boolean().optional(), jobType: responseGenericJobTypeV2Dto.optional(), adGroup: responseGenericAdGroupV2Dto.optional(), description: z.string().optional(), leadLost: z.boolean().optional(), clientInfo: responseJobLeadDetailsV2Dto.optional(), team: z.array(responseGenericTechV2Dto).optional(), tags: z.array(responseGenericTagV2Dto).optional(), customFields: z.array(responseGenericCustomFieldV2Dto).optional(), notes: z.array(responseJobLeadNoteV2Dto).optional(), statusUpdated: z.string().optional(), utcUpdatedAt: z.string().optional(), updatedAt: z.string().optional(), createdAt: z.string().optional(), attachments: z.array(responseGenericAttachmentV2Dto).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 an existing Lead * * PUT /v2/leads/{id} */ export const updateApiV2LeadsId = async (args: UpdateApiV2LeadsIdArgs, options?: UpdateApiV2LeadsIdOptions) => { const {baseUrl = "https://api.workiz.com/crm/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(responseOneLeadV2Dto, await res.json()) };