import {z} from 'zod' export type PluId = {list_code: string, code: string}; export type PropertyRecord = {property_record_id?: string | undefined, property_id: string, property_name?: string | undefined, property_value?: (string | number | number | boolean | Array | Record) | undefined, last_change?: string | undefined}; export type BillItem = {plu_ids: Array, plu_name: string, category_plu_id?: string | undefined, vat_rate: number, quantity: number, paid_amount: number, price: number, bill_item_id: string, loyalty_off: boolean, purchase_item_type_id: string, customer_id?: string | undefined, purchase_item_property_records?: Array | undefined}; export type Bill = {bill_id: string, bill_number?: string | undefined, payment_time: string, created_by?: string | undefined, currency_id: string, total_price: number, bill_items: Array}; export type CreateApiPurchasesActionsRecommendedBestRewardsArgs = {'Accept-Language'?: string | undefined, body: {store_id: string, cashdesk_number: number, card_number?: string | undefined, customer_id?: string | undefined, reward_list_type: 'F' | 'C' | 'A', bill: Bill}}; export type CreateApiPurchasesActionsRecommendedBestRewardsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rewardProperties = z.object({image: z.string().optional(), valid_time_from: z.string().optional(), valid_time_to: z.string().optional()}); export const recommendedReward = z.object({code: z.string().optional(), name: z.string().optional(), reward_priority: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9)]).optional(), max_count: z.number().int().optional(), description: z.string().optional(), reward_properties: rewardProperties.optional()}); export const createApiPurchasesActionsRecommendedBestRewardsResponse = z.object({data: z.object({recommended_best_rewards: z.array(recommendedReward).optional(), total_items: z.number().int().optional()}).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 }; /** * Provides a list of rewards and vouchers available for purchase closure * * POST /purchases/actions/recommended-best-rewards * * Also accepts auth: basicAuth */ export const createApiPurchasesActionsRecommendedBestRewards = async (args: CreateApiPurchasesActionsRecommendedBestRewardsArgs, options?: CreateApiPurchasesActionsRecommendedBestRewardsOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/purchases/actions/recommended-best-rewards${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(createApiPurchasesActionsRecommendedBestRewardsResponse, await res.json()) };