import {z} from 'zod' export type AttachmentTypeEnum = 'Entity' | 'RefPanel' | 'PatientDemog' | 'ImmHistory' | 'MedsList' | 'CompMedsList' | 'WhoHeightChart' | 'WhoWeightChart' | 'WhoHcChart' | 'WhoWflChart' | 'CdcHeightChart' | 'CdcWeightChart' | 'CdcBmiChart' | 'CdcHcChart' | 'PremieHeightChart' | 'PremieWeightChart' | 'PremieHcChart' | 'DsinfantHeightChart' | 'DsinfantWeightChart' | 'DsinfantHcChart' | 'DsHeightChart' | 'DsWeightChart' | 'DsBmiChart' | 'DsHcChart'; export type LetterAttachment = {attachment_type: AttachmentTypeEnum, entity_date?: (string | null) | undefined, entity_description?: (string | null) | undefined, entity_id?: number | undefined, entity_type?: (number | null) | undefined, handout?: (number | null) | undefined, id?: number | undefined, summary?: (string | null) | undefined}; export type DeliveryMethodEnum = 'direct' | 'email' | 'emr' | 'fax' | 'passport' | 'printed'; export type SendToContact = {first_name?: string | undefined, id: number, last_name?: string | undefined, middle_name?: string | undefined, npi?: (string | null) | undefined, org_name?: (string | null) | undefined, specialties?: Array | undefined}; export type PatchedLetter = {attachments?: Array | undefined, body?: string | undefined, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, delivery_date?: (string | null) | undefined, delivery_method?: DeliveryMethodEnum | undefined, direct_message_status?: (string | null) | undefined, direct_message_to?: (string | null) | undefined, display_to?: (string | null) | undefined, document_date?: string | undefined, email_to?: (string | null) | undefined, failure_unacknowledged?: string | undefined, fax_attachments?: boolean | undefined, fax_status?: string | undefined, fax_to?: (string | null) | undefined, id?: number | undefined, is_processed?: boolean | undefined, letter_type?: string | undefined, patient?: number | undefined, practice?: number | undefined, referral_order?: (number | null) | undefined, send_out?: boolean | undefined, send_to_contact?: SendToContact | undefined, send_to_elation_user?: number | undefined, send_to_name?: (string | null) | undefined, sign_date?: (string | null) | undefined, signed_by?: (number | null) | undefined, subject?: string | undefined, tags?: Array | undefined, to_number?: (string | null) | undefined, viewed_at?: (string | null) | undefined, with_archive?: boolean | undefined}; export type PatchApiApi20LettersIdArgs = {id: number, body: PatchedLetter}; export type PatchApiApi20LettersIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const attachmentTypeEnum = z.enum(["Entity", "RefPanel", "PatientDemog", "ImmHistory", "MedsList", "CompMedsList", "WhoHeightChart", "WhoWeightChart", "WhoHcChart", "WhoWflChart", "CdcHeightChart", "CdcWeightChart", "CdcBmiChart", "CdcHcChart", "PremieHeightChart", "PremieWeightChart", "PremieHcChart", "DsinfantHeightChart", "DsinfantWeightChart", "DsinfantHcChart", "DsHeightChart", "DsWeightChart", "DsBmiChart", "DsHcChart"]); export const letterAttachment = z.object({attachment_type: attachmentTypeEnum, entity_date: z.string().nullable().optional(), entity_description: z.string().nullable().optional(), entity_id: z.number().int().optional(), entity_type: z.number().int().nullable().optional(), handout: z.number().int().nullable().optional(), id: z.number().int().optional(), summary: z.string().nullable().optional()}); export const deliveryMethodEnum = z.enum(["direct", "email", "emr", "fax", "passport", "printed"]); export const sendToContact = z.object({first_name: z.string().max(50).optional(), id: z.number().int(), last_name: z.string().max(60).optional(), middle_name: z.string().max(50).optional(), npi: z.string().max(20).nullable().optional(), org_name: z.string().max(100).nullable().optional(), specialties: z.array(z.unknown()).optional()}); export const letter = z.object({attachments: z.array(letterAttachment).optional(), body: z.string().optional(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), delivery_date: z.string().nullable().optional(), delivery_method: deliveryMethodEnum.optional(), direct_message_status: z.string().nullable().optional(), direct_message_to: z.string().max(200).nullable().optional(), display_to: z.string().nullable().optional(), document_date: z.string().optional(), email_to: z.string().max(200).nullable().optional(), failure_unacknowledged: z.string().optional(), fax_attachments: z.boolean().optional(), fax_status: z.string().optional(), fax_to: z.string().max(20).nullable().optional(), id: z.number().int().optional(), is_processed: z.boolean().optional(), letter_type: z.string().optional(), patient: z.number().int(), practice: z.number().int(), referral_order: z.number().int().nullable().optional(), send_out: z.boolean().optional(), send_to_contact: sendToContact, send_to_elation_user: z.number().int().optional(), send_to_name: z.string().max(200).nullable().optional(), sign_date: z.string().nullable().optional(), signed_by: z.number().int().nullable().optional(), subject: z.string().max(200).optional(), tags: z.array(z.number().int()).optional(), to_number: z.string().nullable().optional(), viewed_at: z.string().nullable().optional(), with_archive: z.boolean().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 }; /** * Partially Update Letter * * PATCH /api/2.0/letters/{id}/ */ export const patchApiApi20LettersId = async (args: PatchApiApi20LettersIdArgs, options?: PatchApiApi20LettersIdOptions) => { 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/letters/${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(letter, await res.json()) };