import {z} from 'zod' export type AccountingCreditMemoLine = {id: number, credit_memo?: number | undefined, account_number: string, account_name: string | null, product_name: string, anrok_item_id: string, department_name: string, tags: Array<{[key: string]: unknown}>, tax_rate_name: string, tax_rate_value: number, description?: (string | null) | undefined, amount?: number | undefined, tax?: number | undefined, tax_description?: (string | null) | undefined, tag_ids?: Array | undefined, tag_group_ids?: Array | undefined, created_at: string, last_modified_at: string, customer: number, product?: (number | null) | undefined, account: number, tax_rate?: (number | null) | undefined, department?: (number | null) | undefined}; export type AccountingCreditMemoPayment = {id: number, payment_journal_entry_order: string, voided_journal_entry_order: string | null, invoice: string, currency?: string | undefined, amount?: number | undefined, payment_date?: (string | null) | undefined, created_at: string, last_modified_at: string, voided_date?: (string | null) | undefined, customer: number, credit_memo: number, payment_journal_entry: number, payment_transaction?: (number | null) | undefined, voided_journal_entry?: (number | null) | undefined}; export type File = {id: number, customer: number, created_at: string, created_by: number | null, created_by_name: string, created_by_email: string, last_modified_at: string, name: string, url: string, s3_content_type?: (string | null) | undefined, s3_content_length?: (number | null) | undefined, s3_path: string, object_id?: (number | null) | undefined, app: string, model: string, is_deleted: boolean, deleted_at: string | null}; export type CreditMemoTypeEnum = 'credit_memo' | 'overpayment'; export type ApplicationStatusEnum = 'open' | 'partially_used' | 'used' | 'voided'; export type BlankEnum = ''; export type NullEnum = unknown; export type AccountingCreditMemo = {id: number, lines: Array, payments: Array, amount_remaining: number, entity_name: string, entity_currency: string, client_name: string, client_email: string, contract_name: string, credit_account_number: string, credit_account_name: string | null, attachments: Array, credit_memo_number?: string | undefined, migrated_journal_id?: (number | null) | undefined, voided_date: string, voided_journal_entry_order: string, last_modified_at: string, is_deleted: boolean, deleted_at: string | null, chat_uuid?: string | undefined, search_vector: string | null, search_text: string | null, credit_memo_type?: CreditMemoTypeEnum | undefined, ref_number?: (string | null) | undefined, credit_memo_date: string, applied_date?: (string | null) | undefined, message_on_credit_memo?: (string | null) | undefined, application_status?: (ApplicationStatusEnum | BlankEnum | NullEnum | null) | undefined, currency?: string | undefined, exchange_rate?: (number | null) | undefined, exchange_rate_book?: (number | null) | undefined, created_at: string, last_sent_at?: (string | null) | undefined, anrok_transaction_id?: (string | null) | undefined, integration_id?: (string | null) | undefined, integration_context: unknown, vat_number?: (string | null) | undefined, total_amount?: number | undefined, amount_used?: number | undefined, chat_id?: (number | null) | undefined, customer: number, entity: number, client?: (number | null) | undefined, credit_account?: (number | null) | undefined, journal_entry?: (number | null) | undefined, contract?: (number | null) | undefined, voided_journal_entry?: (number | null) | undefined, anrok_connection?: (number | null) | undefined, avalara_connection?: (number | null) | undefined, sphere_connection?: (number | null) | undefined}; export type CreateApiCoaApiV1CreditMemoArgs = {body: AccountingCreditMemo}; export type CreateApiCoaApiV1CreditMemoOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const accountingCreditMemoLine = z.object({id: z.number().int(), credit_memo: z.number().int().optional(), account_number: z.string(), account_name: z.string().nullable(), product_name: z.string(), anrok_item_id: z.string(), department_name: z.string(), tags: z.array(z.record(z.string(), z.unknown())), tax_rate_name: z.string(), tax_rate_value: z.number().gt(-1000000).lt(1000000), description: z.string().nullable().optional(), amount: z.number().gt(-1000000000000000000).lt(1000000000000000000).optional(), tax: z.number().gt(-1000000000000000000).lt(1000000000000000000).optional(), tax_description: z.string().max(250).nullable().optional(), tag_ids: z.array(z.number().int().gte(-9223372036854776000).lte(9223372036854776000)).optional(), tag_group_ids: z.array(z.number().int().gte(-9223372036854776000).lte(9223372036854776000)).optional(), created_at: z.string(), last_modified_at: z.string(), customer: z.number().int(), product: z.number().int().nullable().optional(), account: z.number().int(), tax_rate: z.number().int().nullable().optional(), department: z.number().int().nullable().optional()}); export const accountingCreditMemoPayment = z.object({id: z.number().int(), payment_journal_entry_order: z.string(), voided_journal_entry_order: z.string().nullable(), invoice: z.string(), currency: z.string().max(3).optional(), amount: z.number().gt(-1000000000000000000).lt(1000000000000000000).optional(), payment_date: z.string().nullable().optional(), created_at: z.string(), last_modified_at: z.string(), voided_date: z.string().nullable().optional(), customer: z.number().int(), credit_memo: z.number().int(), payment_journal_entry: z.number().int(), payment_transaction: z.number().int().nullable().optional(), voided_journal_entry: z.number().int().nullable().optional()}); export const file = z.object({id: z.number().int(), customer: z.number().int(), created_at: z.string(), created_by: z.number().int().nullable(), created_by_name: z.string(), created_by_email: z.string(), last_modified_at: z.string(), name: z.string(), url: z.string(), s3_content_type: z.string().nullable().optional(), s3_content_length: z.number().int().gte(0).lte(2147483647).nullable().optional(), s3_path: z.string(), object_id: z.number().int().gte(0).lte(2147483647).nullable().optional(), app: z.string(), model: z.string(), is_deleted: z.boolean(), deleted_at: z.string().nullable()}); export const creditMemoTypeEnum = z.enum(["credit_memo", "overpayment"]); export const applicationStatusEnum = z.enum(["open", "partially_used", "used", "voided"]); export const blankEnum = z.literal(""); export const nullEnum = z.unknown(); export const accountingCreditMemo = z.object({id: z.number().int(), lines: z.array(accountingCreditMemoLine), payments: z.array(accountingCreditMemoPayment), amount_remaining: z.number(), entity_name: z.string(), entity_currency: z.string(), client_name: z.string(), client_email: z.string(), contract_name: z.string(), credit_account_number: z.string(), credit_account_name: z.string().nullable(), attachments: z.array(file), credit_memo_number: z.string().optional(), migrated_journal_id: z.number().int().nullable().optional(), voided_date: z.string(), voided_journal_entry_order: z.string(), last_modified_at: z.string(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), chat_uuid: z.string().optional(), search_vector: z.string().nullable(), search_text: z.string().nullable(), credit_memo_type: creditMemoTypeEnum.optional(), ref_number: z.string().max(120).nullable().optional(), credit_memo_date: z.string(), applied_date: z.string().nullable().optional(), message_on_credit_memo: z.string().nullable().optional(), application_status: z.union([applicationStatusEnum, blankEnum, nullEnum]).nullable().optional(), currency: z.string().max(3).optional(), exchange_rate: z.number().gt(-100000000000000).lt(100000000000000).nullable().optional(), exchange_rate_book: z.number().gt(-100000000000000).lt(100000000000000).nullable().optional(), created_at: z.string(), last_sent_at: z.string().nullable().optional(), anrok_transaction_id: z.string().max(255).nullable().optional(), integration_id: z.string().max(250).nullable().optional(), integration_context: z.unknown(), vat_number: z.string().max(250).nullable().optional(), total_amount: z.number().gt(-1000000000000000000).lt(1000000000000000000).optional(), amount_used: z.number().gt(-1000000000000000000).lt(1000000000000000000).optional(), chat_id: z.number().int().gte(-2147483648).lte(2147483647).nullable().optional(), customer: z.number().int(), entity: z.number().int(), client: z.number().int().nullable().optional(), credit_account: z.number().int().nullable().optional(), journal_entry: z.number().int().nullable().optional(), contract: z.number().int().nullable().optional(), voided_journal_entry: z.number().int().nullable().optional(), anrok_connection: z.number().int().nullable().optional(), avalara_connection: z.number().int().nullable().optional(), sphere_connection: z.number().int().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 Credit Memo * * POST /coa/api/v1/credit-memo */ export const createApiCoaApiV1CreditMemo = async (args: CreateApiCoaApiV1CreditMemoArgs, options?: CreateApiCoaApiV1CreditMemoOptions) => { 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/v1/credit-memo${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(accountingCreditMemo, await res.json()) };