import {z} from 'zod' export type AiGenerationInfo = {is_finalized?: boolean | undefined, transcript_source_text?: (string | null) | undefined}; export type El8NoteModelsNoteContentBaseNodeNodeNodeAttrs = {ai_generation_info?: (AiGenerationInfo | null) | undefined, creation_type?: ('ai' | 'template' | null) | undefined, is_new?: (boolean | null) | undefined, ref_id?: (string | null) | undefined}; export type AnyNodeContent = {[key: string]: unknown}; export type ColumnLayoutNodeInput = {attrs?: (El8NoteModelsNoteContentBaseNodeNodeNodeAttrs | null) | undefined, content?: Array | undefined, type?: 'columnLayout' | undefined} | {[key: string]: unknown}; export type TwoColumnLayoutNodeInput = {attrs?: (El8NoteModelsNoteContentBaseNodeNodeNodeAttrs | null) | undefined, content?: Array | undefined, type?: 'twoColumnLayout' | undefined} | {[key: string]: unknown}; export type DocumentNodeChildInput = TwoColumnLayoutNodeInput | ColumnLayoutNodeInput; export type DocumentNodeInput = {attrs?: (El8NoteModelsNoteContentBaseNodeNodeNodeAttrs | null) | undefined, content?: Array | undefined, type?: 'document' | undefined} | {[key: string]: unknown}; export type NoteCreate = {bill?: (number | null) | undefined, category: number, confidential?: boolean | undefined, content?: (DocumentNodeInput | null) | undefined, exam_reason?: (string | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, patient: number, practice: number, provider: number, serviced_at?: (string | null) | undefined}; export type CreateApiNotesArgs = {body: NoteCreate}; export type CreateApiNotesOptions = {baseUrl?: string | undefined, RequireAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const aiGenerationInfo = z.object({is_finalized: z.boolean().optional(), transcript_source_text: z.string().nullable().optional()}); export const el8NoteModelsNoteContentBaseNodeNodeNodeAttrs = z.object({ai_generation_info: aiGenerationInfo.nullable().optional(), creation_type: z.enum(["ai", "template"]).nullable().optional(), is_new: z.boolean().nullable().optional(), ref_id: z.string().nullable().optional()}); export const anyNodeContent = z.record(z.string(), z.unknown()); export const columnLayoutNodeOutput = z.object({attrs: el8NoteModelsNoteContentBaseNodeNodeNodeAttrs.nullable().optional(), content: z.array(anyNodeContent).optional(), type: z.literal("columnLayout").optional()}).and(z.record(z.string(), z.unknown())); export const twoColumnLayoutNodeOutput = z.object({attrs: el8NoteModelsNoteContentBaseNodeNodeNodeAttrs.nullable().optional(), content: z.array(columnLayoutNodeOutput).optional(), type: z.literal("twoColumnLayout").optional()}).and(z.record(z.string(), z.unknown())); export const documentNodeChildOutput = z.discriminatedUnion("type", [twoColumnLayoutNodeOutput, columnLayoutNodeOutput]); export const documentNodeOutput = z.object({attrs: el8NoteModelsNoteContentBaseNodeNodeNodeAttrs.nullable().optional(), content: z.array(documentNodeChildOutput).optional(), type: z.literal("document").optional()}).and(z.record(z.string(), z.unknown())); export const cODETYPE = z.enum(["CPT", "SNOMED", "HL7", "ICD9", "LOINC", "CVX", "RXNORM", "ICD10", "CPTII", "HCPCS", "EL8"]); export const doctag = z.object({code: z.string().nullable().optional(), code_type: cODETYPE.nullable().optional(), concept_name: z.string().nullable().optional(), description: z.string().nullable().optional(), id: z.number().int(), is_cpc_plus: z.boolean(), is_mips: z.boolean(), practice: z.number().int().nullable().optional(), shared: z.boolean(), snomed_result_code: z.string().nullable().optional(), value: z.string()}); export const documentSignatureRole = z.enum(["amender", "cosigner", "rendering_provider", "staff_autofiler"]); export const documentSignature = z.object({amendment_id: z.number().int().nullable().optional(), on_behalf_of_user_id: z.number().int().nullable().optional(), on_behalf_of_user_name: z.string().nullable().optional(), signature_comments: z.string(), signature_role: documentSignatureRole, signed_at: z.string(), user_id: z.number().int(), user_name: z.string()}); export const note = z.object({bill: z.number().int().nullable().optional(), category: z.number().int(), confidential: z.boolean().optional(), content: documentNodeOutput.nullable().optional(), doctags: z.array(doctag).optional(), exam_reason: z.string().nullable().optional(), id: z.number().int(), is_draft: z.boolean(), is_outdated: z.boolean().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), patient: z.number().int(), practice: z.number().int(), provider: z.number().int(), serviced_at: z.string().nullable(), signatures: z.array(documentSignature).optional(), snapshot_id: z.number().int().nullable().optional(), summary: 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 }; /** * Create Note * * POST /notes * * Also accepts auth: RequirePracticeAuthorization */ export const createApiNotes = async (args: CreateApiNotesArgs, options?: CreateApiNotesOptions) => { const {baseUrl = "https://sandbox.elationemr.com/api/2.0", fetch: fetchFn = fetch, RequireAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (RequireAuthentication !== undefined) headers.set('authorization', `Bearer ${RequireAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/notes${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(note, await res.json()) };