import {z} from 'zod' export type ExternalIdInput = {type_id: string, value: string}; export type MatchSuggestionRequest = {external_id?: (ExternalIdInput | null) | undefined, first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, date_of_birth?: (string | null) | undefined, phone_number?: (string | null) | undefined, email?: (string | null) | undefined}; export type CreateApiV1PatientsMatchSuggestionArgs = {body: MatchSuggestionRequest}; export type CreateApiV1PatientsMatchSuggestionOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowStageAction = z.enum(["verify_insurance", "sync_external", "complete_verification"]); export const workflowStageSummary = z.object({id: z.string(), title: z.string().nullable().optional(), actions: z.array(workflowStageAction).nullable().optional(), order: z.number().int().nullable().optional(), description: z.string().nullable().optional()}); export const matchSuggestionPatient = z.object({id: z.string(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), phone_number: z.string().nullable().optional(), email: z.string().nullable().optional(), day_of_birth: z.string().nullable().optional(), workflow_stage: workflowStageSummary.nullable().optional(), active: z.boolean().optional()}); export const matchSuggestionResponse = z.object({kind: z.enum(["hard", "medium", "soft", "none"]), reason: z.union([z.enum(["external_id", "demographics", "phone_fuzzy_name", "email_fuzzy_name"]), z.literal("name_only")]).nullable().optional(), patient: matchSuggestionPatient.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 }; /** * Match Suggestion (no-write) * * POST /v1/patients/match-suggestion * * Also accepts auth: HTTPBearer */ export const createApiV1PatientsMatchSuggestion = async (args: CreateApiV1PatientsMatchSuggestionArgs, options: CreateApiV1PatientsMatchSuggestionOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/patients/match-suggestion${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(matchSuggestionResponse, await res.json()) };