import {z} from 'zod' export type ThreadMemberStatusEnum = 'Addressed' | 'Requiring Action'; export type ThreadMember = {ack_time?: string | undefined, group?: (number | null) | undefined, id?: number | undefined, status: ThreadMemberStatusEnum, thread?: number | undefined, user?: (number | null) | undefined}; export type Document = {authoring_practice: number, chart_date: string, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, document_date: string, document_type?: (number | null) | undefined, id: number, last_modified?: string | undefined, patient: number, sign_date?: (string | null) | undefined, signed_by?: number | undefined}; export type ThreadMessage = {body: string, id?: number | undefined, patient?: number | undefined, practice?: number | undefined, send_date: string, sender: number, thread?: number | undefined, to_document?: Document | undefined}; export type MessageThread = {chart_date: string, created_date?: (string | null) | undefined, deleted_date?: (string | null) | undefined, delivery_date?: (string | null) | undefined, document_date: string, id?: number | undefined, is_urgent?: boolean | undefined, members?: Array | undefined, messages?: Array | undefined, patient: number, practice: number, signed_by?: (number | null) | undefined, signed_date?: (string | null) | undefined}; export type UpdateApiApi20MessageThreadsIdArgs = {id: number, body: MessageThread}; export type UpdateApiApi20MessageThreadsIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const threadMemberStatusEnum = z.enum(["Addressed", "Requiring Action"]); export const threadMember = z.object({ack_time: z.string().optional(), group: z.number().int().nullable().optional(), id: z.number().int().optional(), status: threadMemberStatusEnum, thread: z.number().int().optional(), user: z.number().int().nullable().optional()}); export const document = z.object({authoring_practice: z.number().int(), chart_date: z.string(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), document_date: z.string(), document_type: z.number().int().nullable().optional(), id: z.number().int(), last_modified: z.string().optional(), patient: z.number().int(), sign_date: z.string().nullable().optional(), signed_by: z.number().int().optional()}); export const threadMessage = z.object({body: z.string(), id: z.number().int().optional(), patient: z.number().int().optional(), practice: z.number().int().optional(), send_date: z.string(), sender: z.number().int(), thread: z.number().int().optional(), to_document: document.optional()}); export const messageThread = z.object({chart_date: z.string(), created_date: z.string().nullable().optional(), deleted_date: z.string().nullable().optional(), delivery_date: z.string().nullable().optional(), document_date: z.string(), id: z.number().int().optional(), is_urgent: z.boolean().optional(), members: z.array(threadMember).optional(), messages: z.array(threadMessage).optional(), patient: z.number().int(), practice: z.number().int(), signed_by: z.number().int().nullable().optional(), signed_date: 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 Message Thread * * PUT /api/2.0/message_threads/{id}/ */ export const updateApiApi20MessageThreadsId = async (args: UpdateApiApi20MessageThreadsIdArgs, options?: UpdateApiApi20MessageThreadsIdOptions) => { 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/message_threads/${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(messageThread, await res.json()) };