import {z} from 'zod' export type GetApiCommsChatListArgs = {TENANT: string, 'API-KEY': string, page?: (number | null) | undefined, per_page?: (number | null) | undefined, sort_by?: (string | null) | undefined, sort_direction?: ('asc' | 'desc' | null) | undefined, location_id?: (string | null) | undefined, search_term?: (string | null) | undefined, patient_id?: (string | null) | undefined, chat_id?: (string | null) | undefined, unread_only?: boolean | undefined, unresponded_only?: boolean | undefined, human_only?: boolean | undefined}; export type GetApiCommsChatListOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const chatStatus = z.enum(["READ", "UNREAD"]); export const chatPatient = z.object({id: z.string(), firstName: z.string().nullable(), lastName: z.string().nullable(), primaryLocationId: z.string().nullable()}); export const chatSummary = z.object({id: z.string(), primaryPatientId: z.string().nullable(), status: chatStatus, patients: z.array(chatPatient), phoneNumber: z.string(), latestMessageContent: z.string().nullable().optional(), latestMessageDate: z.string().nullable().optional(), createdDate: z.string(), optedOutOfSms: z.boolean()}); export const pageChatSummary = z.object({data: z.array(chatSummary), total: z.number().int()}); 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 }; /** * Get * * GET /comms/chat/list */ export const getApiCommsChatList = async (args: GetApiCommsChatListArgs, options: GetApiCommsChatListOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.location_id != null) query.append("location_id", String(args.location_id)) if (args.search_term != null) query.append("search_term", String(args.search_term)) if (args.patient_id != null) query.append("patient_id", String(args.patient_id)) if (args.chat_id != null) query.append("chat_id", String(args.chat_id)) if (args.unread_only != null) query.append("unread_only", String(args.unread_only)) if (args.unresponded_only != null) query.append("unresponded_only", String(args.unresponded_only)) if (args.human_only != null) query.append("human_only", String(args.human_only)) const url = `${baseUrl.replace(/\/+$/, '')}/comms/chat/list${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(pageChatSummary, await res.json()) };