import {z} from 'zod' export type TemplateGlAccount = {id: string, accountNo: string, name: string, level: number, type: 'ASSET' | 'LIABILITY' | 'EQUITY' | 'INCOME' | 'COGS' | 'EXPENSE' | 'OTHER_INCOME' | 'OTHER_EXPENSE' | 'SUSPENSE', assetType: 'CASH' | 'AR' | 'OTHER_CURRENT' | 'FIXED' | 'OTHER_NON_CURRENT' | 'FIXED_DEPRECIATION' | 'SALES_TAX_CREDIT' | 'DEFAULT_CASH' | 'CLEARING_ACCOUNT' | 'OTHER_RECEIVABLE' | 'SECURITY_DEPOSIT' | 'INVENTORY' | 'UNMATCHED_TRANSFERS' | null, liabilityType: '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' | null, equityType: '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' | null, incomeType: 'SERVICE_INCOME' | 'EXPENSE_MARKUP' | 'DISCOUNT' | null, expenseType: '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' | null, customData?: (Record | null) | undefined, children: Array} | null; export type ChartOfAccounts = {id: string, name: string, businessTypeId: string, accounts: Array}; export type CreateApiV6TemplatesChartOfAccountsArgs = {body: Array}; export type CreateApiV6TemplatesChartOfAccountsOptions = {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 }; /** * Save chart of accounts templates * * POST /v6/templates/chartOfAccounts */ export const createApiV6TemplatesChartOfAccounts = async (args: CreateApiV6TemplatesChartOfAccountsArgs, options?: CreateApiV6TemplatesChartOfAccountsOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, PartnerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(successResponseListChartOfAccounts, await res.json()) };