import {z} from 'zod' export type UpdateApiQuoteIdArgs = {body: {client_id: number, organization_id: number, term_id: number, quote_reference_number?: (string | null) | undefined, bill_to?: (string | null) | undefined, date: string, due_date: string, purchase_order?: (string | null) | undefined, total: number, description?: (string | null) | undefined, note?: (string | null) | undefined, display_tax?: (boolean | null) | undefined, display_discount?: ('no' | 'item_level' | 'invoice_level_after_tax' | 'invoice_level_before_tax' | 'both_item_after_level' | 'both_item_before_level' | null) | undefined, discount_amount?: (number | null) | undefined, discount_percentage?: (number | null) | undefined, discount_type?: ('percentage' | 'amount' | null) | undefined, discount_id?: (number | null) | undefined, display_item_date?: (boolean | null) | undefined, paid_status: 'open' | 'draft', related_cases?: (Array | null) | undefined, related_matters?: (Array | null) | undefined, related_contracts?: (Array | null) | undefined, notify_me_before?: ({time?: number | undefined, time_type?: ('day' | 'week' | 'month') | undefined, type?: ('popup' | 'popup_email') | undefined} | null) | undefined, items?: (Array<{item_id: number, account_id: number, item_title?: string | undefined, description: string, unit_price: number, quantity: number, item_date?: (string | null) | undefined, discount_id?: (number | null) | undefined, discount_amount?: (number | null) | undefined, discount_percentage?: (number | null) | undefined, total: number, sub_total: number, total_tax: number}>) | undefined, expenses?: (Array<{expense_id: number, account_id: number, item_title?: string | undefined, description: string, unit_price: number, quantity: number, total: number, sub_total: number, total_tax: number}> | null) | undefined, time_logs?: (Array<{account_id: number, item_title?: string | undefined, description: string, unit_price: number, quantity: number, total: number, sub_total: number, total_tax: number}> | null) | undefined, bills?: (Array<{unit_price: number, quantity: number, item_title: string, description: string, tax_id?: (number | null) | undefined, bill_item_id?: (number | null) | undefined, tax_percentage?: number | undefined, is_fixed_rate?: boolean | undefined, matter_id?: number | undefined, item_date?: string | undefined, discount_type?: (string | null) | undefined, discount_percentage?: number | undefined, discount_id?: (number | null) | undefined, discount_amount?: number | undefined, sub_total: number, total_tax: number, total: number, account_id: number}> | null) | undefined}}; export type UpdateApiQuoteIdOptions = {baseUrl?: string | undefined, passport?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiQuoteIdResponse = z.object({message: z.string().optional(), quote: z.object({voucher_header_id: z.number().int().optional(), displayTax: z.boolean().optional(), displayDiscount: z.string().optional(), display_item_date: z.boolean().optional(), notes: z.string().optional(), description: z.string().optional(), paidStatus: z.string().optional(), account_id: z.string().optional(), quoteDate: z.string().optional(), billTo: z.string().optional(), term_id: z.string().optional(), prefix: z.string().optional(), suffix: z.string().optional(), groupTimeLogsByUserInExport: z.string().optional(), dueOn: z.string().optional(), purchaseOrder: z.string().optional(), quoteNumber: z.string().optional(), discount_amount: z.number().optional(), discount_percentage: z.number().optional(), lines_total_subtotal: z.number().optional(), lines_total_discount: z.number().optional(), lines_total_tax: z.number().optional(), total: z.number().optional(), id: z.number().int().optional()}).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 }; /** * Update Quote * * PUT /quote/{id}/ */ export const updateApiQuoteId = async (args: UpdateApiQuoteIdArgs, options?: UpdateApiQuoteIdOptions) => { const {baseUrl = "http://localhost/app4legal/api/v2/money", fetch: fetchFn = fetch, passport} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (passport !== undefined) headers.set('authorization', `Bearer ${passport}`) const url = `${baseUrl.replace(/\/+$/, '')}/quote/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiQuoteIdResponse, await res.json()) };