import {z} from 'zod' export type Recipients = {emails?: Array | undefined}; export type ChartOfAccountsRequestDto = {exportType?: ('JSON' | 'CSV') | undefined, recipients?: Recipients | undefined, accountingMethod?: ('ACCRUAL' | 'CASH') | undefined}; export type CreateApiV6ReportsChartOfAccountsArgs = {body: ChartOfAccountsRequestDto}; export type CreateApiV6ReportsChartOfAccountsOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const glAccountExpanded = z.object({id: z.string(), name: z.string().optional(), accountNo: z.string().optional(), type: z.enum(["ASSET", "LIABILITY", "EQUITY", "INCOME", "COGS", "EXPENSE", "OTHER_INCOME", "OTHER_EXPENSE", "SUSPENSE"]).optional(), iconUrl: z.string().optional(), displayName: z.string().optional()}); export const chartOfAccountsReportJson: z.ZodType = z.object({name: z.string().optional(), glAccount: glAccountExpanded.optional(), balance: z.number().int().optional(), children: z.array(z.lazy(() => chartOfAccountsReportJson)).optional()}); export const reportMeta = z.object({title: z.string().optional(), generatedOn: z.string().optional(), generatedBy: z.string().optional(), footerLine: z.string().optional()}); export const reportDtoChartOfAccountsReportJson = z.object({json: z.array(chartOfAccountsReportJson).optional(), url: z.string().optional(), meta: reportMeta.optional()}); export const successResponseReportDtoChartOfAccountsReportJson = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: reportDtoChartOfAccountsReportJson.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 }; /** * Chart of Accounts * * POST /v6/reports/chartOfAccounts */ export const createApiV6ReportsChartOfAccounts = async (args: CreateApiV6ReportsChartOfAccountsArgs, options?: CreateApiV6ReportsChartOfAccountsOptions) => { 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/chartOfAccounts${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(successResponseReportDtoChartOfAccountsReportJson, await res.json()) };