import {z} from 'zod' export type StoreId = string; export type Item2 = {id?: string | undefined, displayName?: string | undefined, quantity?: number | undefined, price?: number | undefined, modifiers?: Array | undefined}; export type Totals = {subTotal?: number | undefined, tax?: number | undefined, discount?: number | undefined, total?: number | undefined}; export type Order2 = {id?: string | undefined, currencyCode?: string | undefined, items?: Array | undefined, totals?: Totals | undefined}; export type SelectedMenuReward = {entityId?: string | undefined}; export type SelectedReward = {id?: string | undefined, amount?: number | undefined, menus?: SelectedMenuReward | undefined}; export type RedeemAndAccumulateRewardsRequest = {userId: string, order: Order2, selectedRewards: Array}; export type CreateApiManagerLoyaltyV1SourceRewardsRedeemAndAccumulateArgs = {'X-Store-Id': StoreId, source: string, body: RedeemAndAccumulateRewardsRequest}; export type CreateApiManagerLoyaltyV1SourceRewardsRedeemAndAccumulateOptions = {baseUrl?: string | undefined, OAuth20?: string | undefined, fetch?: typeof fetch | undefined}; export const subtotal = z.object({amount: z.number().optional()}); export const rewardEffect = z.object({subtotal: subtotal.optional()}); export const userBalance = z.object({amount: z.number().optional(), type: z.enum(["POINT", "DOLLAR"]).optional()}); export const redeemAndAccumulateRewardsResponse = z.object({transactionId: z.string().optional(), rewardEffect: rewardEffect.optional(), accumulatedRewards: z.array(userBalance).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 }; /** * Redeem and accumulate rewards * * POST /manager/loyalty/v1/{source}/rewards/redeem-and-accumulate */ export const createApiManagerLoyaltyV1SourceRewardsRedeemAndAccumulate = async (args: CreateApiManagerLoyaltyV1SourceRewardsRedeemAndAccumulateArgs, options?: CreateApiManagerLoyaltyV1SourceRewardsRedeemAndAccumulateOptions) => { const {baseUrl = "https://partners.tryotter.com", fetch: fetchFn = fetch, OAuth20} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Store-Id'] != null) headers.set("X-Store-Id", String(args['X-Store-Id'])) if (OAuth20 !== undefined) headers.set('authorization', `Bearer ${OAuth20}`) const url = `${baseUrl.replace(/\/+$/, '')}/manager/loyalty/v1/${encodeURIComponent(String(args.source))}/rewards/redeem-and-accumulate${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(redeemAndAccumulateRewardsResponse, await res.json()) };