import {z} from 'zod' export type BenefitPoints = {value: number, card_definition_id: string}; export type BenefitPointsProportionalEvery = {every: number, value: number}; export type BenefitPointsProportionalMetadata = {every: number, value: number, property: string}; export type BenefitPointsProportionalOrder = {amount?: BenefitPointsProportionalEvery | undefined, total_amount?: BenefitPointsProportionalEvery | undefined, metadata?: BenefitPointsProportionalMetadata | undefined}; export type BenefitPointsProportionalCustomer = {metadata: BenefitPointsProportionalMetadata}; export type BenefitPointsProportionalCustomEvent = {metadata: BenefitPointsProportionalMetadata}; export type BenefitPointsProportionalApplicableToProduct = {id: string}; export type BenefitPointsProportionalApplicableToSku = {id: string}; export type BenefitPointsProportionalApplicableToProductsCollection = {id: string}; export type BenefitPointsProportionalApplicableTo = {type: 'product' | 'sku' | 'products_collection', product?: BenefitPointsProportionalApplicableToProduct | undefined, sku?: BenefitPointsProportionalApplicableToSku | undefined, products_collection?: BenefitPointsProportionalApplicableToProductsCollection | undefined}; export type BenefitPointsProportionalOrderItemsCalculation = {every: number, value: number, applicable_to: Array}; export type BenefitPointsProportionalOrderItems = {amount?: BenefitPointsProportionalOrderItemsCalculation | undefined, subtotal_amount?: BenefitPointsProportionalOrderItemsCalculation | undefined, quantity?: BenefitPointsProportionalOrderItemsCalculation | undefined}; export type BenefitPointsProportional = {calculation_type: 'PRE_DISCOUNT_ORDER_AMOUNT' | 'POST_DISCOUNT_ORDER_AMOUNT' | 'PRE_DISCOUNT_ORDER_ITEMS_AMOUNT' | 'POST_DISCOUNT_ORDER_ITEMS_AMOUNT' | 'ORDER_ITEMS_QUANTITY' | 'ORDER_METADATA_VALUE' | 'CUSTOMER_METADATA_VALUE' | 'CUSTOM_EVENT_METADATA_VALUE', card_definition_id: string, order?: BenefitPointsProportionalOrder | undefined, customer?: BenefitPointsProportionalCustomer | undefined, custom_event?: BenefitPointsProportionalCustomEvent | undefined, order_items?: BenefitPointsProportionalOrderItems | undefined}; export type BenefitMaterialProduct = {id: string}; export type BenefitMaterialSKU = {product_id: string, id: string}; export type BenefitMaterial = {type: 'PRODUCT' | 'SKU', product?: BenefitMaterialProduct | undefined, sku?: BenefitMaterialSKU | undefined}; export type BenefitDigitalGiftVouchers = {campaign_id: string, balance: number}; export type BenefitDigitalDiscountCoupons = {campaign_id: string}; export type BenefitDigital = {type: 'GIFT_VOUCHERS' | 'DISCOUNT_COUPONS', gift_vouchers?: BenefitDigitalGiftVouchers | undefined, discount_coupons?: BenefitDigitalDiscountCoupons | undefined}; export type BenefitStockLimited = {quantity: number}; export type BenefitStock = {type: 'UNLIMITED' | 'LIMITED', limited?: BenefitStockLimited | undefined}; export type BenefitUpdateRequest = {name?: string | undefined, type?: ('POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL') | undefined, points?: BenefitPoints | undefined, points_proportional?: BenefitPointsProportional | undefined, material?: BenefitMaterial | undefined, digital?: BenefitDigital | undefined, stock?: BenefitStock | undefined}; export type UpdateApiV2LoyaltiesBenefitsBenefitIdArgs = {id: string, body: BenefitUpdateRequest}; export type UpdateApiV2LoyaltiesBenefitsBenefitIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const benefitStockLimited = z.object({quantity: z.number().int().gte(0).lte(9007199254740991)}); export const benefitStock = z.object({type: z.enum(["UNLIMITED", "LIMITED"]), limited: benefitStockLimited.optional()}); export const benefitPoints = z.object({value: z.number().int().gte(0).lte(9007199254740991), card_definition_id: z.string()}); export const benefitPointsProportionalEvery = z.object({every: z.number().int().gte(1).lte(9007199254740991), value: z.number().gte(0).lte(9007199254740991)}); export const benefitPointsProportionalMetadata = z.object({every: z.number().int().gte(1).lte(9007199254740991), value: z.number().gte(0).lte(9007199254740991), property: z.string().min(1)}); export const benefitPointsProportionalOrder = z.object({amount: benefitPointsProportionalEvery.optional(), total_amount: benefitPointsProportionalEvery.optional(), metadata: benefitPointsProportionalMetadata.optional()}); export const benefitPointsProportionalCustomer = z.object({metadata: benefitPointsProportionalMetadata}); export const benefitPointsProportionalCustomEvent = z.object({metadata: benefitPointsProportionalMetadata}); export const benefitPointsProportionalApplicableToProduct = z.object({id: z.string()}); export const benefitPointsProportionalApplicableToSku = z.object({id: z.string()}); export const benefitPointsProportionalApplicableToProductsCollection = z.object({id: z.string()}); export const benefitPointsProportionalApplicableTo = z.object({type: z.enum(["product", "sku", "products_collection"]), product: benefitPointsProportionalApplicableToProduct.optional(), sku: benefitPointsProportionalApplicableToSku.optional(), products_collection: benefitPointsProportionalApplicableToProductsCollection.optional()}); export const benefitPointsProportionalOrderItemsCalculation = z.object({every: z.number().int().gte(1).lte(9007199254740991), value: z.number().gte(0).lte(9007199254740991), applicable_to: z.array(benefitPointsProportionalApplicableTo)}); export const benefitPointsProportionalOrderItems = z.object({amount: benefitPointsProportionalOrderItemsCalculation.optional(), subtotal_amount: benefitPointsProportionalOrderItemsCalculation.optional(), quantity: benefitPointsProportionalOrderItemsCalculation.optional()}); export const benefitPointsProportional = z.object({calculation_type: z.enum(["PRE_DISCOUNT_ORDER_AMOUNT", "POST_DISCOUNT_ORDER_AMOUNT", "PRE_DISCOUNT_ORDER_ITEMS_AMOUNT", "POST_DISCOUNT_ORDER_ITEMS_AMOUNT", "ORDER_ITEMS_QUANTITY", "ORDER_METADATA_VALUE", "CUSTOMER_METADATA_VALUE", "CUSTOM_EVENT_METADATA_VALUE"]), card_definition_id: z.string(), order: benefitPointsProportionalOrder.optional(), customer: benefitPointsProportionalCustomer.optional(), custom_event: benefitPointsProportionalCustomEvent.optional(), order_items: benefitPointsProportionalOrderItems.optional()}); export const benefitMaterialProduct = z.object({id: z.string()}); export const benefitMaterialSKU = z.object({product_id: z.string(), id: z.string()}); export const benefitMaterial = z.object({type: z.enum(["PRODUCT", "SKU"]), product: benefitMaterialProduct.optional(), sku: benefitMaterialSKU.optional()}); export const benefitDigitalGiftVouchers = z.object({campaign_id: z.string(), balance: z.number().gte(0).lte(9007199254740991)}); export const benefitDigitalDiscountCoupons = z.object({campaign_id: z.string()}); export const benefitDigital = z.object({type: z.enum(["GIFT_VOUCHERS", "DISCOUNT_COUPONS"]), gift_vouchers: benefitDigitalGiftVouchers.optional(), discount_coupons: benefitDigitalDiscountCoupons.optional()}); export const benefit = z.object({id: z.string(), name: z.string(), type: z.enum(["POINTS", "POINTS_PROPORTIONAL", "MATERIAL", "DIGITAL"]), status: z.enum(["DRAFT", "ACTIVE", "DELETED"]), stock: benefitStock.optional(), points: benefitPoints.optional(), points_proportional: benefitPointsProportional.optional(), material: benefitMaterial.optional(), digital: benefitDigital.optional(), created_at: z.string(), updated_at: z.string().optional(), object: 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 benefit * * PUT /v2/loyalties/benefits/{benefitId} */ export const updateApiV2LoyaltiesBenefitsBenefitId = async (args: UpdateApiV2LoyaltiesBenefitsBenefitIdArgs, options?: UpdateApiV2LoyaltiesBenefitsBenefitIdOptions) => { const {baseUrl = "https://api.voucherify.io", 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(/\/+$/, '')}/v2/loyalties/benefits/${encodeURIComponent(String(args.benefitId))}${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(benefit, await res.json()) };