import {z} from 'zod' export type PricingModelEnum = 'unit_based' | 'fixed_fee' | 'percentage_based' | 'graduated_tiered' | 'volume_based'; export type ProductUsageTierSummary = {id: number, name: string, pricing_type: string, from_quantity: number, to_quantity: number | null, price: number | null, percentage: number | null}; export type ProductUsageGroupSummary = {id: number, name?: (string | null) | undefined, pricing_model?: PricingModelEnum | undefined, unit_of_measure?: (string | null) | undefined, tier_count: number, tiers: Array}; export type PatchedProduct = {id?: number | undefined, is_deleted?: boolean | undefined, deleted_at?: (string | null) | undefined, department_name?: string | undefined, invoice_account_name?: (string | null) | undefined, revenue_account_name?: (string | null) | undefined, ar_account_name?: (string | null) | undefined, bad_debt_account_name?: (string | null) | undefined, invoice_unbilled_account_name?: (string | null) | undefined, tax_account_name?: string | undefined, tags?: Array<{[key: string]: unknown}> | undefined, last_modified_at?: string | undefined, product_uuid?: string | undefined, display_name?: string | undefined, usage_group_count?: number | undefined, usage_groups?: Array | undefined, product_id?: (string | null) | undefined, product_name?: (string | null) | undefined, product_description?: (string | null) | undefined, tag_ids?: Array | undefined, tag_group_ids?: Array | undefined, currency?: (string | null) | undefined, price?: (number | null) | undefined, cost?: (number | null) | undefined, exclude_from_mrr?: boolean | undefined, is_taxable?: boolean | undefined, is_renewable?: boolean | undefined, recognize_revenue?: boolean | undefined, is_immediate_recognition?: boolean | undefined, created_at?: string | undefined, stripe_product_id?: (string | null) | undefined, avalara_item_id?: (number | null) | undefined, avalara_item_data: unknown, avalara_tax_code?: (string | null) | undefined, anrok_item_id?: (string | null) | undefined, sphere_item_id?: (string | null) | undefined, apply_dept_tag_to_invoice_journals?: (boolean | null) | undefined, customer?: number | undefined, department?: (number | null) | undefined, invoice_account?: (number | null) | undefined, revenue_account?: (number | null) | undefined, ar_account?: (number | null) | undefined, bad_debt_account?: (number | null) | undefined, invoice_unbilled_account?: (number | null) | undefined}; export type PatchApiRrApiV1ProductIdArgs = {id: number, body: PatchedProduct}; export type PatchApiRrApiV1ProductIdOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const pricingModelEnum = z.enum(["unit_based", "fixed_fee", "percentage_based", "graduated_tiered", "volume_based"]); export const productUsageTierSummary = z.object({id: z.number().int(), name: z.string(), pricing_type: z.string(), from_quantity: z.number().gt(-1000000000000000000).lt(1000000000000000000), to_quantity: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable(), price: z.number().gt(-100000000000000).lt(100000000000000).nullable(), percentage: z.number().gt(-100000000000000).lt(100000000000000).nullable()}); export const productUsageGroupSummary = z.object({id: z.number().int(), name: z.string().nullable().optional(), pricing_model: pricingModelEnum.optional(), unit_of_measure: z.string().nullable().optional(), tier_count: z.number().int(), tiers: z.array(productUsageTierSummary)}); export const product = z.object({id: z.number().int(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), department_name: z.string(), invoice_account_name: z.string().nullable(), revenue_account_name: z.string().nullable(), ar_account_name: z.string().nullable(), bad_debt_account_name: z.string().nullable(), invoice_unbilled_account_name: z.string().nullable(), tax_account_name: z.string(), tags: z.array(z.record(z.string(), z.unknown())), last_modified_at: z.string(), product_uuid: z.string(), display_name: z.string(), usage_group_count: z.number().int(), usage_groups: z.array(productUsageGroupSummary), product_id: z.string().max(250).nullable().optional(), product_name: z.string().max(250).nullable().optional(), product_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(), currency: z.string().max(3).nullable().optional(), price: z.number().gt(-100000000000000).lt(100000000000000).nullable().optional(), cost: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), exclude_from_mrr: z.boolean().optional(), is_taxable: z.boolean().optional(), is_renewable: z.boolean().optional(), recognize_revenue: z.boolean().optional(), is_immediate_recognition: z.boolean().optional(), created_at: z.string(), stripe_product_id: z.string().max(250).nullable().optional(), avalara_item_id: z.number().int().gte(-9223372036854776000).lte(9223372036854776000).nullable().optional(), avalara_item_data: z.unknown(), avalara_tax_code: z.string().max(50).nullable().optional(), anrok_item_id: z.string().max(250).nullable().optional(), sphere_item_id: z.string().max(250).nullable().optional(), apply_dept_tag_to_invoice_journals: z.boolean().nullable().optional(), customer: z.number().int(), department: z.number().int().nullable().optional(), invoice_account: z.number().int().nullable().optional(), revenue_account: z.number().int().nullable().optional(), ar_account: z.number().int().nullable().optional(), bad_debt_account: z.number().int().nullable().optional(), invoice_unbilled_account: 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 }; /** * Partial Update Product * * PATCH /rr/api/v1/product/{id} */ export const patchApiRrApiV1ProductId = async (args: PatchApiRrApiV1ProductIdArgs, options?: PatchApiRrApiV1ProductIdOptions) => { 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(/\/+$/, '')}/rr/api/v1/product/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(product, await res.json()) };