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 ContractMilestone = {id: number, customer: number, contract: number, product?: (number | null) | undefined, product_bundle: string, contract_product_bundle: ContractProductBundleRead, product_name: string, product_bundle_name: string, milestone_name: string, milestone_date: string, amount: number, quantity?: number | undefined, rate?: number | undefined, description?: (string | null) | undefined, department?: (number | null) | undefined, department_name: string, tags: Array<{[key: string]: unknown}>, revenue_transaction_id: number, created_at: string, last_modified_at: string, is_deleted: boolean, deleted_at: string | null}; export type UpdateApiRrApiV1ContractsContractIdMilestonesIdArgs = {contract_id: number, id: number, body: ContractMilestone}; export type UpdateApiRrApiV1ContractsContractIdMilestonesIdOptions = {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 contractMilestone = z.object({id: z.number().int(), customer: 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(), milestone_name: z.string().max(250), milestone_date: z.string(), amount: z.number().gt(-1000000000000000000).lt(1000000000000000000), quantity: z.number().gt(-10000000000000000).lt(10000000000000000).optional(), rate: z.number().gt(-10000000000000).lt(10000000000000).optional(), description: z.string().nullable().optional(), department: z.number().int().nullable().optional(), department_name: z.string(), tags: z.array(z.record(z.string(), z.unknown())), revenue_transaction_id: z.number().int(), created_at: z.string(), last_modified_at: z.string(), is_deleted: z.boolean(), deleted_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 Milestone * * PUT /rr/api/v1/contracts/{contract_id}/milestones/{id} */ export const updateApiRrApiV1ContractsContractIdMilestonesId = async (args: UpdateApiRrApiV1ContractsContractIdMilestonesIdArgs, options?: UpdateApiRrApiV1ContractsContractIdMilestonesIdOptions) => { 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))}/milestones/${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(contractMilestone, await res.json()) };