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 ContractUsage = {id: number, contract: number, product?: (number | null) | undefined, product_bundle: string, contract_product_bundle: ContractProductBundleRead, product_name: string, product_bundle_name: string, usage_group?: (number | null) | undefined, usage_group_name: string, department?: (number | null) | undefined, tags?: string | undefined, description?: (string | null) | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, lines: string, created_at: string | null, last_modified_at: string | null}; export type UpdateApiRrApiV1ContractsContractIdUsageIdArgs = {contract_id: number, id: number, body: ContractUsage}; export type UpdateApiRrApiV1ContractsContractIdUsageIdOptions = {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 contractUsage = z.object({id: z.number().int(), contract: z.number().int(), product: z.number().int().nullable().optional(), product_bundle: z.string(), contract_product_bundle: contractProductBundleRead, product_name: z.string(), product_bundle_name: z.string(), usage_group: z.number().int().nullable().optional(), usage_group_name: z.string(), department: z.number().int().nullable().optional(), tags: z.string().optional(), description: z.string().nullable().optional(), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), lines: z.string(), created_at: z.string().nullable(), last_modified_at: z.string().nullable()}); 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 }; /** * Update Contract Usage Revenue * * PUT /rr/api/v1/contracts/{contract_id}/usage/{id} */ export const updateApiRrApiV1ContractsContractIdUsageId = async (args: UpdateApiRrApiV1ContractsContractIdUsageIdArgs, options?: UpdateApiRrApiV1ContractsContractIdUsageIdOptions) => { 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))}/usage/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(contractUsage, await res.json()) };