mattrguerra/meridian-orders-mcpmainPrivate on GitHub, published here in full

orderNumber.ts

Language TypeScript1.6 KB49 lines

mcp-server/src/lib/orderNumber.ts

  1. /**
  2. * Customer-facing display numbers (e.g. "MD2639582") are neither accepted nor
  3. * returned by the API: resolution to the raw `order_ref` is client-side only.
  4. *
  5. * Rules (see docs/PLINTH-API.md, "Order numbers"):
  6. * - current: ^MD(\d{2})(\d+)$ -> strip "MD" + the 2-digit year; the rest IS
  7. * the order_ref.
  8. * - a year later than this one: accept with a warning (likely a typo).
  9. * - legacy: ^M-\d{6}$ (from before Plinth) -> refuse; route to a human.
  10. */
  11. export type ResolveAction = "accept" | "warn" | "refuse";
  12. export interface DisplayNumberResolution {
  13. action: ResolveAction;
  14. /** Present when action is "accept" or "warn". */
  15. orderId?: number;
  16. /** Human-readable reason, for warn/refuse. */
  17. note?: string;
  18. }
  19. const CURRENT = /^MD(\d{2})(\d+)$/;
  20. const LEGACY = /^M-\d{6}$/;
  21. export function resolveDisplayNumber(display: string, now: Date = new Date()): DisplayNumberResolution {
  22. const value = display.trim().toUpperCase();
  23. const current = CURRENT.exec(value);
  24. if (current) {
  25. const orderId = Number(current[2]);
  26. if (Number(current[1]) > now.getUTCFullYear() % 100) {
  27. return {
  28. action: "warn",
  29. orderId,
  30. note: "The year in this number is later than the current year; check it was typed correctly.",
  31. };
  32. }
  33. return { action: "accept", orderId };
  34. }
  35. if (LEGACY.test(value)) {
  36. return {
  37. action: "refuse",
  38. note: "Legacy number from before Plinth; it is not resolvable by rule. Route to a human.",
  39. };
  40. }
  41. return { action: "refuse", note: `Unrecognized display-number format: ${display}` };
  42. }