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 S3RetrievingDocumentFile = {base64_content?: string | undefined, content_type?: string | undefined, document?: string | undefined, external_content: unknown, id?: number | undefined, original_filename?: string | undefined}; export type PatientLetter = {attachments?: Array | undefined, body?: string | undefined, category?: (number | null) | undefined, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, delivery_date?: (string | null) | undefined, document_date?: string | undefined, files?: (Array | null) | undefined, id?: number | undefined, is_patient_initiated?: (boolean | null) | undefined, is_processed?: boolean | undefined, letter_type?: string | undefined, patient: number, patient_read_time?: (string | null) | undefined, practice: number, send_out?: boolean | undefined, send_to_patient?: boolean | undefined, sign_date?: (string | null) | undefined, signed_by?: (number | null) | undefined, subject?: string | undefined, tags?: Array | undefined, viewed_at?: (string | null) | undefined}; export type UpdateApiApi20PatientLettersIdArgs = {id: number, body: PatientLetter}; export type UpdateApiApi20PatientLettersIdOptions = {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 s3RetrievingDocumentFile = z.object({base64_content: z.string().optional(), content_type: z.string().optional(), document: z.string().optional(), external_content: z.unknown(), id: z.number().int().optional(), original_filename: z.string().optional()}); export const patientLetter = z.object({attachments: z.array(letterAttachment).optional(), body: z.string().optional(), category: z.number().int().nullable().optional(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), delivery_date: z.string().nullable().optional(), document_date: z.string().optional(), files: z.array(s3RetrievingDocumentFile).nullable().optional(), id: z.number().int().optional(), is_patient_initiated: z.boolean().nullable().optional(), is_processed: z.boolean().optional(), letter_type: z.string().optional(), patient: z.number().int(), patient_read_time: z.string().nullable().optional(), practice: z.number().int(), send_out: z.boolean().optional(), send_to_patient: z.boolean().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(), viewed_at: z.string().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 }; /** * Update Patient Letter * * PUT /api/2.0/patient_letters/{id}/ */ export const updateApiApi20PatientLettersId = async (args: UpdateApiApi20PatientLettersIdArgs, options?: UpdateApiApi20PatientLettersIdOptions) => { 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/patient_letters/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(patientLetter, await res.json()) };