import {z} from 'zod' export type GetApiV1AccountingCompanyArgs = {cursor?: (string | null) | undefined, updated_after?: (string | null) | undefined, code?: (string | null) | undefined, 'code:in'?: (string | null) | undefined, accounting_customer_code?: (string | null) | undefined, 'accounting_customer_code:in'?: (string | null) | undefined}; export type GetApiV1AccountingCompanyOptions = {baseUrl?: string | undefined, oAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const taxTypeEnum = z.enum(["VAT", "REVERSE_CHARGE", "UK_REVERSE_CHARGE", "TRANSIT", "NO_VAT", "CO_CONTRACTING", "OTHER"]); export const customerStatus = z.enum(["PROSPECT", "NOT_APPROVED", "APPROVED", "WARNING", "BLOCKED"]); export const locationOutput = z.object({code: z.string().nullable().optional(), display_name: z.string().nullable().optional(), external_id: z.string().nullable().optional(), address: z.string().nullable().optional(), address_second_line: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.string().nullable().optional(), postal_code: z.string().nullable().optional(), name: z.string().nullable().optional(), id: z.string().nullable().optional(), latitude: z.number().nullable().optional(), longitude: z.number().nullable().optional()}); export const paymentTermCode = z.enum(["END_OF_MONTH_2", "END_OF_MONTH_1", "END_OF_MONTH_0_NET_90", "END_OF_MONTH_0_NET_75", "END_OF_MONTH_0_NET_70", "END_OF_MONTH_0_NET_65", "END_OF_MONTH_0_NET_60", "END_OF_MONTH_0_NET_50", "END_OF_MONTH_0_NET_45", "END_OF_MONTH_0_NET_35", "END_OF_MONTH_0_NET_31", "END_OF_MONTH_0_NET_30", "END_OF_MONTH_0_NET_25", "END_OF_MONTH_0_NET_21", "END_OF_MONTH_0_NET_20", "END_OF_MONTH_0_NET_14", "END_OF_MONTH_0_NET_7", "END_OF_MONTH_0_NET_1", "END_OF_MONTH_0", "PREPAID", "CUSTOM_DUE_DATE", "UPON_RECEIPT", "NET_1", "NET_2", "NET_3", "NET_5", "NET_7", "NET_8", "NET_10", "NET_12", "NET_14", "NET_15", "NET_20", "NET_21", "NET_28", "NET_30", "NET_35", "NET_40", "NET_42", "NET_45", "NET_48", "NET_49", "NET_50", "NET_52", "NET_55", "NET_56", "NET_60", "NET_63", "NET_70", "NET_75", "NET_77", "NET_80", "NET_84", "NET_90", "NET_91", "NET_98", "NET_120"]); export const basePaymentTerm = z.object({code: paymentTermCode}); export const creditPolicyOutput = z.object({limit: z.number().nullable().optional()}); export const apiAccountingModelsCompanyCompanyReferenceOutput = z.object({id: z.string(), name: z.string(), code: z.string().nullable().optional()}); export const companyReferenceNumbers = z.object({eori_number: z.string().nullable().optional(), duns_number: z.string().nullable().optional(), scac_code: z.string().nullable().optional(), company_registration_number: z.string().nullable().optional(), tss_registration_number: z.string().nullable().optional()}); export const bankAccount = z.object({bic_code: z.string().nullable().optional(), iban_code: z.string().nullable().optional()}); export const contactType = z.enum(["BILLING", "OPERATIONS", "BOOKINGS", "NOTIFICATIONS", "CLEANING", "HEATING", "STORAGE", "CONTAINER_RELEASE", "DRIVER", "OTHER", "CMR_PROCESSING", "PAYMENT_REMINDERS", "CLAIMS_INSURANCE", "CUSTOMS", "IT", "QUALITY", "SALES", "CUSTOMER_CONTACT"]); export const companyContactOutput = z.object({id: z.string().nullable().optional(), roles: z.array(contactType).optional(), name: z.string().nullable().optional(), note: z.string().nullable().optional(), email: z.string().nullable().optional(), phone_number: z.string().nullable().optional()}); export const companyBillingEntity = z.object({id: z.string().nullable().optional(), code: z.string().nullable().optional(), accounting_code: z.string().nullable().optional()}); export const salesCompanyFieldsOutput = z.object({payment_term: basePaymentTerm.nullable().optional(), billing_entities: z.array(companyBillingEntity).nullable().optional(), status: customerStatus.nullable().optional()}); export const subcontractorStatus = z.enum(["NOT_APPROVED", "APPROVED", "WARNING", "BLOCKED"]); export const purchaseCompanyFieldsOutput = z.object({payment_term: basePaymentTerm.nullable().optional(), billing_entities: z.array(companyBillingEntity).nullable().optional(), status: subcontractorStatus.nullable().optional()}); export const companyDetailOutput = z.object({id: z.string(), code: z.string().nullable().optional(), currency: z.string(), custom_fields: z.object({}).nullable().optional(), is_customer: z.boolean().optional(), is_subcontractor: z.boolean().optional(), is_buyer_or_seller: z.boolean().nullable().optional(), name: z.string(), legal_name: z.string().nullable().optional(), vat_number: z.string().nullable().optional(), note: z.string().nullable().optional(), tax_type: taxTypeEnum.nullable().optional(), status: customerStatus.optional(), accounting_code: z.string().nullable().optional(), billing_location: locationOutput.nullable().optional(), payment_term: basePaymentTerm.nullable().optional(), locale: z.string(), archived_customer: z.boolean(), archived_subcontractor: z.boolean(), archived_buyer_or_seller: z.boolean(), credit_policy: creditPolicyOutput.nullable().optional(), default_invoice_customer: apiAccountingModelsCompanyCompanyReferenceOutput.nullable().optional(), reference_numbers: companyReferenceNumbers.optional(), bank_accounts: z.array(bankAccount).optional(), contacts: z.array(companyContactOutput).nullable().optional(), sales: salesCompanyFieldsOutput.optional(), purchase: purchaseCompanyFieldsOutput.optional(), timestamp_updated: z.string(), is_blocked: z.boolean().optional(), credit_limit_total: z.number().nullable().optional()}); export const listCompanyOutput = z.object({next_cursor: z.string().nullable().optional(), items: z.array(companyDetailOutput)}); 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 }; /** * List Companies (Deprecated) * * GET /v1/accounting/company */ export const getApiV1AccountingCompany = async (args: GetApiV1AccountingCompanyArgs, options?: GetApiV1AccountingCompanyOptions) => { const {baseUrl = "https://api.qargo.com", fetch: fetchFn = fetch, oAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.code != null) query.append("code", String(args.code)) if (args['code:in'] != null) query.append("code:in", String(args['code:in'])) if (args.accounting_customer_code != null) query.append("accounting_customer_code", String(args.accounting_customer_code)) if (args['accounting_customer_code:in'] != null) query.append("accounting_customer_code:in", String(args['accounting_customer_code:in'])) if (oAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounting/company${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listCompanyOutput, await res.json()) };