import {z} from 'zod' export type GetApiBulkmodificationtoolOrderSearchArgs = {productId: number, boxIds: Array, startDate: string, endDate: string, includeDIY: boolean, substitutions?: (boolean | null) | undefined, isMember?: (boolean | null) | undefined, isEISHCustomer?: (boolean | null) | undefined}; export type GetApiBulkmodificationtoolOrderSearchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const userBasic = z.object({active: z.boolean().optional()}); export const streetAddressType = z.enum(["USER", "GUEST", "STORE"]); export const province = z.enum(["ALBERTA", "BRITISH_COLUMBIA", "MANITOBA", "NEW_BRUNSWICK", "NEWFOUNDLAND_AND_LABRADOR", "NORTHWEST_TERRITORIES", "NOVA_SCOTIA", "NUNAVUT", "ONTARIO", "PRINCE_EDWARD_ISLAND", "QUEBEC", "SASKATCHEWAN", "YUKON_TERRITORY"]); export const phoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber = z.object({value: z.string()}); export const streetAddress = z.object({type: streetAddressType, name: z.string(), line1: z.string(), line2: z.string(), city: z.string(), province: province, postalCode: z.string(), telephone: phoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber, instructions: z.string(), buildingType: z.number().int(), latitude: z.number(), longitude: z.number(), id: z.number().int().optional()}); export const ondOrderTimeslot = z.object({name: z.string(), start: z.string(), end: z.string()}); export const ondDeliveryPreferences = z.object({callAhead: z.boolean(), keyId: z.string().nullable()}); export const feeDiscountFeeDiscount = z.object({}); export const appliedDiscount = z.object({isMembershipDiscount: z.boolean().optional(), isSubscriptionDiscount: z.boolean().optional(), isCouponDiscount: z.boolean().optional(), amount: z.number()}); export const totalsTotalWarning = z.object({title: z.string(), details: z.string()}); export const totals = z.object({amountBelowMinimumOrderValue: z.number(), subtotal: z.number(), totalDiscount: z.number(), credits: z.number(), totalSubscriptionDiscount: z.number(), potentialSubscriptionDiscount: z.number(), totalMembershipDiscount: z.number(), potentialMembershipDiscount: z.number(), totalFees: z.number(), totalTax: z.number(), grandTotal: z.number(), fees: z.array(feeDiscountFeeDiscount), discounts: z.array(appliedDiscount), warnings: z.array(totalsTotalWarning)}); export const packagingType = z.enum(["SINGLE_USE", "REUSABLE"]); export const deliveryProvider = z.object({code: z.string(), name: z.string()}); export const deliveryDriver = z.object({externalId: z.string(), displayName: z.string()}); export const deliveryRouteInfo = z.object({date: z.string(), code: z.string(), routeSequence: z.number().int(), stopSequence: z.number().int(), barcode: z.string(), provider: deliveryProvider, driver: deliveryDriver}); export const customerTier = z.enum(["A_LA_CARTE", "PRODUCT_SUBSCRIBER", "BAG_SUBSCRIBER", "MEMBER"]); export const ondOrder = z.object({id: z.number().int(), date: z.string(), created: z.string(), modified: z.string(), visitedAt: z.string(), user: userBasic, deliveryAddress: streetAddress, timeslot: ondOrderTimeslot.nullable(), deliveryPreferences: ondDeliveryPreferences, totals: totals, packagingType: packagingType, isPrime: z.boolean(), isSubstitutionAllowed: z.boolean(), hasAlcohol: z.boolean(), estimatedBinsRequired: z.number().int(), estimatedBinsReturned: z.number().int(), routeInfo: deliveryRouteInfo.nullable(), ordinality: z.number().int(), tier: customerTier.nullable(), packingOrder: z.number().int().nullable()}); export const bagType = z.enum(["REGULAR", "VALUE"]); export const priceAvailability = z.object({startDate: z.string(), endDate: z.string()}); export const price = z.object({promo: z.boolean(), value: z.number(), originalValue: z.number().nullable(), availability: priceAvailability.nullable().optional()}); export const bag = z.object({id: z.number().int(), erpItemId: z.number().int(), name: z.string(), shortName: z.string(), bagType: bagType, description: z.string(), shortDescription: z.string(), contentsProfile: z.string(), contentsDescription: z.string(), portionText: z.string(), portionSize: z.number().int(), uri: z.string(), imageUrl: z.string(), price: price, customizationFee: z.number(), points: z.number().int().nullable(), allowExtraItems: z.boolean(), extraBagPointPrice: z.number(), published: z.boolean(), immutable: z.boolean(), allowedIntervals: z.array(z.number().int()), allowedCategoryCodes: z.array(z.string()), isDIY: z.boolean(), isFeatured: z.boolean(), isOrganic: z.boolean()}); export const bagCustomizedBy = z.enum(["SYSTEM", "CUSTOMER", "ADMIN"]); export const coupon = z.object({}); export const orderSearchResult = z.object({addonId: z.number().int(), order: ondOrder, bag: bag, customizedBy: bagCustomizedBy.nullable(), productPrice: z.number(), productQuantity: z.number().int(), bagPoints: z.number().int().nullable(), coupon: coupon.nullable(), substitutions: z.boolean(), oneOff: z.boolean(), isMember: z.boolean(), isEISHCustomer: z.boolean()}); export const getApiBulkmodificationtoolOrderSearchResponse = z.array(orderSearchResult); 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 }; /** * GET /bulkmodificationtool/order/search */ export const getApiBulkmodificationtoolOrderSearch = async (args: GetApiBulkmodificationtoolOrderSearchArgs, options: GetApiBulkmodificationtoolOrderSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.productId != null) query.append("productId", String(args.productId)) for (const item of args.boxIds ?? []) query.append("boxIds", String(item)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.includeDIY != null) query.append("includeDIY", String(args.includeDIY)) if (args.substitutions != null) query.append("substitutions", String(args.substitutions)) if (args.isMember != null) query.append("isMember", String(args.isMember)) if (args.isEISHCustomer != null) query.append("isEISHCustomer", String(args.isEISHCustomer)) const url = `${baseUrl.replace(/\/+$/, '')}/bulkmodificationtool/order/search${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(getApiBulkmodificationtoolOrderSearchResponse, await res.json()) };