import {z} from 'zod' export type CreateApiAppYelpAdsJobsManualArgs = {body: {programRef?: (string | null) | undefined, businessRef?: (string | null) | undefined, action: 'create_program' | 'edit_program' | 'update_features' | 'pause' | 'resume' | 'end_program' | 'edit_budget', programName?: ('CPC' | 'BP' | 'EP' | 'RCA' | 'CTA' | 'SLIDESHOW' | 'BH' | 'VL' | 'LOGO' | 'PORTFOLIO') | undefined, direction?: ('increase' | 'decrease') | undefined, deltaPct?: number | undefined, budgetCents?: number | undefined, maxBidCents?: (number | null) | undefined, pacingMethod?: ('paced' | 'unpaced' | null) | undefined, startDate?: (string | null) | undefined, endDate?: (string | null) | undefined, futureBudgetDate?: (string | null) | undefined, promotionCode?: (string | null) | undefined, currency?: (string | null) | undefined, isAutobid?: (boolean | null) | undefined, feePeriod?: ('CALENDAR_MONTH' | 'ROLLING_MONTH' | null) | undefined, adCategories?: Array | undefined, features?: (Record | null) | undefined}}; export type CreateApiAppYelpAdsJobsManualOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appYelpAdsJobSchema = z.object({id: z.number().int(), recommendationId: z.number().int().nullable(), recommendationTitle: z.string().nullable(), businessId: z.string().nullable(), businessName: z.string().nullable(), programRef: z.string().nullable(), action: z.string(), status: z.string(), requestedDirection: z.string().nullable(), requestedDeltaPct: z.number().int().nullable(), currentBudgetCents: z.number().int().nullable(), targetBudgetCents: z.number().int().nullable(), guardrailsPassed: z.boolean().nullable(), partnerWriteEnabled: z.boolean().nullable(), blockReason: z.string().nullable(), message: z.string().nullable(), submittedAt: z.string().nullable(), checkedAt: z.string().nullable(), completedAt: z.string().nullable(), createdAt: 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 }; /** * Enqueue a manual guarded Yelp ads job (create/pause/resume/edit budget) * * POST /app/yelp/ads-jobs/manual */ export const createApiAppYelpAdsJobsManual = async (args: CreateApiAppYelpAdsJobsManualArgs, options: CreateApiAppYelpAdsJobsManualOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/app/yelp/ads-jobs/manual${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(appYelpAdsJobSchema, await res.json()) };