import {z} from 'zod' export type PromotionValidityUpdate = {starts_at: string, ends_at?: (string | null) | undefined, recurring_weekdays: Array<'MONDAY' | 'TUESDAY' | 'WEDNESDAY' | 'THURSDAY' | 'FRIDAY' | 'SATURDAY' | 'SUNDAY'>}; export type PromotionEligibilityUpdate = {scope: 'ALL' | 'ONLY' | 'EXCEPT', cluster_ids: Array, strategy_ids: Array, apply_to_all_channels: boolean, channel_ids: Array}; export type PromotionBenefitUpdate = {type: 'DISCOUNT' | 'SHIPPING' | 'GIFT', kind?: string | undefined, value?: number | undefined, apply_by?: ('CART' | 'ITEM') | undefined, quantity?: number | undefined, product_ids?: Array | undefined}; export type PromotionItemRestrictionUpdate = {dimension: 'PRODUCT' | 'CATEGORY', operator: 'IN' | 'NOT_IN', values: Array}; export type PromotionConditionUpdate = {type: 'ORDER_VALUE' | 'SHIPPING_TYPE' | 'PAYMENT_METHOD' | 'INSTALLMENTS', min?: number | undefined, max?: number | undefined, values?: Array | undefined}; export type PromotionEligibleItemsLimitUpdate = {limit: number, count_by: 'ORDER' | 'PRODUCT'}; export type PromotionUsageLimitsUpdate = {per_customer?: (number | null) | undefined, total?: (number | null) | undefined, eligible_items?: (PromotionEligibleItemsLimitUpdate | null) | undefined}; export type PromotionAccumulationUpdate = {with_discount_promotions: boolean, with_manual_prices: boolean}; export type PromotionUpdate = {name?: string | undefined, description?: (string | null) | undefined, active?: boolean | undefined, validity?: PromotionValidityUpdate | undefined, eligibility?: PromotionEligibilityUpdate | undefined, benefit?: PromotionBenefitUpdate | undefined, item_restrictions?: Array | undefined, conditions?: Array | undefined, usage_limits?: PromotionUsageLimitsUpdate | undefined, accumulation?: PromotionAccumulationUpdate | undefined, metadata?: (Record | null) | undefined}; export type PatchApiApiV1PromotionsIdArgs = {id: string, body: PromotionUpdate}; export type PatchApiApiV1PromotionsIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const promotionUpdateResponse = z.object({metadata: z.record(z.string(), z.unknown()).nullable(), id: z.string(), code: z.string(), name: z.string(), description: z.string().nullable(), active: z.boolean(), validity: z.object({}), eligibility: z.object({}), benefit: z.object({}), item_restrictions: z.array(z.object({})), conditions: z.array(z.object({})), usage_limits: z.object({}), accumulation: z.object({}), created_at: z.string(), updated_at: z.string()}); 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 promotion * * PATCH /api/v1/promotions/{id} * * Also accepts auth: bearer */ export const patchApiApiV1PromotionsId = async (args: PatchApiApiV1PromotionsIdArgs, options?: PatchApiApiV1PromotionsIdOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/promotions/${encodeURIComponent(String(args.id))}${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(promotionUpdateResponse, await res.json()) };