import {z} from 'zod' export type GetApiReportsTrialBalanceArgs = {from_date: string, to_date: string, subsidiary_id?: string | undefined, book_id?: (string | null) | undefined}; export type GetApiReportsTrialBalanceOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reportPeriod = z.object({from_date: z.string(), to_date: z.string()}); export const currencyCode = z.string(); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const reportAccountBalance = z.object({id: z.string(), code: z.string(), name: z.string(), beginning_balance: roundedMonetaryAmount, debit_total: roundedMonetaryAmount, credit_total: roundedMonetaryAmount, debit_fx_total: roundedMonetaryAmount.optional(), credit_fx_total: roundedMonetaryAmount.optional(), ending_balance: roundedMonetaryAmount}); export const reportTotals = z.object({beginning_balance: roundedMonetaryAmount, debit_total: roundedMonetaryAmount, credit_total: roundedMonetaryAmount, debit_fx_total: roundedMonetaryAmount.optional(), credit_fx_total: roundedMonetaryAmount.optional(), ending_balance: roundedMonetaryAmount}); export const trialBalanceReport = z.object({period: reportPeriod, currency: currencyCode, accounts: z.array(reportAccountBalance), totals: reportTotals}); 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 }; /** * Retrieves trial balance report * * GET /reports/trial-balance */ export const getApiReportsTrialBalance = async (args: GetApiReportsTrialBalanceArgs, options?: GetApiReportsTrialBalanceOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.subsidiary_id != null) query.append("subsidiary_id", String(args.subsidiary_id)) if (args.book_id != null) query.append("book_id", String(args.book_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/reports/trial-balance${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(trialBalanceReport, await res.json()) };