import {z} from 'zod' export type MonetaryAmount = {amount?: number | undefined, currency?: string | undefined}; export type TimeStamp = number; export type PostChargeOffInformation = {balanceAtChargeOff?: MonetaryAmount | undefined, chargeOffTimestamp?: TimeStamp | undefined, feesAccruedSinceChargeOff?: MonetaryAmount | undefined, interestAccruedSinceChargeOff?: MonetaryAmount | undefined, totalPaidSinceChargeOff?: MonetaryAmount | undefined}; export type Debt = {biller?: string | undefined, product?: string | undefined, initialPrincipal?: MonetaryAmount | undefined, initialInterest?: MonetaryAmount | undefined, initialFees?: MonetaryAmount | undefined, postChargeOffInformation?: PostChargeOffInformation | undefined, transactionId?: string | undefined, transactionIp?: string | undefined, transactionTimestamp?: number | undefined, defaultTimestamp?: TimeStamp | undefined, accountOpenTimestamp?: TimeStamp | undefined, clientFields?: Record | undefined, sendCbrNegativeNotice?: boolean | undefined, hasSentCbrNegativeNotice?: boolean | undefined}; export type CreateApiCustomersCustomerIdDebtsArgs = {body: Debt}; export type CreateApiCustomersCustomerIdDebtsOptions = {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 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 }; /** * Add one debt to customer * * POST /customers/{customerId}/debts/ */ export const createApiCustomersCustomerIdDebts = async (args: CreateApiCustomersCustomerIdDebtsArgs, options?: CreateApiCustomersCustomerIdDebtsOptions) => { const {baseUrl = "https://api.trueaccord.com/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customerId))}/debts/${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(debt, await res.json()) };