FPSMS-frontend
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

68 righe
1.7 KiB

  1. import type { PoResult } from "@/app/api/po";
  2. import type { PoWorkbenchListRow } from "@/components/PoWorkbench/types";
  3. const RECEIVE_STATUSES = new Set(["pending", "receiving", "completed"]);
  4. function normalizeReceiveStatus(status: string): PoWorkbenchListRow["status"] {
  5. if (RECEIVE_STATUSES.has(status)) {
  6. return status as PoWorkbenchListRow["status"];
  7. }
  8. return "pending";
  9. }
  10. function pad2(n: number): string {
  11. return n < 10 ? `0${n}` : String(n);
  12. }
  13. /**
  14. * Normalizes backend date values into `YYYY-MM-DD`.
  15. * Supports both ISO strings and Jackson-style arrays (`[yyyy, mm, dd, ...]`).
  16. */
  17. function toYmd(value: unknown): string {
  18. if (value == null) {
  19. return "";
  20. }
  21. if (Array.isArray(value)) {
  22. const y = Number(value[0]);
  23. const m = Number(value[1]);
  24. const d = Number(value[2]);
  25. if (
  26. Number.isInteger(y) &&
  27. Number.isInteger(m) &&
  28. Number.isInteger(d) &&
  29. y > 0 &&
  30. m >= 1 &&
  31. m <= 12 &&
  32. d >= 1 &&
  33. d <= 31
  34. ) {
  35. return `${y}-${pad2(m)}-${pad2(d)}`;
  36. }
  37. return "";
  38. }
  39. if (typeof value !== "string") {
  40. return "";
  41. }
  42. const t = value.trim();
  43. if (t.length >= 10 && /^\d{4}-\d{2}-\d{2}/.test(t)) {
  44. return t.slice(0, 10);
  45. }
  46. return t;
  47. }
  48. /** Maps one PO list API row into the shape the workbench list UI already expects. */
  49. export function mapPoResultToListRow(po: PoResult): PoWorkbenchListRow {
  50. return {
  51. id: String(po.id),
  52. poNumber: po.code,
  53. supplierName: po.supplier ?? "",
  54. orderDate: toYmd(po.orderDate),
  55. estimatedArrivalDate: toYmd(po.estimatedArrivalDate),
  56. escalated: Boolean(po.escalated),
  57. status: normalizeReceiveStatus(po.status ?? ""),
  58. };
  59. }