import {z} from 'zod' export type ErxMedicationDto = {ndcCode?: string | undefined, name?: string | undefined, brandName?: (string | null) | undefined, drugClass?: (string | null) | undefined, category?: (string | null) | undefined, quantityUomCode?: (string | null) | undefined, packageSize?: (string | null) | undefined, packageSizeUom?: (string | null) | undefined, labeler?: (string | null) | undefined, deaSchedule?: (string | null) | undefined, isControlled?: boolean | undefined}; export type ErxPharmacyDto = {id?: (number | unknown) | undefined, name?: string | undefined, address1?: (string | null) | undefined, address2?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, zip?: (string | null) | undefined, phone?: (string | null) | undefined, ncpdp?: (string | null) | undefined, npi?: (string | null) | undefined}; export type SendErxPrescriptionRequestDto = {patientId?: string | undefined, providerUserId?: string | undefined, medication?: ErxMedicationDto | undefined, quantity?: (number | unknown) | undefined, daysSupply?: (number | unknown | null) | undefined, refills?: (number | unknown) | undefined, sig?: string | undefined, notesToPharmacist?: (string | null) | undefined, weight?: (string | null) | undefined, pharmacy?: ErxPharmacyDto | undefined, medicationRequestId?: (string | null) | undefined}; export type CreateApiErxPrescriptionsArgs = {body: SendErxPrescriptionRequestDto}; export type CreateApiErxPrescriptionsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const outsideMedicationErxDto = z.object({vendor: z.string().optional(), providerUserId: z.string().nullable().optional(), providerName: z.string().nullable().optional(), ndcCode: z.string().nullable().optional(), quantityUomCode: z.string().nullable().optional(), daysSupply: z.union([z.number().int(), z.string()]).nullable().optional(), refills: z.union([z.number().int(), z.string()]).nullable().optional(), pharmacyDirectoryId: z.union([z.number().int(), z.string()]).nullable().optional(), pharmacySnapshot: z.string().nullable().optional(), code: z.string().nullable().optional(), sentAt: z.string().nullable().optional(), result: z.string().nullable().optional(), lastError: z.string().nullable().optional(), medicationRequestId: z.string().nullable().optional()}); export const outsideMedicationDto = z.object({id: z.string().optional(), patientId: z.string().optional(), medicationName: z.string().optional(), quantity: z.string().nullable().optional(), sig: z.string().nullable().optional(), status: z.string().optional(), kind: z.string().optional(), requestedDate: z.string().optional(), source: z.string().nullable().optional(), notes: z.string().nullable().optional(), createdByName: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), filledAt: z.string().nullable().optional(), filledByName: z.string().nullable().optional(), erx: outsideMedicationErxDto.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 }; /** * Send a non-controlled prescription to an outside pharmacy via VetHubRx. Creates the * OutsideMedication record (Status=Sending) and returns it; the final Sent/Failed * state arrives asynchronously via webhook. * * POST /erx/prescriptions */ export const createApiErxPrescriptions = async (args: CreateApiErxPrescriptionsArgs, options?: CreateApiErxPrescriptionsOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/erx/prescriptions${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(outsideMedicationDto, await res.json()) };