Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

11.8 KB
Raw Blame
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.

/**
 * Onshape indented BOM → typed tree. Columns are addressed by Onshape's
 * property name, then by display name (`headerIdFor`); nesting
 * comes from the dotted "Item" index ("1.1.2"). Non-empty cells are
 * kept as properties keyed by column id, so a row resolves through the
 * company's property map like a part.
 */

import { isNotApplicable, type OnshapePropertyValue } from "./properties";

export type OnshapeBomNode = {
  /** Dotted position, e.g. "1.2". */
  index: string;
  level: number;
  partNumber: string | null;
  revision: string | null;
  name: string | null;
  description: string | null;
  quantity: number;
  /** "Show Assembly only": one line, no components. Not a make/buy signal. */
  shownAsUnit?: true;
  standardContent?: true;
  /** Non-empty cells, one per column. Dropped before a plan is stored. */
  properties?: OnshapePropertyValue[];
  itemSource: {
    documentId?: string;
    elementId?: string;
    partId?: string;
    wvmType?: string;
    wvmId?: string;
    /** Onshape's configuration string; "default" for the default one. */
    configuration?: string;
  } | null;
  children: OnshapeBomNode[];
};

type BomHeader = {
  id: string;
  name: string;
  propertyName?: unknown;
  valueType?: unknown;
};
type BomRow = {
  headerIdToValue?: Record<string, unknown>;
  itemSource?: Record<string, unknown>;
  isStandardContent?: unknown;
};

/** "Subassembly BOM behavior" label; the API returns it as text. */
const SHOWN_AS_UNIT = "show assembly only";

function cell(value: unknown): string | null {
  if (value == null) return null;
  if (typeof value === "object") {
    const display = (value as Record<string, unknown>).displayName;
    return typeof display === "string" && display.trim() !== ""
      ? display
      : null;
  }
  const text = String(value).trim();
  return text === "" ? null : text;
}

/**
 * Without "Item" every row reads as the root and a push would apply an empty
 * BOM. "Part number" is the join key to Carbon. Without "Quantity" every line
 * would push as 1.
 */
export const BOM_REQUIRED_COLUMNS = [
  "Item",
  "Part number",
  "Quantity"
] as const;

/**
 * Onshape's property name for each column the parser reads. A BOM template
 * can relabel a column ("PART NUMBER", "Qty") but never renames its property.
 */
const COLUMN_PROPERTY_NAMES: Record<string, string> = {
  Item: "item",
  "Part number": "partNumber",
  Quantity: "quantity",
  Revision: "revision",
  Name: "name",
  Description: "description",
  "Subassembly BOM behavior": "bomBehavior"
};

const labelKey = (label: string) => label.trim().toLowerCase();

/**
 * The column id for a label: by property name, then by the exact label (the
 * lookup this replaced), then by label ignoring case. A repeated label keeps
 * its last header, as that lookup did.
 */
function headerIdFor(headers: BomHeader[]) {
  const byPropertyName = new Map<string, string>();
  const byExactLabel = new Map<string, string>();
  const byLabel = new Map<string, string>();
  for (const header of headers) {
    if (typeof header.propertyName === "string") {
      byPropertyName.set(header.propertyName, header.id);
    }
    if (typeof header.name === "string") {
      byExactLabel.set(header.name, header.id);
      byLabel.set(labelKey(header.name), header.id);
    }
  }
  return (label: string) => {
    const propertyName = COLUMN_PROPERTY_NAMES[label];
    return (
      (propertyName ? byPropertyName.get(propertyName) : undefined) ??
      byExactLabel.get(label) ??
      byLabel.get(labelKey(label))
    );
  };
}

