import {z} from 'zod' export type GetApiCoaApiV1InvoiceReportPreviewArgs = {aggregation_fields?: string | undefined, client_id?: string | undefined, contract_id?: string | undefined, currency?: string | undefined, department_id?: string | undefined, end_date?: string | undefined, entity_id?: string | undefined, group_fields?: string | undefined, group_keys?: string | undefined, include_totals?: boolean | undefined, limit?: number | undefined, mode?: string | undefined, offset?: number | undefined, payment_status?: string | undefined, pivot_fields?: string | undefined, product_id?: string | undefined, shouldPivot?: boolean | undefined, start_date?: string | undefined}; export type GetApiCoaApiV1InvoiceReportPreviewOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceReportResultRow = z.object({filter_params: z.record(z.string(), z.unknown()), group_id: z.number().int(), id: z.number().int()}); export const engineUsedEnum = z.literal("postgres"); export const invoiceReportResponse = z.object({count: z.number().int(), next: z.string().nullable(), previous: z.string().nullable(), results: z.array(invoiceReportResultRow), engine_used: engineUsedEnum, pivot_result_fields: z.array(z.string()).optional(), totals_row: z.record(z.string(), z.unknown()).optional(), pivot_value_mappings: z.record(z.string(), z.unknown()).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 }; /** * Preview invoice report * * GET /coa/api/v1/invoice/report/preview */ export const getApiCoaApiV1InvoiceReportPreview = async (args: GetApiCoaApiV1InvoiceReportPreviewArgs, options?: GetApiCoaApiV1InvoiceReportPreviewOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.aggregation_fields != null) query.append("aggregation_fields", String(args.aggregation_fields)) if (args.client_id != null) query.append("client_id", String(args.client_id)) if (args.contract_id != null) query.append("contract_id", String(args.contract_id)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.department_id != null) query.append("department_id", String(args.department_id)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.entity_id != null) query.append("entity_id", String(args.entity_id)) if (args.group_fields != null) query.append("group_fields", String(args.group_fields)) if (args.group_keys != null) query.append("group_keys", String(args.group_keys)) if (args.include_totals != null) query.append("include_totals", String(args.include_totals)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.payment_status != null) query.append("payment_status", String(args.payment_status)) if (args.pivot_fields != null) query.append("pivot_fields", String(args.pivot_fields)) if (args.product_id != null) query.append("product_id", String(args.product_id)) if (args.shouldPivot != null) query.append("shouldPivot", String(args.shouldPivot)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/coa/api/v1/invoice/report/preview${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(invoiceReportResponse, await res.json()) };