import {z} from 'zod' export type DX = {icd10_code: string, icd9_codes?: Array | undefined}; export type NdcMeasureEnum = 'GR' | 'F2' | 'ME' | 'ML' | 'UN'; export type NullEnum = unknown; export type CPT = {cpt: string, dxs: Array, modifier_1?: (string | null) | undefined, modifier_2?: (string | null) | undefined, modifier_3?: (string | null) | undefined, modifier_4?: (string | null) | undefined, ndc?: (string | null) | undefined, ndc_dose?: (unknown | null) | undefined, ndc_measure?: (NdcMeasureEnum | NullEnum) | undefined, unit_charge: string, units: string}; export type ReferringProvider = {name: string, npi?: (string | null) | undefined, state: string}; export type BillSingleInstance = {billing_provider?: (number | null) | undefined, cpts: Array, id?: number | undefined, notes?: string | undefined, ordering_provider?: ReferringProvider | undefined, patient: number, payment?: number | undefined, payment_amount?: (unknown | null) | undefined, physician: number, practice: number, prior_authorization?: (string | null) | undefined, referring_provider?: ReferringProvider | undefined, rendering_provider?: (number | null) | undefined, service_location: number, show_dual_coding?: (boolean | null) | undefined, supervising_provider?: (number | null) | undefined, visit_note: number}; export type CreateApiApi20BillsArgs = {body: BillSingleInstance}; export type CreateApiApi20BillsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const dX = z.object({icd10_code: z.string(), icd9_codes: z.array(z.string()).optional()}); export const ndcMeasureEnum = z.enum(["GR", "F2", "ME", "ML", "UN"]); export const nullEnum = z.unknown(); export const cPT = z.object({cpt: z.string(), dxs: z.array(dX), modifier_1: z.string().max(2).nullable().optional(), modifier_2: z.string().max(2).nullable().optional(), modifier_3: z.string().max(2).nullable().optional(), modifier_4: z.string().max(2).nullable().optional(), ndc: z.string().max(11).nullable().optional(), ndc_dose: z.string().nullable().optional(), ndc_measure: z.union([ndcMeasureEnum, nullEnum]).optional(), unit_charge: z.string(), units: z.string()}); export const referringProvider = z.object({name: z.string(), npi: z.string().max(10).nullable().optional(), state: z.string()}); export const billSingleInstance = z.object({billing_provider: z.number().int().nullable().optional(), cpts: z.array(cPT), id: z.number().int().optional(), notes: z.string().max(1000).optional(), ordering_provider: referringProvider.optional(), patient: z.number().int(), payment: z.number().int().optional(), payment_amount: z.string().nullable().optional(), physician: z.number().int(), practice: z.number().int(), prior_authorization: z.string().max(50).nullable().optional(), referring_provider: referringProvider.optional(), rendering_provider: z.number().int().nullable().optional(), service_location: z.number().int(), show_dual_coding: z.boolean().nullable().optional(), supervising_provider: z.number().int().nullable().optional(), visit_note: 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 }; /** * Create Bill * * POST /api/2.0/bills/ */ export const createApiApi20Bills = async (args: CreateApiApi20BillsArgs, options?: CreateApiApi20BillsOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/bills/${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(billSingleInstance, await res.json()) };