import {z} from 'zod' export type ParameterLimit = number; export type ParameterOrderListRedeemables = 'id' | '-id'; export type ParameterString = string; export type FilterConditionsString = {$in?: Array | undefined, $not_in?: Array | undefined, $is?: string | undefined, $is_not?: string | undefined, $has_value?: string | undefined, $is_unknown?: string | undefined, $starts_with?: string | undefined, $ends_with?: string | undefined, $contains?: string | undefined, $not_contain?: string | undefined}; export type FilterConditionsDateTime = {$after?: string | undefined, $before?: string | undefined, $has_value?: string | undefined, $is_unknown?: string | undefined, $more_than?: number | undefined, $less_than?: number | undefined}; export type Junction = 'and' | 'or'; export type ParameterFiltersListCustomerRedeemables = {id?: ({conditions?: FilterConditionsString | undefined}) | undefined, created_at?: ({conditions?: FilterConditionsDateTime | undefined}) | undefined, redeemable_id?: ({conditions?: FilterConditionsString | undefined}) | undefined, redeemable_object?: ({conditions?: ({$in?: Array<'voucher'> | undefined, $not_in?: Array<'voucher'> | undefined, $is?: 'voucher' | undefined, $is_not?: 'voucher' | undefined, $has_value?: string | undefined, $is_unknown?: string | undefined, $starts_with?: string | undefined, $ends_with?: string | undefined}) | undefined}) | undefined, holder_role?: ({conditions?: ({$in?: (Array<'OWNER' | 'REFEREE' | 'REFERRER'>) | undefined, $not_in?: (Array<'OWNER' | 'REFEREE' | 'REFERRER'>) | undefined, $is?: ('OWNER' | 'REFEREE' | 'REFERRER') | undefined, $is_not?: ('OWNER' | 'REFEREE' | 'REFERRER') | undefined, $has_value?: string | undefined, $is_unknown?: string | undefined, $starts_with?: string | undefined, $ends_with?: string | undefined}) | undefined}) | undefined, campaign_id?: ({conditions?: FilterConditionsString | undefined}) | undefined, campaign_type?: ({conditions?: ({$in?: (Array<'LOYALTY_PROGRAM' | 'GIFT_VOUCHERS' | 'DISCOUNT_COUPONS' | 'REFERRAL_PROGRAM'>) | undefined, $not_in?: (Array<'LOYALTY_PROGRAM' | 'GIFT_VOUCHERS' | 'DISCOUNT_COUPONS' | 'REFERRAL_PROGRAM'>) | undefined, $is?: ('LOYALTY_PROGRAM' | 'GIFT_VOUCHERS' | 'DISCOUNT_COUPONS' | 'REFERRAL_PROGRAM') | undefined, $is_not?: ('LOYALTY_PROGRAM' | 'GIFT_VOUCHERS' | 'DISCOUNT_COUPONS' | 'REFERRAL_PROGRAM') | undefined, $has_value?: string | undefined, $is_unknown?: string | undefined, $starts_with?: string | undefined, $ends_with?: string | undefined}) | undefined}) | undefined, voucher_type?: ({conditions?: ({$in?: (Array<'GIFT_VOUCHER' | 'DISCOUNT_VOUCHER' | 'LOYALTY_CARD'>) | undefined, $not_in?: (Array<'GIFT_VOUCHER' | 'DISCOUNT_VOUCHER' | 'LOYALTY_CARD'>) | undefined, $is?: ('GIFT_VOUCHER' | 'DISCOUNT_VOUCHER' | 'LOYALTY_CARD') | undefined, $is_not?: ('GIFT_VOUCHER' | 'DISCOUNT_VOUCHER' | 'LOYALTY_CARD') | undefined, $has_value?: string | undefined, $is_unknown?: string | undefined, $starts_with?: string | undefined, $ends_with?: string | undefined}) | undefined}) | undefined, junction?: Junction | undefined}; export type GetApiV1CustomersCustomerIdRedeemablesArgs = {limit?: ParameterLimit | undefined, order?: ParameterOrderListRedeemables | undefined, starting_after_id?: ParameterString | undefined, filters?: ParameterFiltersListCustomerRedeemables | undefined}; export type GetApiV1CustomersCustomerIdRedeemablesOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const discountAmountVouchersEffectTypes = z.enum(["APPLY_TO_ORDER", "APPLY_TO_ITEMS", "APPLY_TO_ITEMS_PROPORTIONALLY", "APPLY_TO_ITEMS_PROPORTIONALLY_BY_QUANTITY", "APPLY_TO_ITEMS_BY_QUANTITY"]); export const discountUnitVouchersEffectTypes = z.enum(["ADD_MISSING_ITEMS", "ADD_NEW_ITEMS", "ADD_MANY_ITEMS", "ADD_SAME_ITEMS"]); export const simpleProductDiscountUnit = z.object({id: z.string(), source_id: z.string().optional(), name: z.string()}); export const simpleSkuDiscountUnit = z.object({id: z.string(), source_id: z.string().optional(), name: z.string()}); export const discountUnitMultipleOneUnit = z.object({unit_off: z.number().optional(), unit_off_formula: z.string().optional(), effect: z.enum(["ADD_NEW_ITEMS", "ADD_MISSING_ITEMS"]), unit_type: z.string(), product: simpleProductDiscountUnit.optional(), sku: simpleSkuDiscountUnit.optional()}); export const discountPercentVouchersEffectTypes = z.enum(["APPLY_TO_ORDER", "APPLY_TO_ITEMS"]); export const discountFixedVouchersEffectTypes = z.enum(["APPLY_TO_ORDER", "APPLY_TO_ITEMS"]); export const discount = z.union([z.object({type: z.literal("AMOUNT"), amount_off: z.number(), amount_off_formula: z.string().optional(), aggregated_amount_limit: z.number().int().optional(), effect: discountAmountVouchersEffectTypes.optional(), is_dynamic: z.boolean().optional()}), z.object({type: z.literal("UNIT"), unit_off: z.number().int().optional(), unit_off_formula: z.string().optional(), effect: discountUnitVouchersEffectTypes.optional(), unit_type: z.string(), product: simpleProductDiscountUnit.optional(), sku: simpleSkuDiscountUnit.optional(), is_dynamic: z.boolean().optional()}), z.object({type: z.literal("UNIT"), effect: z.literal("ADD_MANY_ITEMS").optional(), units: z.array(discountUnitMultipleOneUnit)}), z.object({type: z.literal("PERCENT"), percent_off: z.number(), percent_off_formula: z.string().optional(), amount_limit: z.number().optional(), aggregated_amount_limit: z.number().int().optional(), effect: discountPercentVouchersEffectTypes.optional(), is_dynamic: z.boolean().optional()}), z.object({type: z.literal("FIXED"), fixed_amount: z.number(), fixed_amount_formula: z.string().optional(), effect: discountFixedVouchersEffectTypes.optional(), is_dynamic: z.boolean().optional()})]); export const validityTimeframe = z.object({duration: z.string().optional(), interval: z.string().optional()}); export const validityDayOfWeek = z.array(z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)])); export const validityHours = z.object({daily: z.array(z.object({start_time: z.string().optional(), days_of_week: z.array(z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)])).optional(), expiration_time: z.string().optional()})).optional()}); export const voucherAssets = z.object({qr: z.object({id: z.string().optional(), url: z.string().optional()}).optional(), barcode: z.object({id: z.string().optional(), url: z.string().optional()}).optional()}); export const redeemableVoucher = z.object({id: z.string().optional(), code: z.string().optional(), campaign: z.string().optional(), campaign_id: z.string().optional(), category: z.string().nullable().optional(), category_id: z.string().optional(), categories: z.array(z.object({})).optional(), type: z.enum(["GIFT_VOUCHER", "DISCOUNT_VOUCHER", "LOYALTY_CARD"]).optional(), discount: discount.optional(), gift: z.object({amount: z.number().int().optional(), subtracted_amount: z.number().int().optional(), balance: z.number().int().optional(), effect: z.enum(["APPLY_TO_ORDER", "APPLY_TO_ITEMS"]).optional()}).optional(), loyalty_card: z.object({points: z.number().int().optional(), balance: z.number().int().optional(), next_expiration_date: z.string().optional(), next_expiration_points: z.number().int().optional(), pending_points: z.number().int().optional(), expired_points: z.number().int().optional(), subtracted_points: z.number().int().optional()}).optional(), start_date: z.string().optional(), expiration_date: z.string().optional(), validity_timeframe: validityTimeframe.optional(), validity_day_of_week: validityDayOfWeek.optional(), validity_hours: validityHours.optional(), active: z.boolean().nullable().optional(), additional_info: z.string().optional(), metadata: z.object({}).optional(), assets: voucherAssets.optional(), is_referral_code: z.boolean().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), referrer_id: z.string().optional(), holder_id: z.string().optional(), object: z.string().optional(), publish: z.object({object: z.string().optional(), count: z.number().int().optional(), url: z.string().optional()}).optional(), redemption: z.object({quantity: z.number().int().optional(), redeemed_quantity: z.number().int().optional(), redeemed_points: z.number().int().optional(), object: z.string().optional(), url: z.string().optional()}).optional()}); export const customerRedeemable = z.object({id: z.string(), created_at: z.string(), redeemable_id: z.string(), redeemable_object: z.string(), customer_id: z.string(), holder_role: z.enum(["OWNER", "REFERRER", "REFEREE"]), campaign_id: z.string().nullable(), campaign_type: z.enum(["LOYALTY_PROGRAM", "GIFT_VOUCHERS", "DISCOUNT_COUPONS", "PROMOTION", "REFERRAL_PROGRAM"]).nullable(), voucher_type: z.enum(["GIFT_VOUCHER", "DISCOUNT_VOUCHER", "LOYALTY_CARD"]), redeemable: z.object({type: z.string(), voucher: redeemableVoucher, status: z.enum(["ACTIVE", "USED", "DISABLED", "NOT_ACTIVE_YET", "EXPIRED", "NO_BALANCE"])})}); export const customersRedeemablesListResponseBody = z.object({object: z.string(), data_ref: z.string(), data: z.array(customerRedeemable), total: z.number().int(), has_more: z.boolean(), more_starting_after: 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 }; /** * List Customer's Redeemables * * GET /v1/customers/{customerId}/redeemables * * Also accepts auth: X-Voucherify-OAuth */ export const getApiV1CustomersCustomerIdRedeemables = async (args: GetApiV1CustomersCustomerIdRedeemablesArgs, options?: GetApiV1CustomersCustomerIdRedeemablesOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.starting_after_id != null) query.append("starting_after_id", String(args.starting_after_id)) if (args.filters != null) query.append("filters", JSON.stringify(args.filters)) if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customers/${encodeURIComponent(String(args.customerId))}/redeemables${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(customersRedeemablesListResponseBody, await res.json()) };