import {z} from 'zod' export type ProrationMethodEnum = 'standard' | 'thirty_day' | 'daily'; export type NullEnum = unknown; export type GenerageSubscriptionSchedule = {start_date: string, end_date?: (string | null) | undefined, total_value: number, mrr?: number | undefined, use_daily_accounting?: boolean | undefined, proration_method?: (ProrationMethodEnum | NullEnum | null) | undefined, use_catchup?: boolean | undefined, catchup_date?: (string | null) | undefined, modified_subscription?: (number | null) | undefined, does_modify_contract_end_date?: boolean | undefined, apply_prospectively?: boolean | undefined}; export type CreateApiRrApiV1ModifySubscriptionScheduleArgs = {body: GenerageSubscriptionSchedule}; export type CreateApiRrApiV1ModifySubscriptionScheduleOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const contractSubscriptionLine = z.object({id: z.number().int().nullable().optional(), start_date: z.string(), end_date: z.string(), original_value: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), total_value: z.number().gt(-1000000000000000000).lt(1000000000000000000), original_mrr: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), mrr: z.number().gt(-1000000000000000000).lt(1000000000000000000).optional(), original_quantity: z.number().gt(-100000000000000).lt(100000000000000).nullable().optional(), quantity: z.number().gt(-100000000000000).lt(100000000000000).nullable().optional(), original_rate: z.number().gt(-10000000000000).lt(10000000000000).nullable().optional(), rate: z.number().gt(-10000000000000).lt(10000000000000).nullable().optional(), use_daily_accounting: z.boolean().optional(), is_contract_amendment: z.boolean().optional()}); export const createApiRrApiV1ModifySubscriptionScheduleResponse = z.array(contractSubscriptionLine); 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 }; /** * Modify Subscription Schedule * * POST /rr/api/v1/modify-subscription-schedule */ export const createApiRrApiV1ModifySubscriptionSchedule = async (args: CreateApiRrApiV1ModifySubscriptionScheduleArgs, options?: CreateApiRrApiV1ModifySubscriptionScheduleOptions) => { 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/modify-subscription-schedule${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(createApiRrApiV1ModifySubscriptionScheduleResponse, await res.json()) };