import {z} from 'zod' export type PaymentTypeEnum = 'INGRESO' | 'EGRESO'; export type PaymentVerificationStatusEnum = 'por_confirmar' | 'confirmado' | 'rechazado' | 'anulado'; export type PaymentCreateDocument = {tipo_pagable: string, id_pagable: number, fecha_emision: string | null, monto: number | string, tipo_pago: PaymentTypeEnum, condicion_pago_id: number | null, metodo_pago_id: number | null, moneda_id: number | null, establecimiento_id: number | null, referencia: string | null, observaciones?: (string | null) | undefined, estado_constancia?: (PaymentVerificationStatusEnum | null) | undefined, persona_id?: (number | null) | undefined}; export type CreateApiApiV3PaymentsDocumentArgs = {body: PaymentCreateDocument}; export type CreateApiApiV3PaymentsDocumentOptions = {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 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 }; /** * Send Document * * POST /api/v3/payments/document */ export const createApiApiV3PaymentsDocument = async (args: CreateApiApiV3PaymentsDocumentArgs, options: CreateApiApiV3PaymentsDocumentOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/payments/document${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(paymentOut, await res.json()) };