import {z} from 'zod' export type GetApiV6TemplatesChartOfAccountsArgs = {cursor: string | null, limit: number}; export type GetApiV6TemplatesChartOfAccountsOptions = {baseUrl?: string | undefined, PartnerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const templateGlAccount: z.ZodType = z.object({id: z.string(), accountNo: z.string(), name: z.string(), level: z.number().int(), type: z.enum(["ASSET", "LIABILITY", "EQUITY", "INCOME", "COGS", "EXPENSE", "OTHER_INCOME", "OTHER_EXPENSE", "SUSPENSE"]), assetType: z.enum(["CASH", "AR", "OTHER_CURRENT", "FIXED", "OTHER_NON_CURRENT", "FIXED_DEPRECIATION", "SALES_TAX_CREDIT", "DEFAULT_CASH", "CLEARING_ACCOUNT", "OTHER_RECEIVABLE", "SECURITY_DEPOSIT", "INVENTORY", "UNMATCHED_TRANSFERS"]).nullable(), liabilityType: z.enum(["AP", "CREDIT_CARD", "SALES_TAX", "OTHER_CURRENT", "LONG_TERM", "DEFERRED_REVENUE", "PAYROLL", "ADMIN_STAFF_BENEFITS_PAYABLE", "ADMIN_STAFF_BENEFITS_INSURANCE_PAYABLE", "PAYROLL_TAXES_PAYABLE", "ADMIN_STAFF_BENEFITS_RETIREMENT_PAYABLE"]).nullable(), equityType: z.enum(["CAPITAL", "DRAW_OR_DISTRIBUTIONS", "OPENING_BALANCE", "RETAINED_EARNINGS", "PAID_IN_CAPITAL", "PERSONAL_INCOME_AND_EXPENSES", "CONTRIBUTION", "FEDERAL_INCOME_TAX", "STATE_INCOME_TAX", "PERSONAL_INCOME", "OTHER_PERSONAL_EXPENSES"]).nullable(), incomeType: z.enum(["SERVICE_INCOME", "EXPENSE_MARKUP", "DISCOUNT"]).nullable(), expenseType: z.enum(["MERCHANT_FEE", "OTHER", "CONTRACTORS", "ADMIN_STAFF_BENEFITS", "ADMIN_STAFF_PAYROLL", "ADMIN_STAFF_SALARIES", "NON_DEDUCTIBLE_SALES_TAX_PAID", "INCOME_TAX", "REIMBURSABLE_EXPENSES", "UNCATEGORIZED_EXPENSES", "ADMIN_STAFF_BENEFITS_RETIREMENT", "ADMIN_STAFF_BENEFITS_HEALTH_INSURANCE", "FEDERAL_INCOME_TAX", "STATE_INCOME_TAX"]).nullable(), customData: z.object({}).nullable().optional(), children: z.array(z.lazy(() => templateGlAccount))}).nullable(); export const chartOfAccounts = z.object({id: z.string(), name: z.string(), businessTypeId: z.string(), accounts: z.array(templateGlAccount)}); export const successResponseListChartOfAccounts = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: z.array(chartOfAccounts).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 chart of accounts templates * * GET /v6/templates/chartOfAccounts */ export const getApiV6TemplatesChartOfAccounts = async (args: GetApiV6TemplatesChartOfAccountsArgs, options?: GetApiV6TemplatesChartOfAccountsOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, PartnerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (PartnerAuth !== undefined) headers.set('authorization', `Bearer ${PartnerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/templates/chartOfAccounts${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(successResponseListChartOfAccounts, await res.json()) };