import {z} from 'zod' export type GetApiApi20PatientLettersArgs = {document_date__gt?: string | undefined, document_date__gte?: string | undefined, document_date__lt?: string | undefined, document_date__lte?: string | undefined, limit?: number | undefined, offset?: number | undefined, patient?: Array | undefined, practice?: Array | undefined, unread?: boolean | undefined}; export type GetApiApi20PatientLettersOptions = {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 paginatedPatientLetterList = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(patientLetter)}); 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 }; /** * List Patient Letters * * GET /api/2.0/patient_letters/ */ export const getApiApi20PatientLetters = async (args: GetApiApi20PatientLettersArgs, options?: GetApiApi20PatientLettersOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.document_date__gt != null) query.append("document_date__gt", String(args.document_date__gt)) if (args.document_date__gte != null) query.append("document_date__gte", String(args.document_date__gte)) if (args.document_date__lt != null) query.append("document_date__lt", String(args.document_date__lt)) if (args.document_date__lte != null) query.append("document_date__lte", String(args.document_date__lte)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) for (const item of args.patient ?? []) query.append("patient", String(item)) for (const item of args.practice ?? []) query.append("practice", String(item)) if (args.unread != null) query.append("unread", String(args.unread)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/2.0/patient_letters/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedPatientLetterList, await res.json()) };