import {z} from 'zod' export type GetApiV2PaymentsArgs = {pageSize?: number | undefined, page?: number | undefined, field?: 'date' | undefined, order?: ('ASC' | 'DESC') | undefined, clientId?: string | undefined, status?: (Array<'pending' | 'failed' | 'succeeded'>) | undefined, type?: (Array<'charge' | 'cash' | 'credit' | 'check' | 'refund' | 'refund_offline' | 'bank_transfer' | 'bank_transfer_offline' | 'cash_app' | 'consumer_financing' | 'venmo' | 'zelle' | 'debit_offline' | 'installments' | 'interac' | 'check_deposit'>) | undefined, dateProperty?: 'date' | undefined, dateOperator?: ('prev' | 'next' | 'between') | undefined, date?: string | undefined, Authorization: string}; export type GetApiV2PaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseDetailedPaymentV2Dto = z.object({id: z.string().optional(), date: z.string().optional(), amount: z.number().optional(), tipAmount: z.number().optional(), type: z.enum(["charge", "cash", "credit", "check", "refund", "refund_offline", "bank_transfer", "bank_transfer_offline", "cash_app", "consumer_financing", "venmo", "zelle", "debit_offline", "installments", "interac", "check_deposit"]).optional(), status: z.enum(["pending", "failed", "succeeded"]).optional(), estimateId: z.string().nullable().optional(), confirmationCode: z.string().optional(), collectedById: z.string().nullable().optional(), invoiceId: z.string().nullable().optional(), jobId: z.string().nullable().optional(), netAmount: z.number().optional(), processingFee: z.number().optional(), creditCardServiceFee: z.number().optional(), clientId: z.string().nullable().optional()}); export const responsePaymentsV2Dto = z.object({pageSize: z.number().optional(), page: z.number().optional(), totalResults: z.number().optional(), hasMore: z.boolean().optional(), data: z.array(responseDetailedPaymentV2Dto).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 }; /** * Retrieve a list of Payments * * GET /v2/payments */ export const getApiV2Payments = async (args: GetApiV2PaymentsArgs, options?: GetApiV2PaymentsOptions) => { const {baseUrl = "https://api.workiz.com/crm/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.field != null) query.append("field", String(args.field)) if (args.order != null) query.append("order", String(args.order)) if (args.clientId != null) query.append("clientId", String(args.clientId)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.type ?? []) query.append("type", String(item)) if (args.dateProperty != null) query.append("dateProperty", String(args.dateProperty)) if (args.dateOperator != null) query.append("dateOperator", String(args.dateOperator)) if (args.date != null) query.append("date", String(args.date)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(responsePaymentsV2Dto, await res.json()) };