import {z} from 'zod' export type GetApiPaymentsArgs = {status?: string | undefined, method?: string | undefined, clientId?: string | undefined, invoiceId?: string | undefined, fromDate?: string | undefined, toDate?: string | undefined, page?: (number | unknown) | undefined, pageSize?: (number | unknown) | undefined, search?: string | undefined}; export type GetApiPaymentsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentSummaryDto = z.object({id: z.string().optional(), paymentNumber: z.string().optional(), paymentMethod: z.string().optional(), amount: z.union([z.number(), z.string()]).optional(), status: z.string().optional(), refundedAmount: z.union([z.number(), z.string()]).optional(), createdAt: z.string().optional(), clientName: z.string().nullable().optional(), invoiceNumber: z.string().nullable().optional(), cashReceived: z.union([z.number(), z.string()]).nullable().optional(), changeDue: z.union([z.number(), z.string()]).nullable().optional()}); export const paymentListResponseDto = z.object({items: z.array(paymentSummaryDto).optional(), total: z.union([z.number().int(), z.string()]).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 }; /** * Get a paginated list of payments with optional filters * * GET /Payments */ export const getApiPayments = async (args: GetApiPaymentsArgs, options?: GetApiPaymentsOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.method != null) query.append("method", String(args.method)) if (args.clientId != null) query.append("clientId", String(args.clientId)) if (args.invoiceId != null) query.append("invoiceId", String(args.invoiceId)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.search != null) query.append("search", String(args.search)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) 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(paymentListResponseDto, await res.json()) };