import {z} from 'zod' export type GetApiQuestionnairesArgs = {tax_year?: number | undefined, entity_id?: number | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiQuestionnairesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const firmRoleAssignment = z.object({id: z.number().int().optional(), firm_role_id: z.number().int().optional(), user_id: z.number().int().optional()}); export const questionnaireSummary = z.object({id: z.number().int().optional(), company_id: z.number().int().optional(), name: z.string().nullable().optional(), formatted_name: z.string().nullable().optional(), tax_year: z.number().int().nullable().optional(), status: z.string().nullable().optional(), completed: z.boolean().nullable().optional(), locked: z.boolean().nullable().optional(), is_draft: z.boolean().nullable().optional(), is_open_item: z.boolean().nullable().optional(), in_progress: z.boolean().nullable().optional(), processing: z.boolean().nullable().optional(), due_date: z.string().nullable().optional(), sent_at: z.string().nullable().optional(), submitted_at: z.string().nullable().optional(), opened_timestamp: z.string().nullable().optional(), updated_at_by_client: z.string().nullable().optional(), first_doc_uploaded_at: z.string().nullable().optional(), last_followed_up: z.string().nullable().optional(), last_activity: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), deleted_at: z.string().nullable().optional(), document_path: z.string().nullable().optional(), documents_needed: z.array(z.record(z.string(), z.unknown())).optional(), document_count_answered: z.number().int().nullable().optional(), document_count_total: z.number().int().nullable().optional(), note_count: z.number().int().nullable().optional(), sections: z.array(z.record(z.string(), z.unknown())).optional(), company_details: z.object({entity_type: z.string().nullable().optional(), external_entity_id: z.string().nullable().optional(), hash_id: z.string().nullable().optional(), inactive: z.boolean().nullable().optional(), name: z.string().nullable().optional()}).optional(), user_details: z.object({assigned_advisor_id: z.number().int().nullable().optional(), external_user_id: z.string().nullable().optional(), firm_group_name: z.string().nullable().optional(), for_testing: z.boolean().nullable().optional()}).optional(), firm_role_assignments: z.array(firmRoleAssignment).optional()}).and(z.record(z.string(), z.unknown())); export const getApiQuestionnairesResponse = z.array(questionnaireSummary); 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 }; /** * List questionnaires * * GET /questionnaires */ export const getApiQuestionnaires = async (args: GetApiQuestionnairesArgs, options?: GetApiQuestionnairesOptions) => { const {baseUrl = "https://api.soraban.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tax_year != null) query.append("tax_year", String(args.tax_year)) if (args.entity_id != null) query.append("entity_id", String(args.entity_id)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/questionnaires${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(getApiQuestionnairesResponse, await res.json()) };