import {z} from 'zod' export type GetApiV1DetailedLeadReportsSearchArgs = {'endDate.day'?: number | undefined, 'endDate.month'?: number | undefined, 'endDate.year'?: number | undefined, pageSize?: number | undefined, pageToken?: string | undefined, query?: string | undefined, 'startDate.day'?: number | undefined, 'startDate.month'?: number | undefined, 'startDate.year'?: number | undefined}; export type GetApiV1DetailedLeadReportsSearchOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const googleAdsHomeservicesLocalservicesV1AggregatorInfo = z.object({aggregatorProviderId: z.string().optional()}); export const googleAdsHomeservicesLocalservicesV1BookingLead = z.object({bookingAppointmentTimestamp: z.string().optional(), consumerEmail: z.string().optional(), consumerPhoneNumber: z.string().optional(), customerName: z.string().optional(), jobType: z.string().optional()}); export const googleAdsHomeservicesLocalservicesV1MessageLead = z.object({consumerPhoneNumber: z.string().optional(), customerName: z.string().optional(), jobType: z.string().optional(), postalCode: z.string().optional()}); export const googleAdsHomeservicesLocalservicesV1PhoneLead = z.object({chargedCallTimestamp: z.string().optional(), chargedConnectedCallDurationSeconds: z.string().optional(), consumerPhoneNumber: z.string().optional()}); export const googleTypeTimeZone = z.object({id: z.string().optional(), version: z.string().optional()}); export const googleAdsHomeservicesLocalservicesV1DetailedLeadReport = z.object({accountId: z.string().optional(), aggregatorInfo: googleAdsHomeservicesLocalservicesV1AggregatorInfo.optional(), bookingLead: googleAdsHomeservicesLocalservicesV1BookingLead.optional(), businessName: z.string().optional(), chargeStatus: z.enum(["CHARGE_STATUS_UNSPECIFIED", "CHARGED", "NOT_CHARGED"]).optional(), currencyCode: z.string().optional(), disputeStatus: z.string().optional(), geo: z.string().optional(), googleAdsLeadId: z.string().optional(), leadCategory: z.string().optional(), leadCreationTimestamp: z.string().optional(), leadId: z.string().optional(), leadPrice: z.number().optional(), leadType: z.enum(["LEAD_TYPE_UNSPECIFIED", "MESSAGE", "PHONE_CALL", "BOOKING"]).optional(), messageLead: googleAdsHomeservicesLocalservicesV1MessageLead.optional(), phoneLead: googleAdsHomeservicesLocalservicesV1PhoneLead.optional(), timezone: googleTypeTimeZone.optional()}); export const googleAdsHomeservicesLocalservicesV1SearchDetailedLeadReportsResponse = z.object({detailedLeadReports: z.array(googleAdsHomeservicesLocalservicesV1DetailedLeadReport).optional(), nextPageToken: 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 }; /** * Get detailed lead reports containing leads that have been received by all linked GLS accounts. Caller needs to provide their manager customer id and the associated auth credential that allows them read permissions on their linked accounts. * * GET /v1/detailedLeadReports:search */ export const getApiV1DetailedLeadReportsSearch = async (args: GetApiV1DetailedLeadReportsSearchArgs, options?: GetApiV1DetailedLeadReportsSearchOptions) => { const {baseUrl = "https://localservices.googleapis.com", fetch: fetchFn = fetch, Oauth2, Oauth2c} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['endDate.day'] != null) query.append("endDate.day", String(args['endDate.day'])) if (args['endDate.month'] != null) query.append("endDate.month", String(args['endDate.month'])) if (args['endDate.year'] != null) query.append("endDate.year", String(args['endDate.year'])) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.query != null) query.append("query", String(args.query)) if (args['startDate.day'] != null) query.append("startDate.day", String(args['startDate.day'])) if (args['startDate.month'] != null) query.append("startDate.month", String(args['startDate.month'])) if (args['startDate.year'] != null) query.append("startDate.year", String(args['startDate.year'])) if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) if (Oauth2c !== undefined) headers.set('authorization', `Bearer ${Oauth2c}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/detailedLeadReports:search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(googleAdsHomeservicesLocalservicesV1SearchDetailedLeadReportsResponse, await res.json()) };