import {z} from 'zod' export type GetApiAppYelpReportingArgs = {businessId?: string | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiAppYelpReportingOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appYelpReportingKpiSchema = z.object({totalSpendCents: z.number().int().optional(), totalLeads: z.number().int().optional(), costPerLeadCents: z.number().int().optional(), ctr: z.number().optional(), avgCpcCents: z.number().int().optional(), billableClicks: z.number().int().optional(), billableImpressions: z.number().int().optional(), calls: z.number().int().optional(), raqMessages: z.number().int().optional(), websiteClicks: z.number().int().optional(), pageViews: z.number().int().optional()}); export const appYelpReportingLeadBreakdownSliceSchema = z.object({label: z.string().optional(), value: z.number().int().optional()}); export const appYelpReportingDailyPointSchema = z.object({date: z.string().optional(), spendCents: z.number().int().optional(), leads: z.number().int().optional(), billableClicks: z.number().int().optional(), billableImpressions: z.number().int().optional(), calls: z.number().int().optional(), raqMessages: z.number().int().optional(), websiteClicks: z.number().int().optional(), pageViews: z.number().int().optional()}); export const appYelpReportingBusinessMetricSchema = z.object({businessRef: z.string().optional(), businessName: z.string().optional(), rating: z.number().nullable().optional(), reviewCount: z.number().int().nullable().optional(), pageViews: z.number().int().optional(), websiteClicks: z.number().int().optional(), calls: z.number().int().optional(), raqMessages: z.number().int().optional(), billableImpressions: z.number().int().optional(), billableClicks: z.number().int().optional(), spendCents: z.number().int().optional()}); export const appYelpReportingLeadLocationSchema = z.object({postalCode: z.string().optional(), leads: z.number().int().optional()}); export const appYelpReportingLeadLocationsSchema = z.object({total: z.number().int(), unknown: z.number().int(), items: z.array(appYelpReportingLeadLocationSchema)}); export const appYelpReportingSchema = z.object({kpis: appYelpReportingKpiSchema, leadBreakdown: z.array(appYelpReportingLeadBreakdownSliceSchema), daily: z.array(appYelpReportingDailyPointSchema), businesses: z.array(appYelpReportingBusinessMetricSchema), leadLocations: appYelpReportingLeadLocationsSchema}); 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 }; /** * View aggregated synced Yelp reporting metrics * * GET /app/yelp/reporting */ export const getApiAppYelpReporting = async (args: GetApiAppYelpReportingArgs, options: GetApiAppYelpReportingOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.businessId != null) query.append("businessId", String(args.businessId)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) const url = `${baseUrl.replace(/\/+$/, '')}/app/yelp/reporting${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(appYelpReportingSchema, await res.json()) };