import {z} from 'zod' export type InlineMetadataValues = {[key: string]: string | null}; export type ChartOfAccountsAccountRequest = {metadata?: (InlineMetadataValues | null) | undefined, name: string, code: string, type: 'ASSET' | 'LIABILITY' | 'EQUITY' | 'EXPENSE' | 'INCOME', subtype: 'Revenue' | 'Cost of Goods Sold' | 'Operating Expense' | 'Equity' | 'Equity, Compensation' | 'Other Current Liability' | 'Other Long Term Liability' | 'Accounts Payable' | 'Accounts Receivable' | 'Credit Card' | 'Fixed Assets' | 'Accumulated Depreciation' | 'Other Current Asset' | 'Bank' | 'Allowance for Doubtful Accounts' | 'Unbilled Revenue' | 'Deferred Revenue' | 'Prepaid Expense' | 'Deferred Commissions' | 'Right of Use Asset' | 'Other Assets' | 'Restricted Cash' | 'Accrued Expense' | 'Sales Tax Liability' | 'Debt' | 'Lease Liability' | 'Customer Deposits' | 'Customer Credit' | 'Additional Paid In Capital' | 'Accumulated Other Comprehensive Income' | 'Retained Earnings' | 'Operating Expense - Personnel' | 'Operating Expense - Office Expenses' | 'Operating Expense - Sales & Marketing' | 'Operating Expense - Commissions' | 'Operating Expense - Research & Development' | 'Non Operating Expense' | 'Non Operating Income', intercompany: boolean, parent_grouping?: string | undefined, prepaid_account_code?: string | undefined}; export type CreateChartOfAccountsRequest = {accounts: Array}; export type CreateApiChartOfAccountsArgs = {body: CreateChartOfAccountsRequest}; export type CreateApiChartOfAccountsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accountCode = z.string(); export const account = z.object({metadata: z.record(z.string(), z.string()).nullable().optional(), id: z.string(), code: accountCode, name: z.string(), type: z.enum(["ASSET", "LIABILITY", "EQUITY", "EXPENSE", "INCOME"]), subtype: z.string(), status: z.enum(["ACTIVE", "INACTIVE"]), intercompany: z.boolean(), parent_grouping: z.string(), prepaid_account_code: z.string().nullable().optional(), updated_at: z.string()}); export const createApiChartOfAccountsResponse = z.object({accounts: z.array(account)}); 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 }; /** * Creates a chart of accounts * * POST /chart-of-accounts */ export const createApiChartOfAccounts = async (args: CreateApiChartOfAccountsArgs, options?: CreateApiChartOfAccountsOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/chart-of-accounts${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(createApiChartOfAccountsResponse, await res.json()) };