import {z} from 'zod' export type OrderItemForUpdate = {id: number, quantity: number, unitPrice: number}; export type BulkModificationOperation = 'ADD' | 'CHANGE_QUANTITY' | 'DELETE' | 'SUBSTITUTE' | 'CREDIT_ONLY'; export type BulkModificationItem = {productId: number, quantity: number}; export type CreditReason = 'COUPONS' | 'CANCELLED_ORDER' | 'EMPLOYEE_CREDIT' | 'FRIEND_REFERRAL' | 'GIFT_CARD' | 'GOOD_WILL_CREDIT' | 'LOGISTICS' | 'MARKETING' | 'MISSING_ORDER' | 'MISSING_ITEM' | 'ORDER_ADJUSTMENT' | 'OUT_OF_STOCK' | 'PACKING_ERROR' | 'PAYMENT_ON_ACCOUNT' | 'PRODUCT_QUALITY' | 'STAFF_EP_DISCOUNT' | 'SYSTEM_ERROR' | 'MEO_IMPORT' | 'BIN_RETURN'; export type BulkModificationInternalReason = 'VENDOR' | 'CUSTOMER' | 'BUSINESS_PROCESS' | 'PICK_PACK' | 'PURCHASING' | 'EXPECTED' | 'OTHER'; export type BulkModificationNotificationMode = 'IMMEDIATE' | 'BATCH' | 'NONE'; export type CreateApiBulkmodificationtoolOrderUpdateArgs = {body: {addons?: (Array | null) | undefined, bagItems?: (Array | null) | undefined, operation: BulkModificationOperation, newItems?: Array | undefined, startDate: string, endDate: string, applyProductCredit?: boolean | undefined, applyGoodWillCredit?: boolean | undefined, goodWillCreditAmount?: (number | null) | undefined, creditReason?: (CreditReason | null) | undefined, internalReason?: (BulkModificationInternalReason | null) | undefined, internalReasonNote?: (string | null) | undefined, notifyUsers: BulkModificationNotificationMode}}; export type CreateApiBulkmodificationtoolOrderUpdateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const namedUrl = z.object({name: z.string().min(1), url: z.string().min(1)}); export const bulkModificationUpdateResult = z.object({addonsAffected: z.number().int(), progressLink: namedUrl}); 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 }; /** * POST /bulkmodificationtool/order/update */ export const createApiBulkmodificationtoolOrderUpdate = async (args: CreateApiBulkmodificationtoolOrderUpdateArgs, options: CreateApiBulkmodificationtoolOrderUpdateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/bulkmodificationtool/order/update${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(bulkModificationUpdateResult, await res.json()) };