import {z} from 'zod' export type TriggerFrequency = 'daily'; export type ConditionMetric = 'creative_roi' | 'creative_revenue' | 'campaign_roi' | 'campaign_revenue' | 'campaign_cost' | 'click_through_rate' | 'organic_engagement_rate' | 'product_ad_clicks' | '2s_video_view_rate' | 'impressions' | 'orders' | 'creative_orders' | 'boost_roas' | 'boost_revenue' | 'creative_ctr' | 'product_ad_click_rate' | 'creative_cost' | 'cost' | 'campaign_engagement_rate' | 'total_creative_boost_cost' | 'roi' | 'spend' | 'ctr' | 'spend_pacing'; export type ConditionOperator = 'lt' | 'gte' | 'gt' | 'lte' | '>=' | '<=' | '>' | '<' | '=='; export type ConditionRulePublic = {metric: ConditionMetric, operator: ConditionOperator, value: number, lookback_days?: number | undefined}; export type ActionType = 'set_up_creative_boost'; export type ActionRulePublic = {action_type: ActionType, params?: {[key: string]: unknown} | undefined}; export type GuardrailRuleType = 'total_creative_boost_cost' | 'creative_roi' | 'creative_revenue' | 'campaign_roi' | 'campaign_revenue' | 'campaign_cost' | 'impressions' | 'orders' | 'boost_roas' | 'boost_revenue' | 'click_through_rate' | 'organic_engagement_rate' | 'product_ad_clicks' | '2s_video_view_rate' | 'creative_ctr' | 'product_ad_click_rate' | 'max_budget_daily' | 'cooldown_hours'; export type GuardrailRulePublic = {rule_type: GuardrailRuleType, operator?: (ConditionOperator | null) | undefined, value: number}; export type AutomationUpdateRequestPublic = {name?: (string | null) | undefined, trigger_frequency?: (TriggerFrequency | null) | undefined, campaign_ids?: (Array | null) | undefined, conditions?: (Array | null) | undefined, actions?: (Array | null) | undefined, guardrails?: (Array | null) | undefined, boost_start_date?: (string | null) | undefined, boost_end_date?: (string | null) | undefined}; export type UpdateApiGmvMaxAutomationsAutomationIdArgs = {automation_id: number, 'Idempotency-Key'?: (string | null) | undefined, 'X-Dry-Run'?: (string | null) | undefined, body: AutomationUpdateRequestPublic}; export type UpdateApiGmvMaxAutomationsAutomationIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, ShopId?: string | undefined, fetch?: typeof fetch | undefined}; export const automationResponse = z.object({id: z.number().int(), shop_id: z.number().int(), name: z.string(), template_type: z.string().nullable().optional(), status: z.string(), trigger_frequency: z.string(), campaign_ids: z.array(z.unknown()).optional(), conditions: z.array(z.unknown()).optional(), actions: z.array(z.unknown()).optional(), guardrails: z.array(z.unknown()).optional(), last_run_at: z.string().nullable().optional(), next_run_at: z.string().nullable().optional(), boost_start_date: z.string().nullable().optional(), boost_end_date: z.string().nullable().optional(), is_editable: z.boolean().optional(), created_by: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: 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 }; /** * Update a GMV Max automation * * PUT /gmv-max/automations/{automation_id} */ export const updateApiGmvMaxAutomationsAutomationId = async (args: UpdateApiGmvMaxAutomationsAutomationIdArgs, options?: UpdateApiGmvMaxAutomationsAutomationIdOptions) => { const {baseUrl = "https://api.reacherapp.com/public/v1", fetch: fetchFn = fetch, ApiKeyAuth, ShopId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (args['X-Dry-Run'] != null) headers.set("X-Dry-Run", String(args['X-Dry-Run'])) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) if (ShopId !== undefined) headers.set("x-shop-id", ShopId) const url = `${baseUrl.replace(/\/+$/, '')}/gmv-max/automations/${encodeURIComponent(String(args.automation_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(automationResponse, await res.json()) };