import {z} from 'zod' export type AuthorizationForEnum = 'Consult (One Visit Only)' | 'Procedure / Testing Only' | 'Referral For Treatment, includes Consult Visit'; export type NullEnum = unknown; export type ICD10Code = {code: string, description?: (string | null) | undefined}; export type ReferralLetter = {body?: string | undefined, delivery_date?: (string | null) | undefined, id?: number | undefined, sign_date?: (string | null) | 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 MedicalSpecialtyCategory = {abbreviation?: (string | null) | undefined, created_date?: (string | null) | undefined, id?: number | undefined, name: string}; export type MedicalSpecialty = {abbreviation: string, category?: MedicalSpecialtyCategory | undefined, created_date?: (string | null) | undefined, id?: number | undefined, name: string, practice_created?: (number | null) | undefined}; export type PatchedReferralOrder = {auth_number?: (string | null) | undefined, authorization_for?: (AuthorizationForEnum | NullEnum) | undefined, authorization_for_short?: (string | null) | undefined, consultant_name?: (string | null) | undefined, date_for_reEval?: (string | null) | undefined, icd10_codes?: Array | undefined, id?: number | undefined, letter?: number | undefined, patient?: number | undefined, practice?: number | undefined, referral_letter?: ReferralLetter | undefined, resolution?: DocumentResolutionState | undefined, short_consultant_name?: (string | null) | undefined, sign_date?: (string | null) | undefined, signed_by?: number | undefined, specialty?: MedicalSpecialty | undefined}; export type PatchApiApi20ReferralOrdersIdArgs = {id: number, body: PatchedReferralOrder}; export type PatchApiApi20ReferralOrdersIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const authorizationForEnum = z.enum(["Consult (One Visit Only)", "Procedure / Testing Only", "Referral For Treatment, includes Consult Visit"]); export const nullEnum = z.unknown(); export const iCD10Code = z.object({code: z.string().max(50), description: z.string().max(500).nullable().optional()}); export const referralLetter = z.object({body: z.string().optional(), delivery_date: z.string().nullable().optional(), id: z.number().int().optional(), sign_date: z.string().nullable().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 medicalSpecialtyCategory = z.object({abbreviation: z.string().max(20).nullable().optional(), created_date: z.string().nullable().optional(), id: z.number().int().optional(), name: z.string().max(100)}); export const medicalSpecialty = z.object({abbreviation: z.string().max(20), category: medicalSpecialtyCategory.optional(), created_date: z.string().nullable().optional(), id: z.number().int().optional(), name: z.string().max(100), practice_created: z.number().int().nullable().optional()}); export const referralOrder = z.object({auth_number: z.string().max(20).nullable(), authorization_for: z.union([authorizationForEnum, nullEnum]).optional(), authorization_for_short: z.string().nullable().optional(), consultant_name: z.string().max(1024).nullable(), date_for_reEval: z.string().nullable(), icd10_codes: z.array(iCD10Code).optional(), id: z.number().int().optional(), letter: z.number().int().optional(), patient: z.number().int(), practice: z.number().int(), referral_letter: referralLetter.optional(), resolution: documentResolutionState.optional(), short_consultant_name: z.string().max(1024).nullable(), sign_date: z.string().nullable().optional(), signed_by: z.number().int().optional(), specialty: medicalSpecialty}); 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 }; /** * Partially Update Referral Order * * PATCH /api/2.0/referral_orders/{id}/ */ export const patchApiApi20ReferralOrdersId = async (args: PatchApiApi20ReferralOrdersIdArgs, options?: PatchApiApi20ReferralOrdersIdOptions) => { 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/referral_orders/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(referralOrder, await res.json()) };