import {z} from 'zod' export type Discount = {unit: '%OFF' | 'AMOUNT_OFF' | 'FIXED', value: number, discountId?: string | undefined, autoAdd?: boolean | undefined, ON: {kind: 'SKU' | 'CART' | 'SERVICE' | 'SERVICE_CART', value: string | Array>, quantity?: number | undefined, includeOperator?: ('AND' | 'OR') | undefined, excludeOperator?: ('AND' | 'OR') | undefined}, set?: (Array<'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'>) | undefined}; export type TargetProduct = {kind: 'SKU' | 'CATEGORY' | 'ATTRIBUTE' | 'PRICE_KIND' | 'PREVIOUS_DISCOUNT', value: string | Array, operator: 'IN' | 'NOT_IN' | 'EQUAL' | 'GREATER_THAN', discountId?: string | undefined}; export type Condition = {key: 'SKU' | 'QUANTITY' | 'CATEGORY' | 'ATTRIBUTE' | 'PRICE_KIND' | 'CHANEL' | 'ORDER_VALUE' | 'USER_SEGMENT' | 'USER_KIND', value: string | Array, operator: 'NOT_EQUAL' | 'EQUAL' | 'GREATER_THAN' | 'LESS_THAN' | 'GREATER_THAN_OR_EQUAL_TO' | 'LESS_THAN_OR_EQUAL_TO' | 'NOT_IN' | 'IN'}; export type PromoConditionUpdate = {id?: (string | null) | undefined, operator: 'AND' | 'OR', includeOperator?: ('AND' | 'OR') | undefined, excludeOperator?: ('AND' | 'OR') | undefined, conditions: Array, set?: ('A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z') | undefined}; export type TermsAndConditions = {title?: string | undefined, description?: string | undefined}; export type PromotionMessage = {title?: string | undefined, message?: string | undefined, pages?: Array | undefined, locales?: Array | undefined}; export type ProximityMessage = {threshold?: number | undefined, localizedMessages?: (Array<{locales?: Array | undefined, message?: string | undefined}>) | undefined}; export type UpdatePromo = {title: string, buyOperator?: ('AND' | 'OR') | undefined, promo: Array<{discount: Array, targetProducts: Array, condition: Array}>, startDate: string, endDate: string, isExclusive: boolean, type: 'PRODUCT' | 'CART' | 'QUANTITY' | 'BOGO' | 'SHIPPING' | 'BUYGET' | 'SPEND_GET', stackingType?: ('STACKABLE' | 'EXCLUSIVE' | 'TYPE_EXCLUSIVE' | 'UNIVERSAL') | undefined, level?: number | undefined, termsAndConditions?: Array | undefined, promotionMessages?: Array | undefined, proximityMessages?: Array | undefined, shipmentMethodIds?: Array | undefined, eligiblePriceList?: Array | undefined, limits?: Array<{kind: string, value: number}> | undefined, note?: string | undefined, additionalAttributes?: (Array<{key?: string | undefined, value?: (string | Array) | undefined, attributeId?: number | undefined}>) | undefined}; export type UpdateApiApiOffersPromoUpdatePromoIdArgs = {'x-site-context': string, promoId: string, body: UpdatePromo}; export type UpdateApiApiOffersPromoUpdatePromoIdOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const discount = z.object({unit: z.enum(["%OFF", "AMOUNT_OFF", "FIXED"]), value: z.number(), discountId: z.string().optional(), autoAdd: z.boolean().optional(), ON: z.object({kind: z.enum(["SKU", "CART", "SERVICE", "SERVICE_CART"]), value: z.union([z.string(), z.array(z.object({}))]), quantity: z.number().optional(), includeOperator: z.enum(["AND", "OR"]).optional(), excludeOperator: z.enum(["AND", "OR"]).optional()}), set: z.array(z.enum(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"])).optional()}); export const targetProduct = z.object({kind: z.enum(["SKU", "CATEGORY", "ATTRIBUTE", "PRICE_KIND", "PREVIOUS_DISCOUNT"]), value: z.union([z.string(), z.array(z.union([z.number(), z.string()]))]), operator: z.enum(["IN", "NOT_IN", "EQUAL", "GREATER_THAN"]), discountId: z.string().optional()}); export const termsAndConditions = z.object({title: z.string().optional(), description: z.string().optional()}); export const promotionMessage = z.object({title: z.string().optional(), message: z.string().optional(), pages: z.array(z.string()).optional(), locales: z.array(z.string()).optional()}); export const proximityMessage = z.object({threshold: z.number().int().gte(0).optional(), localizedMessages: z.array(z.object({locales: z.array(z.string()).optional(), message: z.string().optional()})).optional()}); export const promoResponse = z.object({_id: z.string().optional(), promoId: z.number().optional(), promoCodes: z.array(z.string()).optional(), state: z.enum(["ACTIVE", "SCHEDULED", "DISABLED", "EXPIRED"]).optional(), isImplicit: z.boolean().optional(), title: z.string().optional(), buyOperator: z.enum(["AND", "OR"]).optional(), promo: z.array(z.object({discount: z.array(discount).optional(), targetProducts: z.array(targetProduct).optional(), condition: z.array(z.string()).optional()})).optional(), startDate: z.string().optional(), endDate: z.string().optional(), isExclusive: z.boolean().optional(), type: z.enum(["PRODUCT", "CART", "QUANTITY", "BOGO", "SHIPPING", "BUYGET", "SPEND_GET"]).optional(), stackingType: z.enum(["STACKABLE", "EXCLUSIVE", "TYPE_EXCLUSIVE", "UNIVERSAL"]).optional(), level: z.number().int().optional(), termsAndConditions: z.array(termsAndConditions).optional(), promotionMessages: z.array(promotionMessage).optional(), proximityMessages: z.array(proximityMessage).optional(), shipmentMethodIds: z.array(z.string()).optional(), eligiblePriceList: z.array(z.number()).optional(), limits: z.array(z.object({kind: z.string().optional(), value: z.number().optional()})).optional(), note: z.string().optional(), createdAt: z.string().optional(), updatedAt: 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 }; /** * Update a specific promotion * * PUT /api-offers/promo/update/{promoId} */ export const updateApiApiOffersPromoUpdatePromoId = async (args: UpdateApiApiOffersPromoUpdatePromoIdArgs, options?: UpdateApiApiOffersPromoUpdatePromoIdOptions) => { const {baseUrl = "https://live.copilot.fabric.inc", fetch: fetchFn = fetch, AuthorizationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-site-context'] != null) headers.set("x-site-context", String(args['x-site-context'])) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api-offers/promo/update/${encodeURIComponent(String(args.promoId))}${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(promoResponse, await res.json()) };