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.

refactor: add better error handling to sales order lines to job function

Brad Barbinfa14125532a7
diff --git a/apps/erp/app/modules/production/production.service.ts b/apps/erp/app/modules/production/production.service.ts
index d227c1c340..b7bd22d2ed 100644
--- a/apps/erp/app/modules/production/production.service.ts
+++ b/apps/erp/app/modules/production/production.service.ts
@@ -85,6 +85,9 @@ export async function convertSalesOrderLinesToJobs(
const quoteId = opportunity.data?.quotes[0]?.id;
const salesOrderId = opportunity.data?.salesOrders[0]?.id;
+ const errors: string[] = [];
+ let jobsCreated = 0;
+
for await (const line of lines) {
if (line.methodType === "Make" && line.itemId) {
const itemManufacturing = await serviceRole
@@ -114,6 +117,7 @@ export async function convertSalesOrderLinesToJobs(
});
if (!nextSequence.data) {
+ errors.push(`Failed to get sequence for line ${line.itemReadableId}`);
continue;
}
@@ -155,11 +159,15 @@ export async function convertSalesOrderLinesToJobs(
jobId: nextSequence.data,
companyId,
createdBy: userId,
+ updatedBy: userId,
})
.select("id")
.single();
if (createJob.error) {
+ errors.push(
+ `Failed to create job for line ${line.itemReadableId}: ${createJob.error.message}`
+ );
continue;
}
@@ -178,6 +186,9 @@ export async function convertSalesOrderLinesToJobs(
);
if (upsertMethod.error) {
+ errors.push(
+ `Failed to create method for job ${nextSequence.data}: ${upsertMethod.error.message}`
+ );
continue;
}
} else {
@@ -195,6 +206,9 @@ export async function convertSalesOrderLinesToJobs(
);
if (upsertMethod.error) {
+ errors.push(
+ `Failed to create method for job ${nextSequence.data}: ${upsertMethod.error.message}`
+ );
continue;
}
}
@@ -216,10 +230,37 @@ export async function convertSalesOrderLinesToJobs(
userId,
},
});
+
+ jobsCreated++;
}
}
}
+ if (errors.length > 0) {
+ console.error(errors);
+ return {
+ data: null,
+ error: {
+ message: `Failed to create ${errors.length} job(s). ${errors.join(
+ "; "
+ )}`,
+ details: errors.join("; "),
+ code: "JOB_CREATION_ERROR",
+ } as PostgrestError,
+ };
+ }
+
+ if (jobsCreated === 0) {
+ return {
+ data: null,
+ error: {
+ message: "No jobs were created",
+ details: "No Make items found on sales order lines",
+ code: "NO_JOBS_CREATED",
+ } as PostgrestError,
+ };
+ }
+
return salesOrder;
}