import {z} from 'zod' export type CreateApiQuickbooksDesktopCustomersArgs = {'Conductor-End-User-Id': string, body: {name: string, isActive?: boolean | undefined, classId?: string | undefined, parentId?: string | undefined, companyName?: string | undefined, salutation?: string | undefined, firstName?: string | undefined, middleName?: string | undefined, lastName?: string | undefined, jobTitle?: string | undefined, billingAddress?: ({line1?: string | undefined, line2?: string | undefined, line3?: string | undefined, line4?: string | undefined, line5?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined, note?: string | undefined}) | undefined, shippingAddress?: ({line1?: string | undefined, line2?: string | undefined, line3?: string | undefined, line4?: string | undefined, line5?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined, note?: string | undefined}) | undefined, alternateShippingAddresses?: (Array<{name: string, line1?: string | undefined, line2?: string | undefined, line3?: string | undefined, line4?: string | undefined, line5?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined, note?: string | undefined, isDefaultShippingAddress?: boolean | undefined}>) | undefined, phone?: string | undefined, alternatePhone?: string | undefined, fax?: string | undefined, email?: string | undefined, ccEmail?: string | undefined, contact?: string | undefined, alternateContact?: string | undefined, customContactFields?: Array<{name: string, value: string}> | undefined, additionalContacts?: (Array<{salutation?: string | undefined, firstName: string, middleName?: string | undefined, lastName?: string | undefined, jobTitle?: string | undefined, customContactFields?: Array<{name: string, value: string}> | undefined}>) | undefined, customerTypeId?: string | undefined, termsId?: string | undefined, salesRepresentativeId?: string | undefined, openingBalance?: string | undefined, openingBalanceDate?: string | undefined, salesTaxCodeId?: string | undefined, salesTaxItemId?: string | undefined, salesTaxCountry?: ('australia' | 'canada' | 'uk' | 'us') | undefined, resaleNumber?: string | undefined, accountNumber?: string | undefined, creditLimit?: string | undefined, preferredPaymentMethodId?: string | undefined, creditCard?: ({number?: string | undefined, expirationMonth?: number | undefined, expirationYear?: number | undefined, name?: string | undefined, address?: string | undefined, postalCode?: string | undefined}) | undefined, jobStatus?: ('awarded' | 'closed' | 'in_progress' | 'none' | 'not_awarded' | 'pending') | undefined, jobStartDate?: string | undefined, jobProjectedEndDate?: string | undefined, jobEndDate?: string | undefined, jobDescription?: string | undefined, jobTypeId?: string | undefined, note?: string | undefined, additionalNotes?: Array<{note: string}> | undefined, preferredDeliveryMethod?: ('email' | 'mail' | 'none') | undefined, priceLevelId?: string | undefined, externalId?: string | undefined, taxRegistrationNumber?: string | undefined, currencyId?: string | undefined}}; export type CreateApiQuickbooksDesktopCustomersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const qbdAddress = z.object({line1: z.string().nullable(), line2: z.string().nullable(), line3: z.string().nullable(), line4: z.string().nullable(), line5: z.string().nullable(), city: z.string().nullable(), state: z.string().nullable(), postalCode: z.string().nullable(), country: z.string().nullable(), note: z.string().nullable()}); export const qbdShippingAddress = z.object({name: z.string().nullable(), line1: z.string().nullable(), line2: z.string().nullable(), line3: z.string().nullable(), line4: z.string().nullable(), line5: z.string().nullable(), city: z.string().nullable(), state: z.string().nullable(), postalCode: z.string().nullable(), country: z.string().nullable(), note: z.string().nullable(), isDefaultShippingAddress: z.boolean().nullable()}); export const qbdCustomContactField = z.object({name: z.string(), value: z.string().nullable()}); export const qbdContact = z.object({id: z.string(), objectType: z.literal("qbd_contact"), createdAt: z.string(), updatedAt: z.string(), revisionNumber: z.string(), name: z.string().nullable(), salutation: z.string().nullable(), firstName: z.string(), middleName: z.string().nullable(), lastName: z.string().nullable(), jobTitle: z.string().nullable(), customContactFields: z.array(qbdCustomContactField)}); export const qbdCreditCard = z.object({number: z.string().nullable(), expirationMonth: z.number().nullable(), expirationYear: z.number().nullable(), name: z.string().nullable(), address: z.string().nullable(), postalCode: z.string().nullable()}); export const qbdNote = z.object({id: z.number(), date: z.string().nullable(), note: z.string()}); export const qbdCustomField = z.object({ownerId: z.string(), name: z.string(), type: z.enum(["amount_type", "date_time_type", "integer_type", "percent_type", "price_type", "quantity_type", "string_1024_type", "string_255_type"]), value: z.string()}); export const qbdCustomer = z.object({id: z.string(), objectType: z.literal("qbd_customer"), createdAt: z.string(), updatedAt: z.string(), revisionNumber: z.string(), name: z.string(), fullName: z.string(), isActive: z.boolean(), class: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), parent: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), sublevel: z.number(), companyName: z.string().nullable(), salutation: z.string().nullable(), firstName: z.string().nullable(), middleName: z.string().nullable(), lastName: z.string().nullable(), jobTitle: z.string().nullable(), billingAddress: qbdAddress.nullable(), shippingAddress: qbdAddress.nullable(), alternateShippingAddresses: z.array(qbdShippingAddress).nullable(), phone: z.string().nullable(), alternatePhone: z.string().nullable(), fax: z.string().nullable(), email: z.string().nullable(), ccEmail: z.string().nullable(), contact: z.string().nullable(), alternateContact: z.string().nullable(), customContactFields: z.array(qbdCustomContactField), additionalContacts: z.array(qbdContact), customerType: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), terms: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), salesRepresentative: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), balance: z.string().nullable(), totalBalance: z.string().nullable(), salesTaxCode: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), salesTaxItem: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), salesTaxCountry: z.string().nullable(), resaleNumber: z.string().nullable(), accountNumber: z.string().nullable(), creditLimit: z.string().nullable(), preferredPaymentMethod: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), creditCard: qbdCreditCard.nullable(), jobStatus: z.enum(["awarded", "closed", "in_progress", "none", "not_awarded", "pending"]).nullable(), jobStartDate: z.string().nullable(), jobProjectedEndDate: z.string().nullable(), jobEndDate: z.string().nullable(), jobDescription: z.string().nullable(), jobType: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), note: z.string().nullable(), additionalNotes: z.array(qbdNote), preferredDeliveryMethod: z.enum(["email", "mail", "none"]).nullable(), priceLevel: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), externalId: z.string().nullable(), taxRegistrationNumber: z.string().nullable(), currency: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), customFields: z.array(qbdCustomField)}); 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 customer * * POST /quickbooks-desktop/customers */ export const createApiQuickbooksDesktopCustomers = async (args: CreateApiQuickbooksDesktopCustomersArgs, options?: CreateApiQuickbooksDesktopCustomersOptions) => { const {baseUrl = "https://api.conductor.is/v1", 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 (args['Conductor-End-User-Id'] != null) headers.set("Conductor-End-User-Id", String(args['Conductor-End-User-Id'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/quickbooks-desktop/customers${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(qbdCustomer, await res.json()) };