import {z} from 'zod' export type GetApiApiV1AdminInvoicesClaimableArgs = {merchant_id: string, period_start: string, period_end: string}; export type GetApiApiV1AdminInvoicesClaimableOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceClaimablePreviewResponse = z.object({success: z.boolean(), data: z.object({transaction_count: z.number(), fee_totals_by_currency: z.array(z.object({currency: z.string().min(3).max(3), total_minor: z.number(), transaction_count: z.number()})), transactions: z.array(z.object({transaction_id: z.string(), transaction_number: z.string(), created_at: z.string(), deposit_amount_minor: z.number(), deposit_currency: z.string().min(3).max(3), fee_total_minor: z.number(), fee_currency: z.string().min(3).max(3).nullable()})), truncated: z.boolean(), conflicts: z.array(z.object({invoice_id: z.string(), invoice_number: z.string(), status: z.union([z.literal("draft"), z.literal("issued"), z.literal("paid"), z.literal("void")]), transaction_count: z.number()}))})}); 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 }; /** * Preview claimable transactions for a (merchant, period) window. Uses the same eligibility predicate as create; surfaces conflicts when other invoices already own rows in the window. Used by the Create-invoice form to validate the window before submit. * * GET /api/v1/admin/invoices/claimable */ export const getApiApiV1AdminInvoicesClaimable = async (args: GetApiApiV1AdminInvoicesClaimableArgs, options?: GetApiApiV1AdminInvoicesClaimableOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchant_id != null) query.append("merchant_id", String(args.merchant_id)) if (args.period_start != null) query.append("period_start", String(args.period_start)) if (args.period_end != null) query.append("period_end", String(args.period_end)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/admin/invoices/claimable${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(invoiceClaimablePreviewResponse, await res.json()) };