import {z} from 'zod' export type AccountTypeEnum = 'regular' | 'oncall' | 'ft_non_prescribing' | 'pt_prescribing'; export type NullEnum = unknown; export type PhysicianQualifications = {credentials: string, dea?: string | undefined, license: string, license_state: string, npi?: (string | null) | undefined}; export type UserTypeEnum = 'physician' | 'staff'; export type User = {account_type?: (AccountTypeEnum | NullEnum) | undefined, credentialed?: boolean | undefined, email: string, first_name: string, has_chart_access?: boolean | undefined, id?: number | undefined, is_active: boolean, is_practice_admin?: boolean | undefined, is_read_only?: boolean | undefined, last_name: string, physician_qualifications?: PhysicianQualifications | undefined, practice: number, user_type: UserTypeEnum}; export type CreateApiApi20UsersArgs = {body: User}; export type CreateApiApi20UsersOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const accountTypeEnum = z.enum(["regular", "oncall", "ft_non_prescribing", "pt_prescribing"]); export const nullEnum = z.unknown(); export const physicianQualifications = z.object({credentials: z.string().max(50), dea: z.string().max(50).optional(), license: z.string().max(50), license_state: z.string().max(2), npi: z.string().max(10).nullable().optional()}); export const userTypeEnum = z.enum(["physician", "staff"]); export const user = z.object({account_type: z.union([accountTypeEnum, nullEnum]).optional(), credentialed: z.boolean().optional(), email: z.string().max(255), first_name: z.string().max(50), has_chart_access: z.boolean().optional(), id: z.number().int().optional(), is_active: z.boolean(), is_practice_admin: z.boolean().optional(), is_read_only: z.boolean().optional(), last_name: z.string().max(60), physician_qualifications: physicianQualifications.optional(), practice: z.number().int(), user_type: userTypeEnum}); 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 User * * POST /api/2.0/users/ */ export const createApiApi20Users = async (args: CreateApiApi20UsersArgs, options?: CreateApiApi20UsersOptions) => { 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/users/${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(user, await res.json()) };