import {z} from 'zod' export type StartGroupVisitRequest = {patientGroupId?: string | undefined, locationId?: (string | null) | undefined, reasonForVisit?: (string | null) | undefined}; export type CreateApiGroupVisitsStartArgs = {body: StartGroupVisitRequest}; export type CreateApiGroupVisitsStartOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const memberExceptionDto = z.object({id: z.string().optional(), soapNoteId: z.string().optional(), patientGroupMemberId: z.string().optional(), memberDisplayName: z.string().optional(), exceptionNote: z.string().optional(), differentTreatment: z.boolean().optional(), followUpNeeded: z.boolean().optional(), createdAt: z.string().optional()}); export const groupSoapNoteDto = z.object({id: z.string().optional(), subjective: z.string().optional(), objective: z.string().optional(), assessment: z.string().optional(), plan: z.string().optional(), cohortLabel: z.string().nullable().optional(), status: z.string().optional(), format: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), exceptions: z.array(memberExceptionDto).optional()}); export const groupTreatmentDto = z.object({id: z.string().optional(), productName: z.string().optional(), dose: z.string().nullable().optional(), route: z.string().nullable().optional(), frequency: z.string().nullable().optional(), duration: z.string().nullable().optional(), numberTreated: z.union([z.number().int(), z.string()]).nullable().optional(), cohortLabel: z.string().nullable().optional(), inventoryItemId: z.string().nullable().optional(), serviceCatalogItemId: z.string().nullable().optional(), quantityUsed: z.union([z.number(), z.string()]).nullable().optional(), inventoryTransactionId: z.string().nullable().optional(), meatWithdrawalEndDate: z.string().nullable().optional(), milkWithdrawalEndDate: z.string().nullable().optional(), notes: z.string().nullable().optional(), createdById: z.string().optional(), createdAt: z.string().optional()}); export const activeWithdrawalDto = z.object({productName: z.string().optional(), kind: z.string().optional(), endDate: z.string().optional(), treatmentId: z.string().optional()}); export const groupVisitDto = z.object({appointmentId: z.string().optional(), patientGroupId: z.string().optional(), groupName: z.string().optional(), groupType: z.string().optional(), startedAt: z.string().optional(), status: z.string().optional(), reasonForVisit: z.string().nullable().optional(), soapNotes: z.array(groupSoapNoteDto).optional(), treatments: z.array(groupTreatmentDto).optional(), activeWithdrawals: z.array(activeWithdrawalDto).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 }; /** * Start a group visit. Creates a synthetic ScheduleItem + Appointment bound to the * group, returning the appointment ID for the caller to navigate to. * * POST /GroupVisits/start */ export const createApiGroupVisitsStart = async (args: CreateApiGroupVisitsStartArgs, options?: CreateApiGroupVisitsStartOptions) => { 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(/\/+$/, '')}/GroupVisits/start${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(groupVisitDto, await res.json()) };