import {z} from 'zod' export type LabOrderAOEAnswer = {created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, id?: number | undefined, question: number, test?: (number | null) | undefined, value?: (string | null) | undefined}; export type BillTypeEnum = 'thirdparty' | 'patient' | 'client' | 'workerscomp' | 'specialbilling'; export type NullEnum = unknown; export type FastingMethodEnum = 'fasting_12_hour' | 'fasting_8_hours' | 'fasting_2_hours' | 'fasting_none' | 'fasting_random'; export type BlankEnum = ''; export type ICD10Code = {code: string, description?: (string | null) | undefined}; export type StatMethodEnum = 'wet_reading_phone' | 'wet_reading_fax' | 'stat_phone' | 'stat_fax' | 'stat_phone_fax'; export type LabOrderAOEQuestionChoice = {code: string, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, id?: number | undefined, value: string}; export type QuestionTypeEnum = 'enum' | 'enum_multiselect' | 'freetext' | 'y_or_n' | 'numeric' | 'yyyymmdd' | '2_numeric_1_decimal'; export type LabOrderAOEQuestion = {choices: Array, code: string, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, example_format?: (string | null) | undefined, id?: number | undefined, max_length?: (number | null) | undefined, question_type?: QuestionTypeEnum | undefined, secondary_code?: (string | null) | undefined, value: string}; export type LabOrderTestToLabOrderAOEQuestion = {question: LabOrderAOEQuestion, required?: boolean | undefined, sequence: number, test?: number | undefined}; export type LabOrderTestWithDiagnoses = {code?: (string | null) | undefined, compendium?: (number | null) | undefined, cpts?: Array | undefined, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, icd10_codes: Array, id?: number | undefined, lab_vendor?: (number | null) | undefined, name: string, practice_created?: (number | null) | undefined, procedure_class?: (string | null) | undefined, questions?: Array | undefined, synonyms?: Array | undefined}; export type LabOrderContent = {collection_datetime?: (string | null) | undefined, fasting_method?: (FastingMethodEnum | BlankEnum | NullEnum) | undefined, icd10_codes?: Array | undefined, id?: number | undefined, patient_instructions?: string | undefined, standing_order_end_date?: (string | null) | undefined, standing_order_frequency?: (string | null) | undefined, stat_method?: (StatMethodEnum | BlankEnum | NullEnum) | undefined, test_center_notes?: string | undefined, tests?: Array | undefined}; export type BiDiLabConnection = {alias?: (string | null) | undefined, enabled?: boolean | undefined, id?: number | undefined, priority?: number | undefined, protocol?: (string | null) | undefined}; export type FacilityIdentifier = {active: boolean, bidi_connection?: (BiDiLabConnection | null) | undefined, compendium?: (number | null) | undefined, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, description?: (string | null) | undefined, id?: number | undefined, lab_connection?: (number | null) | undefined, lab_facility: string, practice: number, practice_facility: string, practice_secondary_id?: (string | null) | undefined, user_facing?: boolean | undefined, vendor: {display_name?: string | undefined, id?: number | undefined, name?: string | undefined}}; export type StateEnum = 'outstanding' | 'fulfilled' | 'cancelled'; export type DocumentResolutionState = {created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, document?: number | undefined, id?: number | undefined, note?: (string | null) | undefined, resolving_document?: (number | null) | undefined, state: StateEnum}; export type LabVendorIdName = {display_name?: string | undefined, id?: number | undefined, name?: string | undefined}; export type LabVendorPatientSite = {address?: string | undefined, city?: string | undefined, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, fax?: (string | null) | undefined, id?: number | undefined, name: string, phone?: (string | null) | undefined, practice_created?: (number | null) | undefined, state?: string | undefined, suite?: (string | null) | undefined, vendor?: LabVendorIdName | undefined, zip?: string | undefined}; export type PracticeSpecialInsuranceCodeName = {code: string, name: string}; export type BodySite = {code: string, deleted_date?: (string | null) | undefined, text: string}; export type BodySiteCodeModifier = {code: string, deleted_date?: (string | null) | undefined, text: string}; export type LabOrderSpecimenModifier = {deleted_date?: (string | null) | undefined, modifier: BodySiteCodeModifier, sequence: number, specimen: number}; export type LabOrderSpecimen = {body_site: BodySite, container_id: string, description?: (string | null) | undefined, id?: number | undefined, lab_order?: number | undefined, modifiers: Array, test: number}; export type LabOrderSubmission = {id?: number | undefined, lab_order?: number | undefined, state?: string | undefined, state_metadata?: (string | null) | undefined, time_acknowledged?: (string | null) | undefined, time_submitted: string}; export type LabOrder = {answers?: (Array | null) | undefined, bill_type?: (BillTypeEnum | NullEnum) | undefined, ccs?: (Array) | undefined, chart_date?: string | undefined, confidential?: boolean | undefined, content?: LabOrderContent | undefined, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, document_date: string, facility?: (FacilityIdentifier | null) | undefined, follow_up_method?: (string | null) | undefined, id?: number | undefined, ordering_physician: number, patient: number, practice: number, printable_view?: string | undefined, requisition?: number | undefined, resolution?: (DocumentResolutionState | null) | undefined, signed_by?: (number | null) | undefined, signed_date?: (string | null) | undefined, site?: (LabVendorPatientSite | null) | undefined, special_insurance?: (PracticeSpecialInsuranceCodeName | null) | undefined, specimens?: (Array | null) | undefined, submissions?: (Array | null) | undefined, tags?: (Array) | undefined, test_date?: (string | null) | undefined, vendor?: (number | null) | undefined}; export type CreateApiApi20LabOrdersArgs = {body: LabOrder}; export type CreateApiApi20LabOrdersOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const labOrderAOEAnswer = z.object({created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), id: z.number().int().optional(), question: z.number().int(), test: z.number().int().nullable().optional(), value: z.string().nullable().optional()}); export const billTypeEnum = z.enum(["thirdparty", "patient", "client", "workerscomp", "specialbilling"]); export const nullEnum = z.unknown(); export const fastingMethodEnum = z.enum(["fasting_12_hour", "fasting_8_hours", "fasting_2_hours", "fasting_none", "fasting_random"]); export const blankEnum = z.literal(""); export const iCD10Code = z.object({code: z.string().max(50), description: z.string().max(500).nullable().optional()}); export const statMethodEnum = z.enum(["wet_reading_phone", "wet_reading_fax", "stat_phone", "stat_fax", "stat_phone_fax"]); export const labOrderAOEQuestionChoice = z.object({code: z.string().max(100), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), id: z.number().int().optional(), value: z.string()}); export const questionTypeEnum = z.enum(["enum", "enum_multiselect", "freetext", "y_or_n", "numeric", "yyyymmdd", "2_numeric_1_decimal"]); export const labOrderAOEQuestion = z.object({choices: z.array(labOrderAOEQuestionChoice), code: z.string().max(100), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), example_format: z.string().nullable().optional(), id: z.number().int().optional(), max_length: z.number().int().gte(0).lte(65535).nullable().optional(), question_type: questionTypeEnum.optional(), secondary_code: z.string().max(100).nullable().optional(), value: z.string()}); export const labOrderTestToLabOrderAOEQuestion = z.object({question: labOrderAOEQuestion, required: z.boolean().optional(), sequence: z.number().int().gte(0).lte(65535), test: z.number().int().optional()}); export const labOrderTestWithDiagnoses = z.object({code: z.string().max(50).nullable().optional(), compendium: z.number().int().nullable().optional(), cpts: z.array(z.string()).optional(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), icd10_codes: z.array(iCD10Code), id: z.number().int().optional(), lab_vendor: z.number().int().nullable().optional(), name: z.string().max(255), practice_created: z.number().int().nullable().optional(), procedure_class: z.string().max(50).nullable().optional(), questions: z.array(labOrderTestToLabOrderAOEQuestion).optional(), synonyms: z.array(z.string()).optional()}); export const labOrderContent = z.object({collection_datetime: z.string().nullable().optional(), fasting_method: z.union([fastingMethodEnum, blankEnum, nullEnum]).optional(), icd10_codes: z.array(iCD10Code).optional(), id: z.number().int().optional(), patient_instructions: z.string().optional(), standing_order_end_date: z.string().nullable().optional(), standing_order_frequency: z.string().max(50).nullable().optional(), stat_method: z.union([statMethodEnum, blankEnum, nullEnum]).optional(), test_center_notes: z.string().optional(), tests: z.array(labOrderTestWithDiagnoses).optional()}); export const biDiLabConnection = z.object({alias: z.string().nullable().optional(), enabled: z.boolean().optional(), id: z.number().int().optional(), priority: z.number().int().gte(0).lte(65535).optional(), protocol: z.string().nullable().optional()}); export const facilityIdentifier = z.object({active: z.boolean(), bidi_connection: biDiLabConnection.nullable().optional(), compendium: z.number().int().nullable().optional(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), description: z.string().max(255).nullable().optional(), id: z.number().int().optional(), lab_connection: z.number().int().nullable().optional(), lab_facility: z.string().max(50), practice: z.number().int(), practice_facility: z.string().max(50), practice_secondary_id: z.string().nullable().optional(), user_facing: z.boolean().optional(), vendor: z.object({display_name: z.string().optional(), id: z.number().int().optional(), name: z.string().optional()})}); export const stateEnum = z.enum(["outstanding", "fulfilled", "cancelled"]); export const documentResolutionState = z.object({created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), document: z.number().int().optional(), id: z.number().int().optional(), note: z.string().max(200).nullable().optional(), resolving_document: z.number().int().nullable().optional(), state: stateEnum}); export const labVendorIdName = z.object({display_name: z.string().optional(), id: z.number().int().optional(), name: z.string().optional()}); export const labVendorPatientSite = z.object({address: z.string().max(200).optional(), city: z.string().max(50).optional(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), fax: z.string().max(20).nullable().optional(), id: z.number().int().optional(), name: z.string().max(200), phone: z.string().max(20).nullable().optional(), practice_created: z.number().int().nullable().optional(), state: z.string().max(2).optional(), suite: z.string().max(40).nullable().optional(), vendor: labVendorIdName.optional(), zip: z.string().max(10).optional()}); export const practiceSpecialInsuranceCodeName = z.object({code: z.string().max(5), name: z.string().max(64)}); export const bodySite = z.object({code: z.string().max(16), deleted_date: z.string().nullable().optional(), text: z.string().max(64)}); export const bodySiteCodeModifier = z.object({code: z.string().max(3), deleted_date: z.string().nullable().optional(), text: z.string().max(64)}); export const labOrderSpecimenModifier = z.object({deleted_date: z.string().nullable().optional(), modifier: bodySiteCodeModifier, sequence: z.number().int().gte(0).lte(65535), specimen: z.number().int()}); export const labOrderSpecimen = z.object({body_site: bodySite, container_id: z.string().max(2), description: z.string().max(100).nullable().optional(), id: z.number().int().optional(), lab_order: z.number().int().optional(), modifiers: z.array(labOrderSpecimenModifier), test: z.number().int()}); export const labOrderSubmission = z.object({id: z.number().int().optional(), lab_order: z.number().int().optional(), state: z.string().max(20).optional(), state_metadata: z.string().max(1500).nullable().optional(), time_acknowledged: z.string().nullable().optional(), time_submitted: z.string()}); export const labOrder = z.object({answers: z.array(labOrderAOEAnswer).nullable().optional(), bill_type: z.union([billTypeEnum, nullEnum]).optional(), ccs: z.array(z.number().int().nullable()).optional(), chart_date: z.string().optional(), confidential: z.boolean().optional(), content: labOrderContent.optional(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), document_date: z.string(), facility: facilityIdentifier.nullable().optional(), follow_up_method: z.string().nullable().optional(), id: z.number().int().optional(), ordering_physician: z.number().int(), patient: z.number().int(), practice: z.number().int(), printable_view: z.string().optional(), requisition: z.number().int().optional(), resolution: documentResolutionState.nullable().optional(), signed_by: z.number().int().nullable().optional(), signed_date: z.string().nullable().optional(), site: labVendorPatientSite.nullable().optional(), special_insurance: practiceSpecialInsuranceCodeName.nullable().optional(), specimens: z.array(labOrderSpecimen).nullable().optional(), submissions: z.array(labOrderSubmission).nullable().optional(), tags: z.array(z.number().int().nullable()).optional(), test_date: z.string().nullable().optional(), vendor: z.number().int().nullable().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 Lab Order * * POST /api/2.0/lab_orders/ */ export const createApiApi20LabOrders = async (args: CreateApiApi20LabOrdersArgs, options?: CreateApiApi20LabOrdersOptions) => { 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/lab_orders/${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(labOrder, await res.json()) };