import {z} from 'zod' export type LoyaltyV2ActivityBenefitPoints = {value: number, card_definition_id: string}; export type LoyaltyV2ActivityBenefitPointsProportionalEvery = {every: number, value: number}; export type LoyaltyV2ActivityBenefitPointsProportionalMetadata = {every: number, value: number, property: string}; export type BenefitPointsProportionalOrder = {amount?: LoyaltyV2ActivityBenefitPointsProportionalEvery | undefined, total_amount?: LoyaltyV2ActivityBenefitPointsProportionalEvery | undefined, metadata?: LoyaltyV2ActivityBenefitPointsProportionalMetadata | undefined}; export type BenefitPointsProportionalCustomer = {metadata: LoyaltyV2ActivityBenefitPointsProportionalMetadata}; export type BenefitPointsProportionalCustomEvent = {metadata: LoyaltyV2ActivityBenefitPointsProportionalMetadata}; export type LoyaltyV2ActivityBenefitPointsProportionalApplicableToProduct = {id: string}; export type LoyaltyV2ActivityBenefitPointsProportionalApplicableToSku = {id: string}; export type LoyaltyV2ActivityBenefitPointsProportionalApplicableToProductsCollection = {id: string}; export type BenefitPointsProportionalApplicableTo = {type: 'product' | 'sku' | 'products_collection', product?: (LoyaltyV2ActivityBenefitPointsProportionalApplicableToProduct | null) | undefined, sku?: (LoyaltyV2ActivityBenefitPointsProportionalApplicableToSku | null) | undefined, products_collection?: (LoyaltyV2ActivityBenefitPointsProportionalApplicableToProductsCollection | null) | 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 | null) | undefined, customer?: (BenefitPointsProportionalCustomer | null) | undefined, custom_event?: (BenefitPointsProportionalCustomEvent | null) | undefined, order_items?: (BenefitPointsProportionalOrderItems | null) | undefined}; export type LoyaltyV2ActivityBenefitMaterialProduct = {id: string}; export type LoyaltyV2ActivityBenefitMaterialSKU = {product_id: string, id: string}; export type BenefitMaterial = {type: 'PRODUCT' | 'SKU', product?: (LoyaltyV2ActivityBenefitMaterialProduct | null) | undefined, sku?: (LoyaltyV2ActivityBenefitMaterialSKU | null) | undefined}; export type LoyaltyV2ActivityBenefitDigitalGiftVouchers = {campaign_id: string, balance: number}; export type LoyaltyV2ActivityBenefitDigitalDiscountCoupons = {campaign_id: string}; export type BenefitDigital = {type: 'GIFT_VOUCHERS' | 'DISCOUNT_COUPONS', gift_vouchers?: (LoyaltyV2ActivityBenefitDigitalGiftVouchers | null) | undefined, discount_coupons?: (LoyaltyV2ActivityBenefitDigitalDiscountCoupons | null) | undefined}; export type LoyaltyV2ActivityBenefitStockLimited = {quantity: number}; export type BenefitStock = {type: 'UNLIMITED' | 'LIMITED', limited?: (LoyaltyV2ActivityBenefitStockLimited | null) | undefined}; export type V2LoyaltiesBenefitsCreateRequestBody = {name: string, type: 'POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL', status?: ('DRAFT' | 'ACTIVE') | undefined, points?: (LoyaltyV2ActivityBenefitPoints | null) | undefined, points_proportional?: (BenefitPointsProportional | null) | undefined, material?: (BenefitMaterial | null) | undefined, digital?: (BenefitDigital | null) | undefined, stock?: (BenefitStock | null) | undefined}; export type CreateApiV2LoyaltiesBenefitsArgs = {body: V2LoyaltiesBenefitsCreateRequestBody}; export type CreateApiV2LoyaltiesBenefitsOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const loyaltyV2ActivityBenefitStockLimited = z.object({quantity: z.number().int().gte(0).lte(9007199254740991)}); export const benefitStock = z.object({type: z.enum(["UNLIMITED", "LIMITED"]), limited: loyaltyV2ActivityBenefitStockLimited.nullable().optional()}); export const loyaltyV2ActivityBenefitPoints = z.object({value: z.number().int().gte(0).lte(9007199254740991), card_definition_id: z.string()}); export const loyaltyV2ActivityBenefitPointsProportionalEvery = z.object({every: z.number().int().gte(1).lte(9007199254740991), value: z.number().gte(0).lte(9007199254740991)}); export const loyaltyV2ActivityBenefitPointsProportionalMetadata = 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: loyaltyV2ActivityBenefitPointsProportionalEvery.optional(), total_amount: loyaltyV2ActivityBenefitPointsProportionalEvery.optional(), metadata: loyaltyV2ActivityBenefitPointsProportionalMetadata.optional()}); export const benefitPointsProportionalCustomer = z.object({metadata: loyaltyV2ActivityBenefitPointsProportionalMetadata}); export const benefitPointsProportionalCustomEvent = z.object({metadata: loyaltyV2ActivityBenefitPointsProportionalMetadata}); export const loyaltyV2ActivityBenefitPointsProportionalApplicableToProduct = z.object({id: z.string()}); export const loyaltyV2ActivityBenefitPointsProportionalApplicableToSku = z.object({id: z.string()}); export const loyaltyV2ActivityBenefitPointsProportionalApplicableToProductsCollection = z.object({id: z.string()}); export const benefitPointsProportionalApplicableTo = z.object({type: z.enum(["product", "sku", "products_collection"]), product: loyaltyV2ActivityBenefitPointsProportionalApplicableToProduct.nullable().optional(), sku: loyaltyV2ActivityBenefitPointsProportionalApplicableToSku.nullable().optional(), products_collection: loyaltyV2ActivityBenefitPointsProportionalApplicableToProductsCollection.nullable().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.nullable().optional(), customer: benefitPointsProportionalCustomer.nullable().optional(), custom_event: benefitPointsProportionalCustomEvent.nullable().optional(), order_items: benefitPointsProportionalOrderItems.nullable().optional()}); export const loyaltyV2ActivityBenefitMaterialProduct = z.object({id: z.string()}); export const loyaltyV2ActivityBenefitMaterialSKU = z.object({product_id: z.string(), id: z.string()}); export const benefitMaterial = z.object({type: z.enum(["PRODUCT", "SKU"]), product: loyaltyV2ActivityBenefitMaterialProduct.nullable().optional(), sku: loyaltyV2ActivityBenefitMaterialSKU.nullable().optional()}); export const loyaltyV2ActivityBenefitDigitalGiftVouchers = z.object({campaign_id: z.string(), balance: z.number().gte(0).lte(9007199254740991)}); export const loyaltyV2ActivityBenefitDigitalDiscountCoupons = z.object({campaign_id: z.string()}); export const benefitDigital = z.object({type: z.enum(["GIFT_VOUCHERS", "DISCOUNT_COUPONS"]), gift_vouchers: loyaltyV2ActivityBenefitDigitalGiftVouchers.nullable().optional(), discount_coupons: loyaltyV2ActivityBenefitDigitalDiscountCoupons.nullable().optional()}); export const v2LoyaltiesBenefitsCreateResponseBody = z.object({id: z.string(), name: z.string(), type: z.enum(["POINTS", "POINTS_PROPORTIONAL", "MATERIAL", "DIGITAL"]), status: z.enum(["DRAFT", "ACTIVE", "DELETED"]), stock: benefitStock, points: loyaltyV2ActivityBenefitPoints.optional(), points_proportional: benefitPointsProportional.optional(), material: benefitMaterial.optional(), digital: benefitDigital.optional(), created_at: z.string(), updated_at: z.string().nullable(), object: z.literal("benefit")}); 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 }; /** * Create benefit * * POST /v2/loyalties/benefits * * Also accepts auth: bearerAuth */ export const createApiV2LoyaltiesBenefits = async (args: CreateApiV2LoyaltiesBenefitsArgs, options?: CreateApiV2LoyaltiesBenefitsOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/loyalties/benefits${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(v2LoyaltiesBenefitsCreateResponseBody, await res.json()) };