export function parseBomTree(payload: unknown): {
  /** The assembly's own row (index "0"), when the export included it. */
  root: OnshapeBomNode | null;
  lines: OnshapeBomNode[];
  /** Non-empty: callers refuse to plan from `lines`. */
  missingColumns: string[];
} {
  const bom = (payload ?? {}) as {
    headers?: BomHeader[];
    rows?: BomRow[];
  };
  const headers = Array.isArray(bom.headers) ? bom.headers : [];
  const rows = Array.isArray(bom.rows) ? bom.rows : [];

  const headerId = headerIdFor(headers);
  const missingColumns =
    rows.length === 0
      ? []
      : BOM_REQUIRED_COLUMNS.filter((name) => !headerId(name));
  const get = (row: BomRow, name: string) => {
    const id = headerId(name);
    return id ? cell(row.headerIdToValue?.[id]) : null;
  };

  const nodes: OnshapeBomNode[] = rows.map((row) => {
    const index = get(row, "Item") ?? "";
    const quantityText = get(row, "Quantity");
    const quantity = quantityText ? Number(quantityText) : Number.NaN;
    const source = row.itemSource ?? null;
    const shownAsUnit =
      get(row, "Subassembly BOM behavior")?.toLowerCase() === SHOWN_AS_UNIT;
    const standardContent =
      row.isStandardContent === true || source?.isStandardContent === true;
    const properties: OnshapePropertyValue[] = [];
    for (const header of headers) {
      const value = row.headerIdToValue?.[header.id];
      if (cell(value) === null || isNotApplicable(value)) continue;
      properties.push({
        propertyId: header.id,
        name: header.name,
        valueType:
          typeof header.valueType === "string" ? header.valueType : "STRING",
        value
      });
    }
    return {
      index,
      level: index === "" ? 0 : index.split(".").length,
      partNumber: get(row, "Part number"),
      revision: get(row, "Revision"),
      name: get(row, "Name"),
      description: get(row, "Description"),
      quantity: Number.isFinite(quantity) && quantity > 0 ? quantity : 1,
      ...(shownAsUnit ? { shownAsUnit: true as const } : {}),
      ...(standardContent ? { standardContent: true as const } : {}),
      ...(properties.length > 0 ? { properties } : {}),
      itemSource: source
        ? {
            documentId: cell(source.documentId) ?? undefined,
            elementId: cell(source.elementId) ?? undefined,
            partId: cell(source.partId) ?? undefined,
            wvmType: cell(source.wvmType) ?? undefined,
            wvmId: cell(source.wvmId) ?? undefined,
            configuration: cell(source.configuration) ?? undefined
          }
        : null,
      children: []
    };
  });

  const byIndex = new Map<string, OnshapeBomNode>();
  const lines: OnshapeBomNode[] = [];
  let root: OnshapeBomNode | null = null;

  // Sort by depth so parents exist before children regardless of row order.
  for (const node of [...nodes].sort((a, b) => a.level - b.level)) {
    if (node.index === "" || node.index === "0") {
      root = node;
      continue;
    }
    byIndex.set(node.index, node);
    const dot = node.index.lastIndexOf(".");
    if (dot === -1) {
      lines.push(node);
    } else {
      const parent = byIndex.get(node.index.substring(0, dot));
      if (parent) {
        parent.children.push(node);
      } else {
        lines.push(node);
      }
    }
  }

  const sortRec = (list: OnshapeBomNode[]) => {
    list.sort((a, b) =>
      a.index.localeCompare(b.index, undefined, { numeric: true })
    );
    for (const item of list) sortRec(item.children);
  };
  sortRec(lines);

  return { root, lines, missingColumns };
}

export type AssemblyPartSource = {
  documentId: string;
  /** "v" for a part linked from a version; null for one in this document. */
  wvm: "v" | null;
  wvmId: string | null;
  elementId: string;
  partId: string;
};

/**
 * First non-Standard-content part of an assembly definition: its metadata
 * lists the part-scoped properties an assembly's metadata lacks.
 */
