import {z} from 'zod' export type PurchaseOrderCreate = {project_id: string, po_number: string, po_document_r2_key?: (string | null) | undefined, shipping_address?: (string | null) | undefined, shipping_address_id?: (string | null) | undefined, tax_calculation_id?: (string | null) | undefined, tax_amount?: (number | null) | undefined, total_with_tax?: (number | string | null) | undefined}; export type CreateApiApiV1PaymentsPurchaseOrderArgs = {body: PurchaseOrderCreate}; export type CreateApiApiV1PaymentsPurchaseOrderOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const commercialDocumentExtractionSummary = z.object({id: z.string(), status: z.string(), provider: z.string(), schema_version: z.string(), confidence: z.number().nullable().optional(), aggregate_total_cents: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), max_lead_time_days: z.number().int().nullable().optional(), error_message: z.string().nullable().optional(), started_at: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const paymentResponse = z.object({id: z.string(), project_id: z.string(), quote_id: z.string(), method: z.string(), status: z.string(), amount: z.string(), deposit_amount: z.string().nullable().optional(), currency: z.string(), po_number: z.string().nullable().optional(), shipping_address: z.string().nullable().optional(), tax_amount: z.number().int().nullable().optional(), total_with_tax: z.string().nullable().optional(), notes: z.string().nullable().optional(), paid_at: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), submitted_by_admin_id: z.string().nullable().optional(), submitted_by_label: z.string().nullable().optional(), po_document_extraction: commercialDocumentExtractionSummary.nullable().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 }; /** * Submit Purchase Order * * POST /api/v1/payments/purchase-order */ export const createApiApiV1PaymentsPurchaseOrder = async (args: CreateApiApiV1PaymentsPurchaseOrderArgs, options: CreateApiApiV1PaymentsPurchaseOrderOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payments/purchase-order${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(paymentResponse, await res.json()) };