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 ComputeApplicableRewardsRequest = {userId: string, order?: Order2 | undefined}; export type CreateApiManagerLoyaltyV1SourceRewardsEvaluateArgs = {'X-Store-Id': StoreId, source: string, body: ComputeApplicableRewardsRequest}; export type CreateApiManagerLoyaltyV1SourceRewardsEvaluateOptions = {baseUrl?: string | undefined, OAuth20?: string | undefined, fetch?: typeof fetch | undefined}; export const triggerMenu = z.object({entityId: z.string().optional(), entityType: z.enum(["ITEM", "CATEGORY"]).optional(), minimumPurchaseQuantity: z.number().optional()}); export const trigger = z.object({menus: z.array(triggerMenu).optional()}); export const subtotalReward = z.object({amount: z.number().optional(), type: z.enum(["ABSOLUTE_DISCOUNT", "PERCENT_DISCOUNT"]).optional()}); export const menusReward = z.object({entityId: z.string().optional(), amount: z.number().optional(), type: z.enum(["ABSOLUTE_DISCOUNT", "PERCENT_DISCOUNT"]).optional()}); export const reward = z.object({id: z.string().optional(), type: z.enum(["SUBTOTAL", "MENUS"]).optional(), cost: z.number().optional(), costType: z.enum(["POINT", "DOLLAR"]).optional(), trigger: trigger.optional(), subtotal: subtotalReward.optional(), menus: z.array(menusReward).optional()}); export const computeApplicableRewardsResponse = z.object({rewards: z.array(reward).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 }; /** * Compute applicable rewards * * POST /manager/loyalty/v1/{source}/rewards/evaluate */ export const createApiManagerLoyaltyV1SourceRewardsEvaluate = async (args: CreateApiManagerLoyaltyV1SourceRewardsEvaluateArgs, options?: CreateApiManagerLoyaltyV1SourceRewardsEvaluateOptions) => { 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/evaluate${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(computeApplicableRewardsResponse, await res.json()) };