import {z} from 'zod' export type Customer = {id?: string | undefined, source_id?: string | undefined, name?: string | undefined, description?: string | undefined, email?: string | undefined, phone?: string | undefined, birthday?: string | undefined, birthdate?: string | undefined, address?: ({city?: string | undefined, state?: string | undefined, line_1?: string | undefined, line_2?: string | undefined, country?: string | undefined, postal_code?: string | undefined} | null) | undefined, metadata?: Record | undefined}; export type CustomerId = {id: string, object: 'customer'}; export type ReferrerId = {id: string, object: 'customer'}; export type LoyaltiesQualificationsCheckEligibilityRequestBody = {customer: Customer, order: {id?: string | undefined, source_id?: string | undefined, status?: ('CREATED' | 'PAID' | 'CANCELED' | 'FULFILLED') | undefined, amount?: number | undefined, initial_amount?: number | undefined, discount_amount?: number | undefined, items?: (Array<{id?: string | undefined, sku_id?: string | undefined, product_id?: string | undefined, related_object?: ('product' | 'sku') | undefined, source_id?: string | undefined, quantity?: number | undefined, discount_quantity?: number | undefined, initial_quantity?: number | undefined, amount?: number | undefined, discount_amount?: number | undefined, applied_discount_amount?: number | undefined, applied_discount_quantity?: number | undefined, applied_quantity?: number | undefined, applied_quantity_amount?: number | undefined, initial_amount?: number | undefined, price?: number | undefined, subtotal_amount?: number | undefined, product?: ({id?: string | undefined, source_id?: string | undefined, override?: boolean | undefined, name?: string | undefined, metadata?: Record | undefined, price?: number | undefined}) | undefined, sku?: ({id?: string | undefined, source_id?: string | undefined, override?: boolean | undefined, sku?: string | undefined, price?: number | undefined, metadata?: Record | undefined}) | undefined, object: 'order_item', metadata?: Record | undefined}>) | undefined, customer_id?: (string | null) | undefined, referrer_id?: (string | null) | undefined, customer?: CustomerId | undefined, referrer?: ReferrerId | undefined, metadata?: Record | undefined}}; export type CreateApiV1LoyaltiesCampaignIdQualificationsArgs = {body: LoyaltiesQualificationsCheckEligibilityRequestBody}; export type CreateApiV1LoyaltiesCampaignIdQualificationsOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const loyaltiesQualificationsCheckEligibilityResponseBody = z.object({campaign: z.object({id: z.string(), name: z.string(), object: z.literal("campaign")}), points_estimation: z.number().int()}); 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 }; /** * Estimate loyalty points * * POST /v1/loyalties/{campaignId}/qualifications * * Also accepts auth: X-Voucherify-OAuth */ export const createApiV1LoyaltiesCampaignIdQualifications = async (args: CreateApiV1LoyaltiesCampaignIdQualificationsArgs, options?: CreateApiV1LoyaltiesCampaignIdQualificationsOptions) => { 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(/\/+$/, '')}/v1/loyalties/${encodeURIComponent(String(args.campaignId))}/qualifications${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(loyaltiesQualificationsCheckEligibilityResponseBody, await res.json()) };