import {z} from 'zod' export type CreateApiV1OpportunitiesArgs = {center_id: string, opportunity_title: string, opportunity_description?: string | undefined, opportunity_owner_id?: string | undefined, guest_id: string, created_by_id: string, priority_type_id?: string | undefined, status_id?: string | undefined, type?: string | undefined, followup_date: string, expected_close_date?: string | undefined, listed_amount?: number | undefined, offered_amount?: number | undefined, optional_field_1?: string | undefined, optional_field_2?: string | undefined, optional_field_3?: string | undefined, 'ticket_id (guid)'?: string | undefined, change_guest_center?: boolean | undefined, disposition_id?: string | undefined, custom_fields?: string | undefined, lead_source?: Record | undefined, 'lead_source.id'?: string | undefined, 'lead_source.code'?: string | undefined}; export type CreateApiV1OpportunitiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1OpportunitiesResponse = z.object({success: z.boolean().optional(), opportunity_id: z.string().optional(), message: z.string().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 an opportunity * * POST /v1/opportunities */ export const createApiV1Opportunities = async (args: CreateApiV1OpportunitiesArgs, options?: CreateApiV1OpportunitiesOptions) => { const {baseUrl = "https://api.zenoti.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.center_id != null) query.append("center_id", String(args.center_id)) if (args.opportunity_title != null) query.append("opportunity_title", String(args.opportunity_title)) if (args.opportunity_description != null) query.append("opportunity_description", String(args.opportunity_description)) if (args.opportunity_owner_id != null) query.append("opportunity_owner_id", String(args.opportunity_owner_id)) if (args.guest_id != null) query.append("guest_id", String(args.guest_id)) if (args.created_by_id != null) query.append("created_by_id", String(args.created_by_id)) if (args.priority_type_id != null) query.append("priority_type_id", String(args.priority_type_id)) if (args.status_id != null) query.append("status_id", String(args.status_id)) if (args.type != null) query.append("type", String(args.type)) if (args.followup_date != null) query.append("followup_date", String(args.followup_date)) if (args.expected_close_date != null) query.append("expected_close_date", String(args.expected_close_date)) if (args.listed_amount != null) query.append("listed_amount", String(args.listed_amount)) if (args.offered_amount != null) query.append("offered_amount", String(args.offered_amount)) if (args.optional_field_1 != null) query.append("optional_field_1", String(args.optional_field_1)) if (args.optional_field_2 != null) query.append("optional_field_2", String(args.optional_field_2)) if (args.optional_field_3 != null) query.append("optional_field_3", String(args.optional_field_3)) if (args['ticket_id (guid)'] != null) query.append("ticket_id (guid)", String(args['ticket_id (guid)'])) if (args.change_guest_center != null) query.append("change_guest_center", String(args.change_guest_center)) if (args.disposition_id != null) query.append("disposition_id", String(args.disposition_id)) if (args.custom_fields != null) query.append("custom_fields", String(args.custom_fields)) if (args.lead_source != null) query.append("lead_source", JSON.stringify(args.lead_source)) if (args['lead_source.id'] != null) query.append("lead_source.id", String(args['lead_source.id'])) if (args['lead_source.code'] != null) query.append("lead_source.code", String(args['lead_source.code'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/opportunities${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1OpportunitiesResponse, await res.json()) };