orderNumber.ts
Language TypeScript1.6 KB49 lines
/*** Customer-facing display numbers (e.g. "MD2639582") are neither accepted nor* returned by the API: resolution to the raw `order_ref` is client-side only.** Rules (see docs/PLINTH-API.md, "Order numbers"):* - current: ^MD(\d{2})(\d+)$ -> strip "MD" + the 2-digit year; the rest IS* the order_ref.* - a year later than this one: accept with a warning (likely a typo).* - legacy: ^M-\d{6}$ (from before Plinth) -> refuse; route to a human.*/export type ResolveAction = "accept" | "warn" | "refuse";export interface DisplayNumberResolution {action: ResolveAction;/** Present when action is "accept" or "warn". */orderId?: number;/** Human-readable reason, for warn/refuse. */note?: string;}const CURRENT = /^MD(\d{2})(\d+)$/;const LEGACY = /^M-\d{6}$/;export function resolveDisplayNumber(display: string, now: Date = new Date()): DisplayNumberResolution {const value = display.trim().toUpperCase();const current = CURRENT.exec(value);if (current) {const orderId = Number(current[2]);if (Number(current[1]) > now.getUTCFullYear() % 100) {return {action: "warn",orderId,note: "The year in this number is later than the current year; check it was typed correctly.",};}return { action: "accept", orderId };}if (LEGACY.test(value)) {return {action: "refuse",note: "Legacy number from before Plinth; it is not resolvable by rule. Route to a human.",};}return { action: "refuse", note: `Unrecognized display-number format: ${display}` };}