import {z} from 'zod' export type DiscountStatus = 'OTHER' | 'ACTIVE' | 'PAUSED' | 'SCHEDULED' | 'EXPIRED'; export type PromotionType = 'VARIABLE' | 'SITEWIDE' | 'CHECKOUT' | 'SAVE_X_ON_Y' | 'BUY_X_GET_Y' | 'SPEND_X_SAVE_Y' | 'FREE_SHIPPING' | 'FREE_SHIPPING_MINIMUM' | 'FREE_SHIPPING_WITH_DISCOUNT' | 'SITEWIDE_IN_STORES' | 'EXTRA_PERCENT_OFF' | 'GIFT_WITH_PURCHASE' | 'GIFT_WITH_PURCHASE_MINIMUM' | 'FIXED' | 'PERCENT_OFF_CLEARANCE' | 'X_OFF_Y' | 'GIFT_WITH_FIRST_PURCHASE' | 'BUY_X_GET_ONE_FREE' | 'CASH_BACK' | 'POINTS_ON_ALL_PURCHASES' | 'BONUS' | 'POINTS_WITH_PURCHASE' | 'CUSTOM'; export type Currency = 'UNK' | 'USD' | 'GBP' | 'CAD' | 'EUR' | 'AUD' | 'NZD' | 'SEK' | 'ILS' | 'CHF' | 'HKD' | 'JPY' | 'SGD' | 'KRW' | 'NOK' | 'DKK' | 'PLN' | 'RON' | 'HUF' | 'CZK' | 'BRL' | 'MXN' | 'ARS' | 'CLP' | 'COP' | 'INR' | 'TRY'; export type PromotionTemplateValue = {amount?: number | undefined, currency_code?: Currency | undefined, custom_text?: string | undefined, percent?: number | undefined}; export type PromotionBatchUpdate = {discount_status?: DiscountStatus | undefined, end_time?: number | undefined, external_id?: string | undefined, id: string, platform_type?: string | undefined, promotion_code?: string | undefined, promotion_custom_id?: string | undefined, promotion_title?: string | undefined, promotion_type?: PromotionType | undefined, start_time?: number | undefined, template_values?: Array | undefined}; export type PromotionUpdateBatch = Array; export type PatchApiAdAccountsAdAccountIdPromotionsArgs = {ad_account_id: string, body: PromotionUpdateBatch}; export type PatchApiAdAccountsAdAccountIdPromotionsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const discountStatus = z.enum(["OTHER", "ACTIVE", "PAUSED", "SCHEDULED", "EXPIRED"]); export const promotionType = z.enum(["VARIABLE", "SITEWIDE", "CHECKOUT", "SAVE_X_ON_Y", "BUY_X_GET_Y", "SPEND_X_SAVE_Y", "FREE_SHIPPING", "FREE_SHIPPING_MINIMUM", "FREE_SHIPPING_WITH_DISCOUNT", "SITEWIDE_IN_STORES", "EXTRA_PERCENT_OFF", "GIFT_WITH_PURCHASE", "GIFT_WITH_PURCHASE_MINIMUM", "FIXED", "PERCENT_OFF_CLEARANCE", "X_OFF_Y", "GIFT_WITH_FIRST_PURCHASE", "BUY_X_GET_ONE_FREE", "CASH_BACK", "POINTS_ON_ALL_PURCHASES", "BONUS", "POINTS_WITH_PURCHASE", "CUSTOM"]); export const entityStatus = z.enum(["ACTIVE", "PAUSED", "ARCHIVED", "DRAFT", "DELETED_DRAFT"]); export const currency = z.enum(["UNK", "USD", "GBP", "CAD", "EUR", "AUD", "NZD", "SEK", "ILS", "CHF", "HKD", "JPY", "SGD", "KRW", "NOK", "DKK", "PLN", "RON", "HUF", "CZK", "BRL", "MXN", "ARS", "CLP", "COP", "INR", "TRY"]); export const promotionTemplateValue = z.object({amount: z.number().optional(), currency_code: currency.optional(), custom_text: z.string().optional(), percent: z.number().optional()}); export const promotion = z.object({ad_account_id: z.string(), discount_status: discountStatus.optional(), end_time: z.number().int().optional(), external_id: z.string().max(64).optional(), id: z.string().max(18), platform_type: z.string().optional(), promotion_code: z.string().optional(), promotion_custom_id: z.string().max(50).optional(), promotion_title: z.string(), promotion_type: promotionType, start_time: z.number().int().optional(), status: entityStatus.optional(), template_values: z.array(promotionTemplateValue).optional()}); export const exception = z.object({code: z.number().int().optional(), message: z.string()}); export const promotionArrayElement = z.object({data: promotion.optional(), exception: exception.optional()}); export const promotionsResponse = z.object({promotions: z.array(promotionArrayElement).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 promotions * * PATCH /ad_accounts/{ad_account_id}/promotions */ export const patchApiAdAccountsAdAccountIdPromotions = async (args: PatchApiAdAccountsAdAccountIdPromotionsArgs, options?: PatchApiAdAccountsAdAccountIdPromotionsOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/promotions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(promotionsResponse, await res.json()) };