import {z} from 'zod' export type Gender = 'male' | 'female' | 'other'; export type States = 'alabama' | 'alaska' | 'arizona' | 'arkansas' | 'california' | 'colorado' | 'connecticut' | 'delaware' | 'florida' | 'georgia' | 'hawaii' | 'idaho' | 'illinois' | 'indiana' | 'iowa' | 'kansas' | 'kentucky' | 'louisiana' | 'maine' | 'maryland' | 'massachusetts' | 'michigan' | 'minnesota' | 'mississippi' | 'missouri' | 'montana' | 'nebraska' | 'nevada' | 'new_hampshire' | 'new_jersey' | 'new_mexico' | 'new_york' | 'north_carolina' | 'north_dakota' | 'ohio' | 'oklahoma' | 'oregon' | 'pennsylvania' | 'rhode_island' | 'south_carolina' | 'south_dakota' | 'tennessee' | 'texas' | 'utah' | 'vermont' | 'virginia' | 'washington' | 'west_virginia' | 'wisconsin' | 'wyoming' | 'district_of_columbia' | 'puerto_rico' | 'virgin_islands' | 'guam' | 'northern_mariana_islands' | 'american_samoa'; export type ServiceType = 'medical_care' | 'surgical' | 'consultation' | 'diagnostic_x_ray' | 'diagnostic_lab' | 'radiation_therapy' | 'anesthesia' | 'surgical_assistance' | 'other_medical' | 'blood_charges' | 'used_durable_medical_equipment' | 'durable_medical_equipment_purchase' | 'ambulatory_service_center_facility' | 'renal_supplies_in_home' | 'alternate_method_dialysis' | 'chronic_renal_disease_equipment' | 'pre_admission_testing' | 'durable_medical_equipment_rental' | 'pneumonia_vaccine' | 'second_surgical_opinion' | 'third_surgical_opinion' | 'social_work' | 'diagnostic_dental' | 'periodontics' | 'restorative' | 'endodontics' | 'maxillofacial_prosthetics' | 'adjunctive_dental_services' | 'health_benefit_plan_coverage' | 'plan_waiting_period' | 'chiropractic' | 'chiropractic_office_visits' | 'dental_care' | 'dental_crowns' | 'dental_accident' | 'orthodontics' | 'prosthodontics' | 'oral_surgery' | 'routine_preventive_dental' | 'home_health_care' | 'home_health_prescriptions' | 'home_health_visits' | 'hospice' | 'respite_care' | 'hospital' | 'hospital_inpatient' | 'hospital_room_and_board' | 'hospital_outpatient' | 'hospital_emergency_accident' | 'hospital_emergency_medical' | 'hospital_ambulatory_surgical' | 'long_term_care' | 'major_medical' | 'medically_related_transportation' | 'air_transportation' | 'cabulance' | 'licensed_ambulance' | 'general_benefits' | 'in_vitro_fertilization' | 'mri_cat_scan' | 'donor_procedures' | 'acupuncture' | 'newborn_care' | 'pathology' | 'smoking_cessation' | 'well_baby_care' | 'maternity' | 'transplants' | 'audiology_exam' | 'inhalation_therapy' | 'diagnostic_medical' | 'private_duty_nursing' | 'prosthetic_device' | 'dialysis' | 'otological_exam' | 'chemotherapy' | 'allergy_testing' | 'immunizations' | 'routine_physical' | 'family_planning' | 'infertility' | 'abortion' | 'aids' | 'emergency_services' | 'cancer' | 'pharmacy' | 'free_standing_prescription_drug' | 'mail_order_prescription_drug' | 'brand_name_prescription_drug' | 'generic_prescription_drug' | 'podiatry' | 'podiatry_office_visits' | 'podiatry_nursing_home_visits' | 'professional_physician' | 'anesthesiologist' | 'professional_visit_office' | 'professional_visit_inpatient' | 'professional_visit_outpatient' | 'professional_visit_nursing_home' | 'professional_visit_skilled_nursing_facility' | 'professional_visit_home' | 'psychiatric' | 'psychiatric_room_and_board' | 'psychotherapy' | 'psychiatric_inpatient' | 'psychiatric_outpatient' | 'rehabilitation' | 'rehabilitation_room_and_board' | 'rehabilitation_inpatient' | 'rehabilitation_outpatient' | 'occupational_therapy' | 'physical_medicine' | 'speech_therapy' | 'skilled_nursing_care' | 'skilled_nursing_care_room_and_board' | 'substance_abuse' | 'alcoholism' | 'drug_addiction' | 'vision_optometry' | 'frames' | 'routine_exam' | 'lenses' | 'nonmedically_necessary_physical' | 'experimental_drug_therapy' | 'burn_care' | 'brand_name_prescription_drug_formulary' | 'brand_name_prescription_drug_non_formulary' | 'independent_medical_evaluation' | 'partial_hospitalization_psychiatric' | 'day_care_psychiatric' | 'cognitive_therapy' | 'massage_therapy' | 'pulmonary_rehabilitation' | 'cardiac_rehabilitation' | 'pediatric' | 'nursery' | 'skin' | 'orthopedic' | 'cardiac' | 'lymphatic' | 'gastrointestinal' | 'endocrine' | 'neurology' | 'eye' | 'invasive_procedures' | 'gynecological' | 'obstetrical' | 'obstetrical_gynecological' | 'mail_order_prescription_drug_brand_name' | 'mail_order_prescription_drug_generic' | 'physician_visit_office_sick' | 'physician_visit_office_well' | 'coronary_care' | 'private_duty_nursing_inpatient' | 'private_duty_nursing_home' | 'surgical_benefits_professional' | 'surgical_benefits_facility' | 'mental_health_provider_inpatient' | 'mental_health_provider_outpatient' | 'mental_health_facility_inpatient' | 'mental_health_facility_outpatient' | 'substance_abuse_facility_inpatient' | 'substance_abuse_facility_outpatient' | 'screening_x_ray' | 'screening_laboratory' | 'mammogram_high_risk_patient' | 'mammogram_low_risk_patient' | 'flu_vaccination' | 'eyewear_and_accessories' | 'case_management' | 'dermatology' | 'durable_medical_equipment' | 'diabetic_supplies' | 'generic_prescription_drug_formulary' | 'generic_prescription_drug_non_formulary' | 'allergy' | 'intensive_care' | 'mental_health' | 'neonatal_intensive_care' | 'oncology' | 'physical_therapy' | 'pulmonary' | 'renal' | 'residential_psychiatric_treatment' | 'transitional_care' | 'transitional_nursery_care' | 'urgent_care'; export type ReferralCreateRequest = {physician_name?: (string | null) | undefined, physician_email?: (string | null) | undefined, physician_phone?: (string | null) | undefined, physician_fax?: (string | null) | undefined, physician_group?: (string | null) | undefined, physician_npi?: (string | null) | undefined, physician_address?: (string | null) | undefined, physician_address_2?: (string | null) | undefined, physician_city?: (string | null) | undefined, physician_state?: (States | null) | undefined, physician_zip?: (string | null) | undefined, icd_codes?: (Array | null) | undefined, service_types?: (Array | null) | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, location_id?: (string | null) | undefined, file_id?: (string | null) | undefined}; export type InsuranceTier = 'primary' | 'secondary' | 'tertiary' | 'quaternary'; export type BatchPayorInline = {insurance: string, insured_member_id?: (string | null) | undefined, payor_responsibility: InsuranceTier}; export type PatientBatchItem = {first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, middle_name?: (string | null) | undefined, phone_number?: (string | null) | undefined, additional_phone_number?: (string | null) | undefined, email?: (string | null) | undefined, date_of_birth?: (string | null) | undefined, gender?: (Gender | null) | undefined, address?: (string | null) | undefined, address2?: (string | null) | undefined, city?: (string | null) | undefined, state?: (States | null) | undefined, zip?: (string | null) | undefined, comments?: (string | null) | undefined, workflow_stage_id?: (string | null) | undefined, assigned_user_id?: (string | null) | undefined, location_id?: (string | null) | undefined, organization_id?: (string | null) | undefined, tags?: (Array | null) | undefined, referral?: (ReferralCreateRequest | null) | undefined, payors?: (Array | null) | undefined}; export type PatientBatchCreateRequest = {items: Array}; export type CreateApiV1PatientsBatchArgs = {body: PatientBatchCreateRequest}; export type CreateApiV1PatientsBatchOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const patientBatchCreateResponse = z.object({created_count: z.number().int(), updated_count: z.number().int().optional(), patient_ids: z.array(z.string()), updated_patient_ids: z.array(z.string()).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 }; /** * Batch create/update patients * * POST /v1/patients/batch * * Also accepts auth: HTTPBearer */ export const createApiV1PatientsBatch = async (args: CreateApiV1PatientsBatchArgs, options: CreateApiV1PatientsBatchOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/patients/batch${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(patientBatchCreateResponse, await res.json()) };