import {z} from 'zod' export type IssueHandoutRefDto = {questionnaireId: string, attachPdf?: boolean | undefined}; export type IssueHandoutLinksRequestDto = {clientId: string, patientId?: (string | null) | undefined, channel: string, handouts: Array}; export type CreateApiQuestionnairesHandoutsIssueLinksArgs = {body: IssueHandoutLinksRequestDto}; export type CreateApiQuestionnairesHandoutsIssueLinksOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const issuedHandoutLinkDto = z.object({questionnaireId: z.string(), name: z.string(), url: z.string(), responseId: z.string(), linkId: z.string(), handoutVersion: z.union([z.number().int(), z.string()]).nullable().optional(), stagedAttachmentId: z.string().nullable().optional()}); export const issueHandoutLinksResultDto = z.object({success: z.boolean().optional(), errorMessage: z.string().nullable().optional(), links: z.array(issuedHandoutLinkDto).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 }; /** * Issue links for the handouts in a client message that is sending now (ITE-1590): one * response and pinned link each, created exactly as `send` creates them, with no * invitation — Orchestrate's message carries the links. Internal only: called by the * Orchestrate send path, never by the browser (a preview or render must not create links). * * POST /Questionnaires/handouts/issue-links */ export const createApiQuestionnairesHandoutsIssueLinks = async (args: CreateApiQuestionnairesHandoutsIssueLinksArgs, options?: CreateApiQuestionnairesHandoutsIssueLinksOptions) => { 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(/\/+$/, '')}/Questionnaires/handouts/issue-links${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(issueHandoutLinksResultDto, await res.json()) };