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.

- fixed faulty logic

Gaurav Bhattd2d1d88cd860
diff --git a/packages/ee/src/paperless-parts/lib/lib.ts b/packages/ee/src/paperless-parts/lib/lib.ts
index fde8e133b9..aa2571e973 100644
--- a/packages/ee/src/paperless-parts/lib/lib.ts
+++ b/packages/ee/src/paperless-parts/lib/lib.ts
@@ -787,6 +787,43 @@ async function uploadModelFile(
const { file, companyId, itemId, salesOrderLineId, createdBy } = args;
try {
+ // If the item already has a model linked, reuse it for the line so item and
+ // line stay in sync — and so we don't overwrite the existing model's
+ // thumbnail with a freshly uploaded one that hasn't been processed yet.
+ const existingItem = await carbon
+ .from("item")
+ .select("modelUploadId")
+ .eq("id", itemId)
+ .single();
+
+ if (existingItem.error) {
+ console.error(
+ `Failed to read item ${itemId} before model upload:`,
+ existingItem.error
+ );
+ return false;
+ }
+
+ if (existingItem.data.modelUploadId) {
+ const lineUpdate = await carbon
+ .from("salesOrderLine")
+ .update({ modelUploadId: existingItem.data.modelUploadId })
+ .eq("id", salesOrderLineId);
+
+ if (lineUpdate.error) {
+ console.error(
+ `Failed to link existing model to sales order line:`,
+ lineUpdate.error
+ );
+ return false;
+ }
+
+ console.log(
+ `Item ${itemId} already has model ${existingItem.data.modelUploadId}; skipped uploading ${file.name} and linked line ${salesOrderLineId} to existing model`
+ );
+ return true;
+ }
+
const modelId = nanoid();
const fileExtension = file.name.split(".").pop();
const modelPath = `${companyId}/models/${modelId}.${fileExtension}`;
@@ -828,11 +865,14 @@ async function uploadModelFile(
return false;
}
- // Link model to sales order line
- const lineUpdate = await carbon
- .from("salesOrderLine")
- .update({ modelUploadId: modelId })
- .eq("id", salesOrderLineId);
+ // Link the new model to both the line and the item so they stay in sync.
+ const [lineUpdate, itemUpdate] = await Promise.all([
+ carbon
+ .from("salesOrderLine")
+ .update({ modelUploadId: modelId })
+ .eq("id", salesOrderLineId),
+ carbon.from("item").update({ modelUploadId: modelId }).eq("id", itemId)
+ ]);
if (lineUpdate.error) {
console.error(
@@ -842,23 +882,13 @@ async function uploadModelFile(
return false;
}
- // Only update item.modelUploadId if the item doesn't already have a model
- // linked — replacing it would orphan the old modelUpload and its thumbnail
- const existingItem = await carbon
- .from("item")
- .select("modelUploadId")
- .eq("id", itemId)
- .single();
-
- if (!existingItem.data?.modelUploadId) {
- await carbon
- .from("item")
- .update({ modelUploadId: modelId })
- .eq("id", itemId);
+ if (itemUpdate.error) {
+ console.error(`Failed to link model to item:`, itemUpdate.error);
+ return false;
}
console.log(
- `Successfully uploaded CAD model ${file.name} and linked to line ${salesOrderLineId}`
+ `Successfully uploaded CAD model ${file.name} and linked to line ${salesOrderLineId} and item ${itemId}`
);
return true;
} catch (error) {
@@ -975,13 +1005,14 @@ async function processSupportingFiles(
createdBy
});
- if (!uploadSuccess) {
+ if (uploadSuccess) {
+ hasModel = true;
+ } else {
console.error(
`Failed to upload CAD model: ${supportingFile.filename}`
);
}
} else {
- hasModel = true;
console.log(`Processing ${file.name} as document`);
// Upload as regular document
const uploadSuccess = await uploadFileToItem(carbon, {