mattrguerra/meridian-ops-boardmainPrivate on GitHub, published here in full

order-number.ts

Language TypeScript1.7 KB37 lines

packages/plinth-client/src/order-number.ts

  1. /**
  2. * Customer-facing order numbers.
  3. *
  4. * Plinth stores a bare integer `order_ref`. What everyone at Meridian actually says out
  5. * loud, on the phone, in Bitrix, on a carrier claim, is the display number:
  6. *
  7. * MD + two-digit year of the order + order_ref
  8. * 39582 placed in 2026 -> MD2639582
  9. *
  10. * The dashboard must show the display number. A designer handed "39582" has to
  11. * translate it before they can search for it, and a number nobody can paste into
  12. * Plinth is decoration rather than information.
  13. *
  14. * The year comes from when the order was placed, which in Plinth is
  15. * `closed_on` despite the name (see projection.ts). Without a date
  16. * there is no correct prefix, so this returns the bare id rather than guessing
  17. * the current year: a wrong order number is worse than an obviously partial one,
  18. * because it looks searchable and silently is not.
  19. *
  20. * "MD" is hardcoded, and that is correct. Meridian runs a single
  21. * storefront, so there is no second prefix to choose between. If another one
  22. * ever opens, this is the function to revisit, and it will need a storefront
  23. * field on `orders.fetch` rather than a guess.
  24. */
  25. /** "MD2639582", or "39582" when the placed date is unknown. */
  26. export function displayOrderNumber(orderId: number, placedAt: string | null): string {
  27. if (!placedAt) return String(orderId)
  28. const year = new Date(placedAt).getUTCFullYear()
  29. if (!Number.isFinite(year)) return String(orderId)
  30. return `MD${String(year % 100).padStart(2, '0')}${orderId}`
  31. }
  32. /** True when the number is the real, pasteable one rather than a bare id. */
  33. export function isDisplayOrderNumber(value: string): boolean {
  34. return /^[A-Z]{2}\d{2}\d+$/.test(value.trim().toUpperCase())
  35. }