getOrders.ts
Language TypeScript3.3 KB77 lines
import { z } from "zod";import type { ToolModule } from "./types.js";import { plinthRequest, type PlinthParams } from "../lib/http.js";import { toRequestDate } from "../lib/dates.js";/*** get_orders: retrieve one order or a batch (Plinth method `orders.fetch`).** Wire notes:* - `order_ref` is a RAW integer, not the MD-prefixed display number (resolve* that via lib/orderNumber.ts first).* - `customer_ref` ALONE errors ({"error":"Invalid request"}); it must be paired* with since/until dates.* - Request dates are yyyymmdd (lib/dates.ts:toRequestDate); ISO is rejected.* - Response container is `order_map`, orders keyed `ord_{order_ref}`; `lines`* and `shipments` are ordinal-keyed (lib/collections.ts:normalizeCollection).*/export const getOrders: ToolModule = {name: "get_orders",config: {title: "Get orders",description:"Retrieve one order or a batch, filterable by order ref, customer, shop, status, or date range. Read-only.",inputSchema: {orderId: z.number().int().positive().optional().describe("Raw integer order_ref (not the MD display number)."),compare: z.enum(["below", "above"]).optional().describe("With orderId: orders below/above that ref."),customerId: z.number().int().positive().optional().describe("Customer ref; MUST be paired with fromDate + toDate."),shop: z.string().optional().describe("Shop id, e.g. '2'."),includeSubshops: z.boolean().optional().describe("With shop: include its child shops (default true)."),statusCode: z.string().optional().describe("Status code from get_status_catalog."),fromDate: z.string().optional().describe("ISO YYYY-MM-DD; converted to yyyymmdd on the wire."),toDate: z.string().optional().describe("ISO YYYY-MM-DD; converted to yyyymmdd on the wire."),offset: z.number().int().min(0).optional(),limit: z.number().int().min(1).max(100).optional().describe("Documented cap 100; observed default may be 50."),},},async handler(args, ctx) {const { orderId, compare, customerId, shop, includeSubshops, statusCode, fromDate, toDate, offset, limit } =args as {orderId?: number;compare?: "below" | "above";customerId?: number;shop?: string;includeSubshops?: boolean;statusCode?: string;fromDate?: string;toDate?: string;offset?: number;limit?: number;};if (customerId !== undefined && (fromDate === undefined || toDate === undefined)) {return {content: [{ type: "text", text: "customerId must be combined with both fromDate and toDate (the API rejects it otherwise)." },],isError: true,};}const params: PlinthParams = {order_ref: orderId,ref_cmp: compare,customer_ref: customerId,shop,subshops: shop !== undefined && includeSubshops !== undefined ? (includeSubshops ? 1 : 0) : undefined,status_code: statusCode,since: fromDate !== undefined ? toRequestDate(fromDate) : undefined,until: toDate !== undefined ? toRequestDate(toDate) : undefined,offset,limit,};const body = await plinthRequest(ctx, "orders.fetch", params);return { content: [{ type: "text", text: JSON.stringify(body, null, 2) }] };},};