import {z} from 'zod' export type ChargeStatus = 'OUTSTANDING' | 'PAID' | 'EXTERNAL_SETTLEMENT' | 'VOID' | 'WRITE_OFF' | 'REFUNDED' | 'CHARGEBACK' | 'PAYMENT_PLAN' | 'COLLECTIONS'; export type GetApiBillingChargeTableArgs = {TENANT: string, 'API-KEY': string, page?: (number | null) | undefined, per_page?: (number | null) | undefined, sort_by?: string | undefined, sort_direction?: string | undefined, location_id?: (string | null) | undefined, id?: (string | null) | undefined, patient_id?: (string | null) | undefined, name?: (string | null) | undefined, total?: (Array | null) | undefined, total_outstanding?: (Array | null) | undefined, description?: (string | null) | undefined, status?: (Array | null) | undefined, external_id?: (string | null) | undefined, created_date?: (Array | string | null) | undefined, payment_date?: (Array | string | null) | undefined, tz?: (string | null) | undefined}; export type GetApiBillingChargeTableOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const chargeStatus = z.enum(["OUTSTANDING", "PAID", "EXTERNAL_SETTLEMENT", "VOID", "WRITE_OFF", "REFUNDED", "CHARGEBACK", "PAYMENT_PLAN", "COLLECTIONS"]); export const userType = z.enum(["PROVIDER", "PATIENT", "ASSISTANT"]); export const patientTagSummary = z.object({id: z.string(), name: z.string(), emoji: z.string().nullable().optional(), color: z.string().nullable().optional(), isActive: z.boolean(), createdDate: z.string(), updatedDate: z.string().nullable().optional()}); export const patientSummary = z.object({id: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), email: z.string().nullable().optional(), type: userType, locationId: z.string().nullable().optional(), externalId: z.string().nullable().optional(), address: z.string().nullable().optional(), addressLineTwo: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), zipCode: z.string().nullable().optional(), country: z.string().nullable().optional(), createdDate: z.string(), addressValid: z.boolean().nullable().optional(), meta: z.object({}).nullable().optional(), isArchived: z.boolean().nullable().optional(), primaryLocationId: z.string().nullable().optional(), gender: z.string().nullable().optional(), dateOfBirth: z.string().nullable().optional(), patientMedications: z.array(z.string()).nullable().optional(), onSchedulingBlacklist: z.boolean().nullable().optional(), surchargeDisabled: z.boolean().nullable().optional(), tags: z.array(patientTagSummary).nullable().optional(), creditBalance: z.number().int().nullable().optional(), preferredProviderId: z.string().nullable().optional()}); export const userTiny = z.object({id: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), email: z.string().nullable().optional(), type: userType, locationId: z.string().nullable().optional()}); export const adjustmentType = z.enum(["WRITE_OFF", "ADJUSTMENT", "EXTERNAL_SETTLEMENT"]); export const adjustmentSummary = z.object({chargeId: z.string(), providerId: z.string().nullable().optional(), locationId: z.string().nullable().optional(), id: z.string(), amount: z.number().int(), reason: z.string().nullable().optional(), createdDate: z.string(), adjustmentType: adjustmentType}); export const paymentStatus = z.enum(["CANCELED", "CREATED", "FAILED", "IN_REVIEW", "PRESENTING", "PROCESSING", "SUCCEEDED"]); export const paymentMethodType = z.enum(["CARD", "ACH", "APPLE_PAY"]); export const paymentMethodSummary = z.object({id: z.string(), patientId: z.string().nullable(), rainforestPaymentMethodId: z.string(), brand: z.string().nullable(), last4: z.string().nullable(), expMonth: z.number().int().nullable(), expYear: z.number().int().nullable(), accountHolderType: z.string().nullable(), accountNumberLast4: z.number().int().nullable(), bankName: z.string().nullable(), routingNumber: z.number().int().nullable(), description: z.string().nullable(), type: paymentMethodType.nullable(), isDefault: z.boolean().optional(), isArchived: z.boolean().optional()}); export const refundReason = z.enum(["DUPLICATE", "DISPUTE", "BOOKING_FEE", "OTHER"]); export const tableRefundOutput = z.object({id: z.string(), amount: z.number().int(), reason: refundReason, createdDate: z.string(), description: z.string().nullable().optional(), creator: userTiny.nullable().optional()}); export const chargeTablePaymentOutput = z.object({id: z.string(), amount: z.number().int(), status: paymentStatus, createdDate: z.string(), paymentMedium: z.string(), paymentMethod: paymentMethodSummary.nullable().optional(), refunds: z.array(tableRefundOutput)}); export const plannedPaymentStatus = z.enum(["SCHEDULED", "COMPLETED", "FAILED", "CANCELLED"]); export const chargeTablePlannedPayment = z.object({id: z.string(), amount: z.number().int(), status: plannedPaymentStatus, paymentDate: z.string()}); export const chargeItem = z.object({id: z.string().nullable().optional(), itemId: z.string().nullable().optional(), chargeId: z.string(), name: z.string().nullable().optional(), description: z.string().nullable().optional(), quantity: z.number().int(), discountAmount: z.number().int().nullable().optional(), discountPercentage: z.number().nullable().optional(), discountReason: z.string().nullable().optional(), price: z.number().int(), soldPackageId: z.string().nullable().optional(), bankedQuantity: z.number().int().nullable().optional(), bankedItemId: z.string().nullable().optional(), issuedDiscountId: z.string().nullable().optional(), soldByProviderId: z.string().nullable().optional(), taxAmount: z.number().int().nullable().optional(), pricingId: z.string().nullable().optional(), shipmentIds: z.array(z.string()).nullable().optional()}); export const providerTiny = z.object({id: z.string(), firstName: z.string(), lastName: z.string().nullable().optional(), email: z.string().nullable().optional(), createdDate: z.string().nullable().optional()}); export const tipDetail = z.object({chargeId: z.string(), amount: z.number().int(), providerId: z.string(), id: z.string().optional(), provider: providerTiny.nullable().optional()}); export const chargeTableOutput = z.object({id: z.string(), total: z.number().int(), totalOutstanding: z.number().int(), description: z.string().nullable().optional(), status: chargeStatus, patient: patientSummary, ruleSetId: z.string().nullable().optional(), externalCreatedDate: z.string().nullable().optional(), createdDate: z.string(), externalId: z.string().nullable().optional(), creator: userTiny.nullable().optional(), adjustments: z.array(adjustmentSummary), payments: z.array(chargeTablePaymentOutput), plannedPayments: z.array(chargeTablePlannedPayment), comment: z.string().nullable().optional(), items: z.array(chargeItem), locationId: z.string().nullable().optional(), tips: z.array(tipDetail).optional()}); export const pageChargeTable = z.object({data: z.array(chargeTableOutput), total: z.number().int()}); 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 Charges Table * * GET /billing/charge/table */ export const getApiBillingChargeTable = async (args: GetApiBillingChargeTableArgs, options: GetApiBillingChargeTableOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.location_id != null) query.append("location_id", String(args.location_id)) if (args.id != null) query.append("id", String(args.id)) if (args.patient_id != null) query.append("patient_id", String(args.patient_id)) if (args.name != null) query.append("name", String(args.name)) for (const item of args.total ?? []) query.append("total", String(item)) for (const item of args.total_outstanding ?? []) query.append("total_outstanding", String(item)) if (args.description != null) query.append("description", String(args.description)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.external_id != null) query.append("external_id", String(args.external_id)) if (args.created_date != null) query.append("created_date", String(args.created_date)) if (args.payment_date != null) query.append("payment_date", String(args.payment_date)) if (args.tz != null) query.append("tz", String(args.tz)) const url = `${baseUrl.replace(/\/+$/, '')}/billing/charge/table${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(pageChargeTable, await res.json()) };