import {z} from 'zod' export type GetApiAccountingConnectionIdSalesorderIdArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'posted_at' | 'contact_id' | 'account_id' | 'currency' | 'total_amount' | 'shipping_address' | 'billing_address' | 'status' | 'lineitems' | 'sales_channel' | 'organization_id' | 'fees' | 'category_ids' | 'metadata' | 'order_number' | 'location_id' | 'employee_user_id' | 'device_id' | 'fulfillment_type' | 'guest_count' | 'subtotal_amount' | 'tax_amount' | 'discount_amount' | 'tip_amount' | 'service_charge_amount' | 'refunded_amount' | 'closed_at' | 'subscription_id' | 'payments' | 'table_id' | 'paid_amount' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, id: string}; export type GetApiAccountingConnectionIdSalesorderIdOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyAccountingSalesorderShippingAddress = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), region_code: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), country_code: z.string().optional()}); export const propertyAccountingSalesorderBillingAddress = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), region_code: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), country_code: z.string().optional()}); export const propertyAccountingLineitemCategoryIds = z.array(z.string()); export const accountingReference = z.object({id: z.string().optional(), name: z.string().optional()}); export const propertyAccountingLineitemLocations = z.array(accountingReference); export const propertyAccountingLineitemItemVariants = z.array(accountingReference); export const accountingFee = z.object({type: z.enum(["TAX", "DISCOUNT", "PROMOTION", "SHIPPING", "GIFT_WRAP", "COD", "SURCHARGE", "TIP", "SERVICE_CHARGE", "OTHER"]), original_type: z.string().optional(), amount: z.number(), currency: z.string().optional()}); export const propertyAccountingLineitemFees = z.array(accountingFee); export const accountingLineitem = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), refunded_at: z.string().optional(), total_amount: z.number().optional(), refund_amount: z.number().optional(), discount_amount: z.number().optional(), tax_amount: z.number().optional(), item_id: z.string().optional(), unit_amount: z.number().optional(), unit_quantity: z.number().optional(), item_sku: z.string().optional(), item_name: z.string().optional(), item_description: z.string().optional(), notes: z.string().optional(), taxrate_id: z.string().optional(), account_id: z.string().optional(), category_ids: propertyAccountingLineitemCategoryIds.optional(), locations: propertyAccountingLineitemLocations.optional(), item_variants: propertyAccountingLineitemItemVariants.optional(), fees: propertyAccountingLineitemFees.optional(), contact_id: z.string().optional(), project_id: z.string().optional(), exchange_rate: z.number().optional(), is_billable: z.boolean().optional()}); export const propertyAccountingSalesorderLineitems = z.array(accountingLineitem); export const propertyAccountingSalesorderFees = z.array(accountingFee); export const propertyAccountingSalesorderCategoryIds = z.array(z.string()); export const accountingMetadata = z.object({id: z.string().optional(), slug: z.string().optional(), value: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), namespace: z.string().optional(), format: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "MEASUREMENT", "PRICE", "YES_NO", "CURRENCY", "URL"]).optional(), extra_data: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional()}); export const propertyAccountingSalesorderMetadata = z.array(accountingMetadata); export const accountingPaymentReference = z.object({payment_id: z.string().optional(), amount: z.number().optional(), allocated_at: z.string().optional()}); export const propertyAccountingSalesorderPayments = z.array(accountingPaymentReference); export const accountingSalesorder = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), posted_at: z.string().optional(), contact_id: z.string().optional(), account_id: z.string().optional(), currency: z.string().optional(), total_amount: z.number().optional(), shipping_address: propertyAccountingSalesorderShippingAddress.optional(), billing_address: propertyAccountingSalesorderBillingAddress.optional(), status: z.enum(["DRAFT", "VOIDED", "AUTHORIZED", "PAID", "PARTIALLY_PAID", "PARTIALLY_REFUNDED", "REFUNDED", "SUBMITTED", "DELETED", "OPEN", "COMPLETED", "CANCELED"]).optional(), lineitems: propertyAccountingSalesorderLineitems.optional(), sales_channel: z.string().optional(), organization_id: z.string().optional(), fees: propertyAccountingSalesorderFees.optional(), category_ids: propertyAccountingSalesorderCategoryIds.optional(), metadata: propertyAccountingSalesorderMetadata.optional(), order_number: z.string().optional(), location_id: z.string().optional(), employee_user_id: z.string().optional(), device_id: z.string().optional(), fulfillment_type: z.enum(["DINE_IN", "TAKEOUT", "DELIVERY", "PICKUP", "CURBSIDE", "SHIPPING", "DIGITAL", "OTHER"]).optional(), guest_count: z.number().optional(), subtotal_amount: z.number().optional(), tax_amount: z.number().optional(), discount_amount: z.number().optional(), tip_amount: z.number().optional(), service_charge_amount: z.number().optional(), refunded_amount: z.number().optional(), closed_at: z.string().optional(), subscription_id: z.string().optional(), payments: propertyAccountingSalesorderPayments.optional(), table_id: z.string().optional(), paid_amount: z.number().optional(), raw: 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 }; /** * Retrieve a salesorder * * GET /accounting/{connection_id}/salesorder/{id} */ export const getApiAccountingConnectionIdSalesorderId = async (args: GetApiAccountingConnectionIdSalesorderIdArgs, options?: GetApiAccountingConnectionIdSalesorderIdOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.raw != null) query.append("raw", String(args.raw)) if (jwt !== undefined) headers.set("authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/accounting/${encodeURIComponent(String(args.connection_id))}/salesorder/${encodeURIComponent(String(args.id))}${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(accountingSalesorder, await res.json()) };