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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
type Item = {
id: string;
readableIdWithRevision: string;
};
/** `readableId.revision`, or the bare readableId for revision "0" / none. */
export const getReadableIdWithRevision = (
readableId: string,
revision?: string | null
) => {
if (revision && revision !== "0") {
return `${readableId}.${revision}`;
}
return readableId;
};
/**
* Get the readable ID for an item given its ID
* @param items - Array of items from useItems hook
* @param itemId - The item ID to look up
* @returns The readable ID with revision, or undefined if not found
*/
export function getItemReadableId(
items: Item[],
itemId?: string | null
): string | undefined {
if (!itemId) return undefined;
// Indexed loop — `.find` allocates a closure per call. Hot path: list
// rendering looks up readable ids per row.
const len = items.length;
for (let i = 0; i < len; i++) {
const item = items[i]!;
if (item.id === itemId) return item.readableIdWithRevision;
}
return undefined;
}
/**
* The secondary text shown with an item's readable id — its name, or a line's
* description — or undefined when it only repeats the readable id. A service's
* readable id IS its name, and a part's name is sometimes typed the same as its
* number; showing both reads as a stutter. Compares the text, not the item
* type, so an edited line description still shows.
*/
export function distinctItemText(
primary: string | null | undefined,
secondary: string | null | undefined
): string | undefined {
const text = secondary?.trim();
if (!text) return undefined;
return text === primary?.trim() ? undefined : secondary!;
}
/**
* Get an item by its ID
* @param items - Array of items from useItems hook
* @param itemId - The item ID to look up
* @returns The item, or undefined if not found
*/
export function getItemById<T extends Item>(
items: T[],
itemId: string
): T | undefined {
const len = items.length;
for (let i = 0; i < len; i++) {
const item = items[i]!;
if (item.id === itemId) return item;
}
return undefined;
}
// Build a separator-joined string from a fixed-arity record without
// allocating an intermediate `[…].filter().join()` array — saves two array
// allocations and a closure per call. Falsy parts (undefined, "") are
// dropped to match the prior `.filter((p) => !!p)` behaviour.
function joinTruthy(parts: (string | undefined)[], sep: string): string {
let out = "";
const len = parts.length;
for (let i = 0; i < len; i++) {
const part = parts[i];
if (!part) continue;
out = out.length === 0 ? part : out + sep + part;
}
return out;
}
export function getMaterialDescription(material: {
materialType?: string;
substance?: string;
grade?: string;
shape?: string;
dimensions?: string;
finish?: string;
}) {
return joinTruthy(
[
material.grade,
material.substance,
material.materialType,
material.shape,
material.dimensions,
material.finish
],
" "
);
}
export function getMaterialId(material: {
materialTypeCode?: string;
substanceCode?: string;
grade?: string;
shapeCode?: string;
dimensions?: string;
finish?: string;
}) {
return joinTruthy(
[
material.grade,
material.substanceCode,
material.materialTypeCode,
material.shapeCode,
material.dimensions,
material.finish
],
"-"
);
}