import {z} from 'zod' export type TimeStamp = number; export type GetApiCustomersCustomerIdDebtsArgs = {customerId: string, startTime?: TimeStamp | undefined, endTime?: TimeStamp | undefined}; export type GetApiCustomersCustomerIdDebtsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const monetaryAmount = z.object({amount: z.number().int().optional(), currency: z.string().optional()}); export const timeStamp = z.number().int(); export const postChargeOffInformation = z.object({balanceAtChargeOff: monetaryAmount.optional(), chargeOffTimestamp: timeStamp.optional(), feesAccruedSinceChargeOff: monetaryAmount.optional(), interestAccruedSinceChargeOff: monetaryAmount.optional(), totalPaidSinceChargeOff: monetaryAmount.optional()}); export const debt = z.object({biller: z.string().optional(), product: z.string().optional(), initialPrincipal: monetaryAmount.optional(), initialInterest: monetaryAmount.optional(), initialFees: monetaryAmount.optional(), postChargeOffInformation: postChargeOffInformation.optional(), transactionId: z.string().max(1024).optional(), transactionIp: z.string().optional(), transactionTimestamp: z.number().int().optional(), defaultTimestamp: timeStamp.optional(), accountOpenTimestamp: timeStamp.optional(), clientFields: z.object({}).optional(), sendCbrNegativeNotice: z.boolean().optional(), hasSentCbrNegativeNotice: z.boolean().optional()}); export const getApiCustomersCustomerIdDebtsResponse = z.object({debts: z.array(debt).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 debts by customer * * GET /customers/{customerId}/debts/ */ export const getApiCustomersCustomerIdDebts = async (args: GetApiCustomersCustomerIdDebtsArgs, options?: GetApiCustomersCustomerIdDebtsOptions) => { const {baseUrl = "https://api.trueaccord.com/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customerId))}/debts/${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(getApiCustomersCustomerIdDebtsResponse, await res.json()) };