import {z} from 'zod' export type OpportunityUpdateRequest = {name?: (string | null) | undefined, stage?: (string | null) | undefined, value_of_sale?: (number | null) | undefined, probability_pcnt?: (number | null) | undefined, expected_close_date?: (number | null) | undefined, marketing_source?: (string | null) | undefined, business_need_description?: (string | null) | undefined, product_dict_list?: (Array | null) | undefined}; export type PatchApiCrmOpportunitiesOpportunityIdArgs = {opportunity_id: string, body: OpportunityUpdateRequest}; export type PatchApiCrmOpportunitiesOpportunityIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const dealResponse = z.object({id: z.string(), name: z.string(), stage: z.string(), marketing_source: z.string().nullable().optional(), agent_name: z.string().nullable().optional(), agent_email: z.string().nullable().optional(), agent_phone: z.string().nullable().optional(), contact_name: z.string().nullable().optional(), contact_email: z.string().nullable().optional(), contact_phone: z.string().nullable().optional(), account_name: z.string().nullable().optional(), account_email: z.string().nullable().optional(), product_dict_list: z.array(z.unknown()).nullable().optional(), value_of_sale: z.string().nullable().optional(), estimated_value_of_sale: z.string().nullable().optional(), created_date: z.string().nullable().optional(), updated_date: z.string().nullable().optional(), expected_close_date: z.string().nullable().optional(), close_date: z.string().nullable().optional(), renewal_date: z.string().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 }; /** * Patch Opportunity * * PATCH /crm/opportunities/{opportunity_id} */ export const patchApiCrmOpportunitiesOpportunityId = async (args: PatchApiCrmOpportunitiesOpportunityIdArgs, options: PatchApiCrmOpportunitiesOpportunityIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/crm/opportunities/${encodeURIComponent(String(args.opportunity_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(dealResponse, await res.json()) };