import {z} from 'zod' export type Date = {day?: number | undefined, month?: number | undefined, year?: number | undefined}; export type Addresses = Array<{streetLine1?: string | undefined, streetLine2?: string | undefined, city: string, state: string, zipcode: string, countryCode: string, types?: Array | undefined}>; export type Phones = Array<{phoneNumber: string, types?: Array | undefined}>; export type Emails = Array<{email: string, types?: Array | undefined}>; 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 CustomerRequest = {name: {firstName: string, middleName?: string | undefined, lastName: string}, dateOfBirth?: Date | undefined, businesses?: (Array<{name?: string | undefined, url?: string | undefined}>) | undefined, addresses?: Addresses | undefined, phones?: Phones | undefined, emails?: Emails | undefined, comments?: Array<{comment: string}> | undefined, reference?: string | undefined, debts?: Array | undefined, ssn?: string | undefined}; export type Meta = {id?: string | undefined, isActive?: boolean | undefined, isPrimary?: boolean | undefined, timeCreated?: TimeStamp | undefined, lastModified?: TimeStamp | undefined}; export type Customer = {meta?: Meta | undefined, name: {firstName: string, middleName?: string | undefined, lastName: string}, dateOfBirth?: Date | undefined, businesses?: (Array<{name?: string | undefined, url?: string | undefined}>) | undefined, addresses?: Addresses | undefined, phones?: Phones | undefined, emails?: Emails | undefined, comments?: Array<{comment: string}> | undefined, reference?: string | undefined, debts?: Array | undefined, ssn?: string | undefined}; export type CreateApiCustomersBatchArgs = {body: {responses?: Array | undefined, addDebtsIfPossible?: boolean | undefined, customersWithAddedDebts?: Array | undefined}}; export type CreateApiCustomersBatchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timeStamp = z.number().int(); export const meta = z.object({id: z.string().optional(), isActive: z.boolean().optional(), isPrimary: z.boolean().optional(), timeCreated: timeStamp.optional(), lastModified: timeStamp.optional()}); export const date = z.object({day: z.number().int().optional(), month: z.number().int().optional(), year: z.number().int().optional()}); export const addresses = z.array(z.object({streetLine1: z.string().optional(), streetLine2: z.string().optional(), city: z.string(), state: z.string(), zipcode: z.string(), countryCode: z.string(), types: z.array(z.string()).optional()})); export const phones = z.array(z.object({phoneNumber: z.string(), types: z.array(z.string()).optional()})); export const emails = z.array(z.object({email: z.string(), types: z.array(z.string()).optional()})); export const monetaryAmount = z.object({amount: z.number().int().optional(), currency: z.string().optional()}); 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 customer = z.object({meta: meta.optional(), name: z.object({firstName: z.string(), middleName: z.string().optional(), lastName: z.string()}), dateOfBirth: date.optional(), businesses: z.array(z.object({name: z.string().optional(), url: z.string().optional()})).optional(), addresses: addresses.optional(), phones: phones.optional(), emails: emails.optional(), comments: z.array(z.object({comment: z.string()})).optional(), reference: z.string().optional(), debts: z.array(debt).optional(), ssn: z.string().optional()}); export const customersBatchError = z.object({isError: z.boolean().optional(), error: z.object({errorCode: z.string().optional(), message: z.string().optional(), reference: z.string().optional(), transactionId: z.string().optional()}).optional()}); export const createApiCustomersBatchResponse = z.object({responses: z.array(z.union([z.object({isAddedDebt: z.boolean().optional(), customer: customer.optional()}), customersBatchError])).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 }; /** * Create customers (recommended method) * * POST /customersBatch */ export const createApiCustomersBatch = async (args: CreateApiCustomersBatchArgs, options?: CreateApiCustomersBatchOptions) => { 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(/\/+$/, '')}/customersBatch${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(createApiCustomersBatchResponse, await res.json()) };