import {z} from 'zod' export type GetApiFinancialsFinancialsIdArgs = {financialsId: string, includes?: ('amendments' | 'worksheet' | 'amendments worksheet') | undefined}; export type GetApiFinancialsFinancialsIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const worksheetSectionTotals = z.object({worksheetTotal: z.number().optional(), changeOrderTotal: z.number().optional(), insuranceClaimTotal: z.number().optional(), upgradeTotal: z.number().optional(), discountTotal: z.number().optional(), supplementTotal: z.number().optional(), workNotDoingTotal: z.number().optional()}); export const worksheetLink = z.object({id: z.string().optional(), _link: z.string().optional()}); export const worksheetAmendmentLink = z.object({id: z.string().optional(), _link: z.string().optional()}); export const financials = z.object({id: z.string().optional(), _link: z.string().optional(), jobId: z.string().optional(), approvedJobValue: z.number().optional(), balanceDue: z.number().optional(), worksheetSectionTotals: worksheetSectionTotals.optional(), worksheet: z.array(worksheetLink).optional(), amendments: z.array(worksheetAmendmentLink).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 }; /** * Get Financials * * GET /financials/{financialsId} */ export const getApiFinancialsFinancialsId = async (args: GetApiFinancialsFinancialsIdArgs, options?: GetApiFinancialsFinancialsIdOptions) => { const {baseUrl = "https://api.acculynx.com/api/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includes != null) query.append("includes", String(args.includes)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/financials/${encodeURIComponent(String(args.financialsId))}${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(financials, await res.json()) };