import {z} from 'zod' export type GetApiApiV1InventoryMovementsArgs = {sku?: string | undefined, q?: string | undefined, strategyCode?: Array | undefined, reason?: string | undefined, updateType?: string | undefined, occurredFrom?: string | undefined, occurredTo?: string | undefined, page?: number | undefined, limit?: number | undefined, sortBy?: string | undefined, sortOrder?: string | undefined}; export type GetApiApiV1InventoryMovementsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const inventoryMovementBalanceAfterResponse = z.object({on_hand: z.number(), reserved: z.number(), blocked: z.number()}); export const inventoryMovementListItemResponse = z.object({update_type: z.literal("SNAPSHOT"), reason: z.enum(["ADJUSTMENT", "ERP_SYNC", "ORDER_RELEASED", "ORDER_RESERVED", "POS_SALE", "RETURN_RECEIVED", "SAFETY_STOCK_CONFIG", "STOCK_SHORTAGE", "TRANSFER"]), balance_after: inventoryMovementBalanceAfterResponse.nullable(), product_name: z.string().nullable(), strategy_name: z.string().nullable(), id: z.string(), event_id: z.string(), sku: z.string(), strategy_code: z.string(), quantity: z.number(), occurred_at: z.string()}); export const paginationMetaResponse = z.object({total: z.number(), pages: z.number(), current_page: z.number(), next_page: z.number().nullable(), previous_page: z.number().nullable(), limit: z.number()}); export const inventoryMovementsListResponse = z.object({data: z.array(inventoryMovementListItemResponse), meta: paginationMetaResponse}); 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 }; /** * List inventory movements * * GET /api/v1/inventory-movements * * Also accepts auth: bearer */ export const getApiApiV1InventoryMovements = async (args: GetApiApiV1InventoryMovementsArgs, options?: GetApiApiV1InventoryMovementsOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sku != null) query.append("sku", String(args.sku)) if (args.q != null) query.append("q", String(args.q)) for (const item of args.strategyCode ?? []) query.append("strategyCode", String(item)) if (args.reason != null) query.append("reason", String(args.reason)) if (args.updateType != null) query.append("updateType", String(args.updateType)) if (args.occurredFrom != null) query.append("occurredFrom", String(args.occurredFrom)) if (args.occurredTo != null) query.append("occurredTo", String(args.occurredTo)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/inventory-movements${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(inventoryMovementsListResponse, await res.json()) };