import {z} from 'zod' export type Status372Enum = 'ACTIVE' | 'INACTIVE'; export type AccountTypeEnum = 'ASSET' | 'LIABILITY' | 'EQUITY' | 'REVENUE' | 'COGS' | 'OPERATING_EXPENSES' | 'OTHER_INCOME' | 'OTHER_EXPENSE' | 'UNCATEGORIZED' | 'COST_ALLOCATION'; export type AccountSubtypeEnum = 'BANK' | 'ACCOUNTS_RECEIVABLE' | 'OTHER_CURRENT_ASSET' | 'FIXED_ASSET' | 'OTHER_ASSET' | 'ACCOUNTS_PAYABLE' | 'UNAPPLIED_CREDITS' | 'CREDIT_CARD' | 'ACCRUED_REVENUE' | 'DEFERRED_REVENUE' | 'OTHER_CURRENT_LIABILITY' | 'LONG_TERM_LIABILITY' | 'UNBILLED_RECEIVABLE' | 'EQUITY' | 'RETAINED_EARNINGS' | 'INCOME' | 'COST_OF_GOODS_SOLD' | 'EXPENSE' | 'DEFERRED_EXPENSE' | 'OTHER_INCOME' | 'OTHER_EXPENSE' | 'REALIZED_GAIN_LOSS' | 'UNREALIZED_GAIN_LOSS' | 'PREPAID' | 'COST_ALLOCATION' | 'UNCATEGORIZED'; export type AccountCashflowClassificationEnum = 'NONE' | 'OPERATING' | 'INVESTING' | 'FINANCING'; export type ChartAccount = {id: number, is_deleted: boolean, deleted_at: string | null, reconciliation_ending_date: string, reconciliation_ending_balance: number, balance_ytd: number, name_and_number: string, parent_name_and_number: string | null, sort_key: string, has_associated_data: boolean, last_modified_at: string, account_id: string, number: string | null, currency?: string | undefined, name?: (string | null) | undefined, nickname?: (string | null) | undefined, description?: (string | null) | undefined, status?: Status372Enum | undefined, account_type?: AccountTypeEnum | undefined, account_subtype?: AccountSubtypeEnum | undefined, account_cashflow_classification?: AccountCashflowClassificationEnum | undefined, include_in_eliminations?: boolean | undefined, include_in_revaluations?: boolean | undefined, protected?: boolean | undefined, skip_subledger_validation?: boolean | undefined, external_id: string | null, created_at: string, last_recompute_balance_at?: (string | null) | undefined, lock_recompute_balance_key?: (string | null) | undefined, current_balance?: number | undefined, current_balance_book?: number | undefined, lineage_array?: (Array | null) | undefined, customer: number, parent?: (number | null) | undefined, parent_bank_account?: (number | null) | undefined, default_department?: (number | null) | undefined, default_tags?: Array | undefined}; export type CreateApiCoaApiAccountArgs = {body: ChartAccount}; export type CreateApiCoaApiAccountOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const status372Enum = z.enum(["ACTIVE", "INACTIVE"]); export const accountTypeEnum = z.enum(["ASSET", "LIABILITY", "EQUITY", "REVENUE", "COGS", "OPERATING_EXPENSES", "OTHER_INCOME", "OTHER_EXPENSE", "UNCATEGORIZED", "COST_ALLOCATION"]); export const accountSubtypeEnum = z.enum(["BANK", "ACCOUNTS_RECEIVABLE", "OTHER_CURRENT_ASSET", "FIXED_ASSET", "OTHER_ASSET", "ACCOUNTS_PAYABLE", "UNAPPLIED_CREDITS", "CREDIT_CARD", "ACCRUED_REVENUE", "DEFERRED_REVENUE", "OTHER_CURRENT_LIABILITY", "LONG_TERM_LIABILITY", "UNBILLED_RECEIVABLE", "EQUITY", "RETAINED_EARNINGS", "INCOME", "COST_OF_GOODS_SOLD", "EXPENSE", "DEFERRED_EXPENSE", "OTHER_INCOME", "OTHER_EXPENSE", "REALIZED_GAIN_LOSS", "UNREALIZED_GAIN_LOSS", "PREPAID", "COST_ALLOCATION", "UNCATEGORIZED"]); export const accountCashflowClassificationEnum = z.enum(["NONE", "OPERATING", "INVESTING", "FINANCING"]); export const chartAccount = z.object({id: z.number().int(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), reconciliation_ending_date: z.string(), reconciliation_ending_balance: z.number().gt(-1000000000000000000).lt(1000000000000000000), balance_ytd: z.number().gt(-1000000000000000000).lt(1000000000000000000), name_and_number: z.string(), parent_name_and_number: z.string().nullable(), sort_key: z.string(), has_associated_data: z.boolean(), last_modified_at: z.string(), account_id: z.string(), number: z.string().max(12).nullable(), currency: z.string().max(3).optional(), name: z.string().max(250).nullable().optional(), nickname: z.string().max(250).nullable().optional(), description: z.string().nullable().optional(), status: status372Enum.optional(), account_type: accountTypeEnum.optional(), account_subtype: accountSubtypeEnum.optional(), account_cashflow_classification: accountCashflowClassificationEnum.optional(), include_in_eliminations: z.boolean().optional(), include_in_revaluations: z.boolean().optional(), protected: z.boolean().optional(), skip_subledger_validation: z.boolean().optional(), external_id: z.string().nullable(), created_at: z.string(), last_recompute_balance_at: z.string().nullable().optional(), lock_recompute_balance_key: z.string().max(250).nullable().optional(), current_balance: z.number().gt(-1000000000000000000).lt(1000000000000000000).optional(), current_balance_book: z.number().gt(-1000000000000000000).lt(1000000000000000000).optional(), lineage_array: z.array(z.string().max(50)).nullable().optional(), customer: z.number().int(), parent: z.number().int().nullable().optional(), parent_bank_account: z.number().int().nullable().optional(), default_department: z.number().int().nullable().optional(), default_tags: z.array(z.number().int()).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 }; /** * Create Chart Account * * POST /coa/api/account */ export const createApiCoaApiAccount = async (args: CreateApiCoaApiAccountArgs, options?: CreateApiCoaApiAccountOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/coa/api/account${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(chartAccount, await res.json()) };