import {z} from 'zod' export type ItaPayerStatusEnum = '1' | '2' | '3' | '4' | '5'; export type BlankEnum = ''; export type NullEnum = unknown; export type ChartEntity = {id: number, customer: number, parent?: (number | null) | undefined, parent_name: string, elimination_entity?: (number | null) | undefined, primary_entity?: (number | null) | undefined, primary_entity_name: string, is_adjusting: boolean, is_elimination_entity?: boolean | undefined, is_deleted: boolean, deleted_at: string | null, last_modified_at: string, name?: (string | null) | undefined, description?: (string | null) | undefined, currency?: string | undefined, address_1?: (string | null) | undefined, address_2?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, zip_code?: (string | null) | undefined, country?: (string | null) | undefined, disable_service_date?: boolean | undefined, logo_url?: (string | null) | undefined, active?: boolean | undefined, fiscal_year_month?: number | undefined, fiscal_year_day?: number | undefined, created_at: string | null, enable_mid_month_convention?: (boolean | null) | undefined, mid_month_threshold?: (number | null) | undefined, use_whole_month_accounting_for_prepaids?: (boolean | null) | undefined, allow_payments_in_closed_periods?: boolean | undefined, auto_apply_catchup_for_closed_periods?: boolean | undefined, lineage_array?: (Array | null) | undefined, tax_identification_number?: (string | null) | undefined, representative_name?: (string | null) | undefined, business_type?: (string | null) | undefined, business_category?: (string | null) | undefined, invoice_name: string | null, invoice_email: string | null, invoice_message: string | null, invoice_email_subject: string | null, invoice_email_body: string | null, invoice_prefix: string | null, invoice_address: string | null, invoice_cc_emails: string | null, invoice_display_settings: {[key: string]: unknown} | null, invoice_email_attachments: {[key: string]: unknown} | null, withholding_scheme?: (number | null) | undefined, withholding_scheme_name: string, withholding_certificate?: (number | null) | undefined, withholding_certificate_number: string, ita_deduction_file_number?: (string | null) | undefined, ita_payer_status?: (ItaPayerStatusEnum | BlankEnum | NullEnum | null) | undefined}; export type CreateApiCoaApiEntityArgs = {body: ChartEntity}; export type CreateApiCoaApiEntityOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const itaPayerStatusEnum = z.enum(["1", "2", "3", "4", "5"]); export const blankEnum = z.literal(""); export const nullEnum = z.unknown(); export const chartEntity = z.object({id: z.number().int(), customer: z.number().int(), parent: z.number().int().nullable().optional(), parent_name: z.string(), elimination_entity: z.number().int().nullable().optional(), primary_entity: z.number().int().nullable().optional(), primary_entity_name: z.string(), is_adjusting: z.boolean(), is_elimination_entity: z.boolean().optional(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), last_modified_at: z.string(), name: z.string().max(250).nullable().optional(), description: z.string().nullable().optional(), currency: z.string().max(3).optional(), address_1: z.string().max(120).nullable().optional(), address_2: z.string().max(120).nullable().optional(), city: z.string().max(120).nullable().optional(), state: z.string().max(120).nullable().optional(), zip_code: z.string().max(120).nullable().optional(), country: z.string().max(120).nullable().optional(), disable_service_date: z.boolean().optional(), logo_url: z.string().max(500).nullable().optional(), active: z.boolean().optional(), fiscal_year_month: z.number().int().gte(-2147483648).lte(2147483647).optional(), fiscal_year_day: z.number().int().gte(-2147483648).lte(2147483647).optional(), created_at: z.string().nullable(), enable_mid_month_convention: z.boolean().nullable().optional(), mid_month_threshold: z.number().int().gte(0).lte(31).nullable().optional(), use_whole_month_accounting_for_prepaids: z.boolean().nullable().optional(), allow_payments_in_closed_periods: z.boolean().optional(), auto_apply_catchup_for_closed_periods: z.boolean().optional(), lineage_array: z.array(z.string().max(50)).nullable().optional(), tax_identification_number: z.string().max(250).nullable().optional(), representative_name: z.string().max(250).nullable().optional(), business_type: z.string().max(250).nullable().optional(), business_category: z.string().max(250).nullable().optional(), invoice_name: z.string().nullable(), invoice_email: z.string().nullable(), invoice_message: z.string().nullable(), invoice_email_subject: z.string().nullable(), invoice_email_body: z.string().nullable(), invoice_prefix: z.string().nullable(), invoice_address: z.string().nullable(), invoice_cc_emails: z.string().nullable(), invoice_display_settings: z.record(z.string(), z.unknown()).nullable(), invoice_email_attachments: z.record(z.string(), z.unknown()).nullable(), withholding_scheme: z.number().int().nullable().optional(), withholding_scheme_name: z.string(), withholding_certificate: z.number().int().nullable().optional(), withholding_certificate_number: z.string(), ita_deduction_file_number: z.string().max(9).nullable().optional(), ita_payer_status: z.union([itaPayerStatusEnum, blankEnum, nullEnum]).nullable().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 Entity * * POST /coa/api/entity */ export const createApiCoaApiEntity = async (args: CreateApiCoaApiEntityArgs, options?: CreateApiCoaApiEntityOptions) => { 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/entity${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(chartEntity, await res.json()) };