import {z} from 'zod' export type CreateClientAddressDetailsV2Dto = {country?: string | undefined, state?: string | undefined, city?: string | undefined, zipCode?: string | undefined, address?: string | undefined, unit?: string | undefined}; export type CustomFieldValueV2Dto = {id: string, value: string | number | boolean}; export type CreateClientV2Dto = {firstName: string, lastName?: string | undefined, companyName?: string | undefined, email?: string | undefined, primaryExt?: string | undefined, primaryPhone?: string | undefined, secondaryExt?: string | undefined, secondaryPhone?: string | undefined, addressDetails?: CreateClientAddressDetailsV2Dto | undefined, billingAddressDetails?: CreateClientAddressDetailsV2Dto | undefined, adGroupId?: string | undefined, allowBilling?: boolean | undefined, parentClientId?: string | undefined, taxExempt?: boolean | undefined, taxExemptReason?: ('1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10' | '11' | '12' | '13' | '14' | '15' | '99') | undefined, description?: string | undefined, customFields?: Array | undefined}; export type CreateApiV2ClientsArgs = {Authorization: string, body: CreateClientV2Dto}; export type CreateApiV2ClientsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseGenericAdGroupV2Dto = z.object({id: z.string().optional(), name: z.string().optional()}); export const responseOneTagV2Dto = z.object({id: z.string().optional(), name: z.string().optional(), color: z.string().optional()}); export const responseOneBaseClientV2Dto = z.object({id: z.string().optional(), serialId: z.number().optional(), firstName: z.string().optional(), lastName: z.string().optional(), fullName: z.string().optional(), companyName: z.string().optional(), description: z.string().optional(), email: z.string().optional(), primaryExt: z.string().optional(), primaryPhone: z.string().optional(), secondaryExt: z.string().optional(), secondaryPhone: z.string().optional(), country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zipcode: z.string().optional(), unit: z.string().optional(), address: z.string().optional(), serviceAddress: z.string().optional(), billingAddress: z.string().optional(), allowBilling: z.boolean().optional(), taxExempt: z.boolean().optional(), taxExemptReason: z.enum(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "99"]).optional()}); export const responseOneClientContactV2Dto = z.object({id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), fullName: z.string().optional(), email: z.string().optional(), primaryPhone: z.string().optional(), secondaryPhone: z.string().optional(), country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zipcode: z.string().optional(), address: z.string().optional(), role: z.string().optional(), note: z.string().optional()}); export const responseOneClientNoteV2Dto = z.object({id: z.string().optional(), note: z.string().optional(), techName: z.string().optional(), createdAt: 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 responseClientV2Dto = z.object({id: z.string().optional(), serialId: z.number().optional(), firstName: z.string().optional(), lastName: z.string().optional(), fullName: z.string().optional(), companyName: z.string().optional(), description: z.string().optional(), email: z.string().optional(), primaryExt: z.string().optional(), primaryPhone: z.string().optional(), secondaryExt: z.string().optional(), secondaryPhone: z.string().optional(), country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zipcode: z.string().optional(), unit: z.string().optional(), address: z.string().optional(), serviceAddress: z.string().optional(), billingAddress: z.string().optional(), allowBilling: z.boolean().optional(), taxExempt: z.boolean().optional(), taxExemptReason: z.enum(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "99"]).optional(), adGroup: responseGenericAdGroupV2Dto.optional(), tags: z.array(responseOneTagV2Dto).optional(), parentClient: responseOneBaseClientV2Dto.optional(), clientContacts: z.array(responseOneClientContactV2Dto).optional(), notes: z.array(responseOneClientNoteV2Dto).optional(), customFields: z.array(responseGenericCustomFieldV2Dto).optional(), created: 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 }; /** * Create a new Client * * POST /v2/clients */ export const createApiV2Clients = async (args: CreateApiV2ClientsArgs, options?: CreateApiV2ClientsOptions) => { 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/clients${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(responseClientV2Dto, await res.json()) };