import {z} from 'zod' export type Recipients = {emails?: Array | undefined}; export type DateRangeFilters = {beginDate?: string | undefined, endDate?: string | undefined}; export type PeriodGrouping = {period?: ('DAILY' | 'MONTHLY' | 'QUARTERLY' | 'YEARLY' | 'NONE') | undefined, dateGrouping?: ('DAILY' | 'MONTHLY' | 'QUARTERLY' | 'YEARLY' | 'NONE') | undefined}; export type BalanceSheetRequestDto = {exportType?: ('JSON' | 'CSV') | undefined, recipients?: Recipients | undefined, accountingMethod?: ('ACCRUAL' | 'CASH') | undefined, filter?: DateRangeFilters | undefined, grouping?: PeriodGrouping | undefined, dateGrouping?: ('DAILY' | 'MONTHLY' | 'QUARTERLY' | 'YEARLY' | 'NONE') | undefined}; export type CreateApiV6ReportsBalanceSheetArgs = {body: BalanceSheetRequestDto}; export type CreateApiV6ReportsBalanceSheetOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const period = z.object({beginDate: z.string().optional(), endDate: z.string().optional()}); export const balanceSheetLineItem: z.ZodType = z.object({name: z.string().optional(), accountId: z.string().nullable().optional(), accountNo: z.string().optional(), total: z.number().int().optional(), children: z.array(z.lazy(() => balanceSheetLineItem)).optional()}); export const balanceSheetSection = z.object({name: z.string().optional(), total: z.number().int().optional(), children: z.array(balanceSheetLineItem).optional()}); export const liabilitiesAndEquity = z.object({name: z.string().optional(), total: z.number().int().optional(), liabilities: balanceSheetSection.optional(), equity: balanceSheetSection.optional()}); export const balanceSheetJson = z.object({name: z.string().optional(), period: period.optional(), assets: balanceSheetSection.optional(), liabilitiesAndEquity: liabilitiesAndEquity.optional()}); export const reportMeta = z.object({title: z.string().optional(), generatedOn: z.string().optional(), generatedBy: z.string().optional(), footerLine: z.string().optional()}); export const reportDtoBalanceSheetJson = z.object({json: z.array(balanceSheetJson).optional(), url: z.string().optional(), meta: reportMeta.optional()}); export const successResponseReportDtoBalanceSheetJson = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: reportDtoBalanceSheetJson.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 }; /** * Balance Sheet * * POST /v6/reports/balanceSheet */ export const createApiV6ReportsBalanceSheet = async (args: CreateApiV6ReportsBalanceSheetArgs, options?: CreateApiV6ReportsBalanceSheetOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, EmbedToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (EmbedToken !== undefined) headers.set('authorization', `Bearer ${EmbedToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/reports/balanceSheet${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(successResponseReportDtoBalanceSheetJson, await res.json()) };