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.

fix: external integration mappings in onshape

Gaurav Bhatt1db71a92a13f
diff --git a/apps/erp/app/routes/api+/integrations.onshape.sync.ts b/apps/erp/app/routes/api+/integrations.onshape.sync.ts
index 84af42a2c4..99da3ed587 100644
--- a/apps/erp/app/routes/api+/integrations.onshape.sync.ts
+++ b/apps/erp/app/routes/api+/integrations.onshape.sync.ts
@@ -54,6 +54,7 @@ export async function action({ request }: ActionFunctionArgs) {
});
if (sync.error) {
+ console.log("Failed to sync onshape data", sync.error);
return data(
{ success: false, message: "Failed to sync onshape data" },
{ status: 400 }
@@ -63,7 +64,7 @@ export async function action({ request }: ActionFunctionArgs) {
const itemId = record.data?.itemId as string;
// Upsert the OnShape mapping in externalIntegrationMapping
- await getCarbonServiceRole()
+ await serviceRole
.from("externalIntegrationMapping")
.delete()
.eq("entityType", "item")
diff --git a/packages/database/supabase/functions/sync/index.ts b/packages/database/supabase/functions/sync/index.ts
index d6c313daee..e80560d3f8 100644
--- a/packages/database/supabase/functions/sync/index.ts
+++ b/packages/database/supabase/functions/sync/index.ts
@@ -400,6 +400,8 @@ serve(async (req: Request) => {
const partId = readableId || name;
if (!partId) return;
+ const externalPartId = getReadableIdWithRevision(partId, revision);
+
const isMade = children.length > 0;
let itemId = id;
@@ -418,8 +420,14 @@ serve(async (req: Request) => {
await trx
.deleteFrom("externalIntegrationMapping")
.where("entityType", "=", "item")
- .where("entityId", "=", itemId)
.where("integration", "=", "onshapeData")
+ .where("companyId", "=", companyId)
+ .where((eb) =>
+ eb.or([
+ eb("entityId", "=", itemId),
+ eb("externalId", "=", externalPartId),
+ ])
+ )
.execute();
await trx
@@ -428,7 +436,7 @@ serve(async (req: Request) => {
entityType: "item",
entityId: itemId,
integration: "onshapeData",
- externalId: partId,
+ externalId: externalPartId,
metadata: data.data,
companyId,
allowDuplicateExternalId: false,
@@ -461,13 +469,22 @@ serve(async (req: Request) => {
// Create OnShape mapping for the new item
if (itemId) {
+ // Delete any stale mapping with the same externalId
+ await trx
+ .deleteFrom("externalIntegrationMapping")
+ .where("integration", "=", "onshapeData")
+ .where("externalId", "=", externalPartId)
+ .where("entityType", "=", "item")
+ .where("companyId", "=", companyId)
+ .execute();
+
await trx
.insertInto("externalIntegrationMapping")
.values({
entityType: "item",
entityId: itemId,
integration: "onshapeData",
- externalId: partId,
+ externalId: externalPartId,
metadata: data.data,
companyId,
allowDuplicateExternalId: false,