import {z} from 'zod' export type GFECreateRequest = {patientId: string, locationId: string, appointmentTime: string, requestedTreatments: Array, patientEmail?: (string | null) | undefined, patientPhone?: (string | null) | undefined}; export type CreateApiSpakinectGfeArgs = {TENANT: string, 'API-KEY': string, body: GFECreateRequest}; export type CreateApiSpakinectGfeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const gFEStatus = z.enum(["Processing", "Pending Review", "Approved", "Rejected", "Completed", "Cancelled", "Cancelled by client"]); export const patientDetails = z.object({name: z.string().nullable().optional(), first_name: z.string(), last_name: z.string(), email: z.string(), phone: z.string().nullable().optional()}); export const documentType = z.enum(["LAB", "PHARMACY", "IMAGE", "REPORT", "OTHER", "CONSENT", "NOTE_UPLOAD"]); export const documentSummary = z.object({id: z.string(), patientId: z.string(), filePath: z.string(), fileType: z.string(), title: z.string(), summary: z.string().nullable(), docType: documentType, signatureUrl: z.string().nullable()}); export const gFEDetail = z.object({id: z.string(), patientId: z.string(), spakinectGfeId: z.string(), createdDate: z.string(), updatedDate: z.string().nullable().optional(), status: gFEStatus, requestedTreatments: z.array(z.string()), patientDetails: patientDetails, document: documentSummary.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 Gfe Request * * POST /spakinect/gfe */ export const createApiSpakinectGfe = async (args: CreateApiSpakinectGfeArgs, options: CreateApiSpakinectGfeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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'])) const url = `${baseUrl.replace(/\/+$/, '')}/spakinect/gfe${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(gFEDetail, await res.json()) };