import {z} from 'zod' export type PaymentTypeEnum = 'INGRESO' | 'EGRESO'; export type GetApiPaymentsArgs = {payment_type?: (PaymentTypeEnum | null) | undefined, 'X-Company-Token': string}; export type GetApiPaymentsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentStatusEnum = z.enum(["active", "partial_applied", "fully_applied", "voided"]); export const paymentTypeEnum = z.enum(["INGRESO", "EGRESO"]); export const paymentAttachmentOutOutput = z.object({nombre: z.string(), url: z.string(), ruta: z.string().nullable().optional(), tamano: z.number().int().nullable().optional(), content_type: z.string().nullable().optional(), extension: z.string().nullable().optional()}); export const paymentVerificationStatusEnum = z.enum(["por_confirmar", "confirmado", "rechazado", "anulado"]); export const paymentItemTypeEnum = z.enum(["manual", "purchase_order_transfer", "purchase_transfer", "document_transfer"]); export const paymentItemOut = z.object({id: z.number().int(), payment_id: z.number().int(), orden_compra_id: z.number().int().nullable().optional(), compra_id: z.number().int().nullable().optional(), venta_id: z.number().int().nullable().optional(), orden_id: z.number().int().nullable().optional(), payable_id: z.number().int().nullable().optional(), payable_type: z.string().nullable().optional(), tipo: paymentItemTypeEnum, total_a_pagar: z.string(), total_pagado: z.string(), estado: paymentStatusEnum, nota: z.string().nullable().optional()}); export const paymentOut = z.object({id: z.number().int(), fecha_emision: z.string().nullable(), total: z.string(), estado: paymentStatusEnum, tipo_pago: paymentTypeEnum.nullable().optional(), condicion_pago_id: z.number().int().nullable().optional(), metodo_pago_id: z.number().int().nullable().optional(), moneda_id: z.number().int().nullable(), tipo_cambio: z.string().nullable().optional(), referencia: z.string().nullable().optional(), observaciones: z.string().nullable().optional(), serie: z.string().nullable().optional(), numero: z.number().int().nullable().optional(), persona_id: z.number().int().nullable().optional(), persona: z.record(z.string(), z.unknown()).nullable().optional(), cliente: z.record(z.string(), z.unknown()).nullable().optional(), proveedor: z.record(z.string(), z.unknown()).nullable().optional(), documento_relacionado: z.string().nullable().optional(), adjunto: paymentAttachmentOutOutput.nullable().optional(), estado_constancia: paymentVerificationStatusEnum.nullable().optional(), items: z.array(paymentItemOut).optional()}); export const getApiPaymentsResponse = z.array(paymentOut); 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 }; /** * List Payments * * GET /payments */ export const getApiPayments = async (args: GetApiPaymentsArgs, options: GetApiPaymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.payment_type != null) query.append("payment_type", String(args.payment_type)) if (args['X-Company-Token'] != null) headers.set("X-Company-Token", String(args['X-Company-Token'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments${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(getApiPaymentsResponse, await res.json()) };