import {z} from 'zod' export type ContractProductBundleLineRead = {id: number, product: number, product_name: string, amount: number, percentage: string, original_percentage?: (number | null) | undefined}; export type ContractProductBundleRead = {id: number, source_bundle?: (number | null) | undefined, source_bundle_name: string | null, total_amount: number, currency?: string | undefined, lines: Array}; export type RenewalBehaviorEnum = 'FOLLOW_CONTRACT' | 'FIXED_END'; export type ProrationMethodEnum = 'standard' | 'thirty_day' | 'daily'; export type InvoiceCalculationEnum = 'FIXED_RATE' | 'FIXED_QUANTITY'; export type SourceFc1Enum = 'SALESFORCE' | 'HUBSPOT' | 'STRIPE' | 'MANUAL'; export type ContractSubscription = {id: number, is_deleted: boolean, deleted_at: string | null, lines: string, product_name: string, product_is_renewable: boolean | null, product_bundle: string, product_bundle_name: string, contract_product_bundle: ContractProductBundleRead, total_value: number, last_modified_at: string, rate?: (number | null) | undefined, quantity?: (number | null) | undefined, renewal_behavior?: RenewalBehaviorEnum | undefined, period_amount?: (number | null) | undefined, invoiced_amount_migration?: (number | null) | undefined, is_contract_amendment?: boolean | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, mrr?: (number | null) | undefined, discount?: (number | null) | undefined, notes?: (string | null) | undefined, use_daily_accounting?: boolean | undefined, proration_method?: ProrationMethodEnum | undefined, use_catchup?: boolean | undefined, catchup_date?: (string | null) | undefined, catchup_manually_overridden?: boolean | undefined, invoice_calculation?: InvoiceCalculationEnum | undefined, created_at: string, source?: SourceFc1Enum | undefined, external_id?: (string | null) | undefined, rates_are_net?: boolean | undefined, customer: number, contract: number, product?: (number | null) | undefined, modified_subscription?: (number | null) | undefined}; export type CreateApiRrApiV1ContractsContractIdSubscriptionsArgs = {contract_id: number, body: ContractSubscription}; export type CreateApiRrApiV1ContractsContractIdSubscriptionsOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const contractProductBundleLineRead = z.object({id: z.number().int(), product: z.number().int(), product_name: z.string(), amount: z.number().gt(-1000000000000000000).lt(1000000000000000000), percentage: z.string(), original_percentage: z.number().gt(-1000).lt(1000).nullable().optional()}); export const contractProductBundleRead = z.object({id: z.number().int(), source_bundle: z.number().int().nullable().optional(), source_bundle_name: z.string().nullable(), total_amount: z.number().gt(-1000000000000000000).lt(1000000000000000000), currency: z.string().max(3).optional(), lines: z.array(contractProductBundleLineRead)}); export const renewalBehaviorEnum = z.enum(["FOLLOW_CONTRACT", "FIXED_END"]); export const prorationMethodEnum = z.enum(["standard", "thirty_day", "daily"]); export const invoiceCalculationEnum = z.enum(["FIXED_RATE", "FIXED_QUANTITY"]); export const sourceFc1Enum = z.enum(["SALESFORCE", "HUBSPOT", "STRIPE", "MANUAL"]); export const contractSubscription = z.object({id: z.number().int(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), lines: z.string(), product_name: z.string(), product_is_renewable: z.boolean().nullable(), product_bundle: z.string(), product_bundle_name: z.string(), contract_product_bundle: contractProductBundleRead, total_value: z.number().gt(-1000000000000000000).lt(1000000000000000000), last_modified_at: z.string(), rate: z.number().gt(-10000000000000).lt(10000000000000).nullable().optional(), quantity: z.number().gt(-100000000000000).lt(100000000000000).nullable().optional(), renewal_behavior: renewalBehaviorEnum.optional(), period_amount: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), invoiced_amount_migration: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), is_contract_amendment: z.boolean().optional(), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), mrr: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), discount: z.number().gte(0).lt(10000000000000).nullable().optional(), notes: z.string().nullable().optional(), use_daily_accounting: z.boolean().optional(), proration_method: prorationMethodEnum.optional(), use_catchup: z.boolean().optional(), catchup_date: z.string().nullable().optional(), catchup_manually_overridden: z.boolean().optional(), invoice_calculation: invoiceCalculationEnum.optional(), created_at: z.string(), source: sourceFc1Enum.optional(), external_id: z.string().max(250).nullable().optional(), rates_are_net: z.boolean().optional(), customer: z.number().int(), contract: z.number().int(), product: z.number().int().nullable().optional(), modified_subscription: 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 Contract Subscription * * POST /rr/api/v1/contracts/{contract_id}/subscriptions */ export const createApiRrApiV1ContractsContractIdSubscriptions = async (args: CreateApiRrApiV1ContractsContractIdSubscriptionsArgs, options?: CreateApiRrApiV1ContractsContractIdSubscriptionsOptions) => { 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/contracts/${encodeURIComponent(String(args.contract_id))}/subscriptions${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(contractSubscription, await res.json()) };