import {z} from 'zod' export type GetApiQuickbooksDesktopPreferencesArgs = {'Conductor-End-User-Id': string}; export type GetApiQuickbooksDesktopPreferencesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const qbdAccountingPreferences = z.object({isUsingAccountNumbers: z.boolean(), isRequiringAccounts: z.boolean(), isUsingClassTracking: z.boolean(), defaultTransactionClass: z.enum(["accounts", "items", "names", "none"]).nullable(), isUsingAuditTrail: z.boolean(), isAssigningJournalEntryNumbers: z.boolean(), closingDate: z.string().nullable()}); export const qbdFinanceChargePreferences = z.object({annualInterestRate: z.number().nullable(), minimumFinanceCharge: z.number().nullable(), gracePeriod: z.number(), financeChargeAccount: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), isAssessingForOverdueCharges: z.boolean(), calculateChargesFrom: z.enum(["due_date", "invoice_or_billed_date"]), isMarkedToBePrinted: z.boolean()}); export const qbdJobsAndEstimatesPreferences = z.object({isUsingEstimates: z.boolean(), isUsingProgressInvoicing: z.boolean(), isPrintingItemsWithZeroAmounts: z.boolean()}); export const qbdMultiCurrencyPreferences = z.object({isMultiCurrencyEnabled: z.boolean().nullable(), homeCurrency: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable()}); export const qbdMultiLocationInventoryPreferences = z.object({isMultiLocationInventoryAvailable: z.boolean().nullable(), isMultiLocationInventoryEnabled: z.boolean().nullable()}); export const qbdPurchasesAndVendorsPreferences = z.object({isUsingInventory: z.boolean(), daysBillsAreDue: z.number(), isAutomaticallyUsingDiscounts: z.boolean(), defaultDiscountAccount: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable()}); export const qbdReportsPreferences = z.object({agingReportBasis: z.enum(["age_from_due_date", "age_from_transaction_date"]), summaryReportBasis: z.enum(["accrual", "cash"])}); export const qbdPriceLevels = z.object({isUsingPriceLevels: z.boolean(), isRoundingSalesPriceUp: z.boolean().nullable()}); export const qbdSalesAndCustomersPreferences = z.object({defaultShippingMethod: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), defaultShipmentOrigin: z.string().nullable(), defaultMarkupPercentage: z.string().nullable(), isTrackingReimbursedExpensesAsIncome: z.boolean(), isAutoApplyingPayments: z.boolean(), priceLevels: qbdPriceLevels.nullable()}); export const qbdSalesTaxPreferences = z.object({defaultItemSalesTax: z.object({id: z.string().nullable(), fullName: z.string().nullable()}).nullable(), salesTaxReportingFrequency: z.enum(["monthly", "quarterly", "annually"]), defaultTaxableSalesTaxCode: z.object({id: z.string().nullable(), fullName: z.string().nullable()}), defaultNonTaxableSalesTaxCode: z.object({id: z.string().nullable(), fullName: z.string().nullable()}), isUsingVendorTaxCode: z.boolean().nullable(), isUsingCustomerTaxCode: z.boolean().nullable(), isUsingTaxInclusivePrices: z.boolean().nullable()}); export const qbdTimeTrackingPreferences = z.object({firstDayOfWeek: z.enum(["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"])}); export const qbdCurrentAppAccessRights = z.object({isAutomaticLoginAllowed: z.boolean(), automaticLoginUserName: z.string().nullable(), isPersonalDataAccessAllowed: z.boolean()}); export const qbdItemsAndInventoryPreferences = z.object({isEnhancedInventoryReceivingEnabled: z.boolean().nullable(), inventoryTrackingMethod: z.enum(["none", "serial_number", "lot_number"]).nullable(), isInventoryExpirationDateEnabled: z.boolean().nullable(), isTrackingOnSalesTransactionsEnabled: z.boolean().nullable(), isTrackingOnPurchaseTransactionsEnabled: z.boolean().nullable(), isTrackingOnInventoryAdjustmentEnabled: z.boolean().nullable(), isTrackingOnBuildAssemblyEnabled: z.boolean().nullable(), isFifoEnabled: z.boolean().nullable(), fifoEffectiveDate: z.string().nullable(), isBinTrackingEnabled: z.boolean().nullable(), isBarcodeEnabled: z.boolean().nullable()}); export const qbdPreferences = z.object({accounting: qbdAccountingPreferences, financeCharges: qbdFinanceChargePreferences, jobsAndEstimates: qbdJobsAndEstimatesPreferences, multiCurrency: qbdMultiCurrencyPreferences.nullable(), multiLocationInventory: qbdMultiLocationInventoryPreferences.nullable(), purchasesAndVendors: qbdPurchasesAndVendorsPreferences, reports: qbdReportsPreferences, salesAndCustomers: qbdSalesAndCustomersPreferences, salesTax: qbdSalesTaxPreferences.nullable(), timeTracking: qbdTimeTrackingPreferences.nullable(), appAccessRights: qbdCurrentAppAccessRights, itemsAndInventory: qbdItemsAndInventoryPreferences.nullable()}); 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 company file preferences * * GET /quickbooks-desktop/preferences */ export const getApiQuickbooksDesktopPreferences = async (args: GetApiQuickbooksDesktopPreferencesArgs, options?: GetApiQuickbooksDesktopPreferencesOptions) => { const {baseUrl = "https://api.conductor.is/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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/preferences${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(qbdPreferences, await res.json()) };