export function firstOwnAssemblyPart(
  definition: unknown
): AssemblyPartSource | null {
  const parts =
    definition && typeof definition === "object"
      ? (definition as { parts?: unknown }).parts
      : null;
  if (!Array.isArray(parts)) return null;
  for (const part of parts) {
    if (!part || typeof part !== "object") continue;
    const entry = part as Record<string, unknown>;
    if (entry.isStandardContent === true) continue;
    const { documentId, elementId, partId, documentVersion } = entry;
    if (
      typeof documentId !== "string" ||
      typeof elementId !== "string" ||
      typeof partId !== "string" ||
      documentId === "" ||
      elementId === "" ||
      partId === ""
    ) {
      continue;
    }
    const version =
      typeof documentVersion === "string" && documentVersion !== ""
        ? documentVersion
        : null;
    return {
      documentId,
      wvm: version ? "v" : null,
      wvmId: version,
      elementId,
      partId
    };
  }
  return null;
}

export function withoutProperties(nodes: OnshapeBomNode[]): OnshapeBomNode[] {
  return nodes.map(({ properties: _properties, children, ...node }) => ({
    ...node,
    children: withoutProperties(children)
  }));
}

export function missingBomColumnsMessage(missingColumns: string[]): string {
  const names = new Intl.ListFormat("en", {
    style: "long",
    type: "conjunction"
  }).format(missingColumns.map((name) => `"${name}"`));
  const plural = missingColumns.length > 1;
  return `Onshape's BOM for this assembly has no ${names} column${
    plural ? "s" : ""
  }. Add ${plural ? "them" : "it"} back to the assembly's BOM table in Onshape, then try again.`;
}

/** At most this many headers go into a diagnostics report. */
const DIAGNOSTIC_HEADER_LIMIT = 60;

/**
 * The shape of a BOM response and its column headers, for the panel's Details
 * when a BOM can't be read. Never a cell value.
 */
export type BomDiagnostics = {
  /** Top-level keys of the response. */
  payloadKeys: string[];
  /** "array", "missing", or what `headers` was instead. */
  headersType: string;
  headerCount: number;
  headers: {
    id: string | null;
    name: string | null;
    propertyName: string | null;
  }[];
  rowCount: number;
  formatVersion: string | null;
  templateId: string | null;
  topLevelAssemblyRow: boolean;
};

export function describeBomPayload(payload: unknown): BomDiagnostics {
  const bom =
    payload && typeof payload === "object"
      ? (payload as Record<string, unknown>)
      : {};
  const headers = bom.headers;
  const text = (value: unknown) =>
    typeof value === "string" || typeof value === "number"
      ? String(value)
      : null;
  const headerList = Array.isArray(headers) ? headers : [];
  return {
    payloadKeys: payload && typeof payload === "object" ? Object.keys(bom) : [],
    headersType: Array.isArray(headers)
      ? "array"
      : headers === undefined
        ? "missing"
        : headers === null
          ? "null"
          : typeof headers,
    headerCount: headerList.length,
    headers: headerList.slice(0, DIAGNOSTIC_HEADER_LIMIT).map((header) => {
      const h = (header ?? {}) as Record<string, unknown>;
      return {
        id: text(h.id),
        name: text(h.name),
        propertyName: text(h.propertyName)
      };
    }),
    rowCount: Array.isArray(bom.rows) ? bom.rows.length : 0,
    formatVersion: text(bom.formatVersion),
    templateId: text(bom.templateId),
    topLevelAssemblyRow: !!bom.topLevelAssemblyRow
  };
}

export function flattenBomTree(lines: OnshapeBomNode[]): OnshapeBomNode[] {
  const out: OnshapeBomNode[] = [];
  const walk = (list: OnshapeBomNode[]) => {
    for (const node of list) {
      out.push(node);
      walk(node.children);
    }
  };
  walk(lines);
  return out;
}

/** Non-empty string value of a named property in Onshape metadata. */
export function metadataProperty(
  payload: unknown,
  name: string
): string | null {
  const properties = (payload as { properties?: unknown })?.properties;
  if (!Array.isArray(properties)) return null;
  for (const property of properties) {
    const p = property as { name?: unknown; value?: unknown };
    if (p.name === name) {
      const value = p.value;
      if (typeof value === "string" && value.trim() !== "") return value;
      return null;
    }
  }
  return null;
}