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(tables): background CSV export reads the Jobs, Suppliers and Workflows lookups 100 ids at a time and fails instead of leaving columns blank

aashu3ecb653e6827
diff --git a/apps/erp/app/modules/purchasing/purchasing.service.ts b/apps/erp/app/modules/purchasing/purchasing.service.ts
index 35db151db0..d445ee79b7 100644
--- a/apps/erp/app/modules/purchasing/purchasing.service.ts
+++ b/apps/erp/app/modules/purchasing/purchasing.service.ts
@@ -1094,31 +1094,40 @@ export async function getSupplierReportContacts(
supplierIds: string[]
) {
return Promise.all([
- client
- .from("supplier")
- .select(
- `id, purchasingContact:supplierContact!supplier_purchasingContactId_fkey(contact(fullName, email, workPhone))`
- )
- .eq("companyId", companyId)
- .in("id", supplierIds),
- client
- .from("supplierPayment")
- .select(
- `supplierId,
+ fetchAllByIds(supplierIds, (batch) =>
+ client
+ .from("supplier")
+ .select(
+ `id, purchasingContact:supplierContact!supplier_purchasingContactId_fkey(contact(fullName, email, workPhone))`
+ )
+ .eq("companyId", companyId)
+ .in("id", batch)
+ .order("id", { ascending: true })
+ ),
+ fetchAllByIds(supplierIds, (batch) =>
+ client
+ .from("supplierPayment")
+ .select(
+ `supplierId,
invoiceContact:supplierContact!supplierPayment_invoiceSupplierContactId_fkey(contact(fullName, email, workPhone)),
invoiceLocation:supplierLocation!supplierPayment_invoiceSupplierLocationId_fkey(address(addressLine1, addressLine2, city, stateProvince, postalCode, country(name)))`
- )
- .eq("companyId", companyId)
- .in("supplierId", supplierIds),
- client
- .from("supplierShipping")
- .select(
- `supplierId,
+ )
+ .eq("companyId", companyId)
+ .in("supplierId", batch)
+ .order("supplierId", { ascending: true })
+ ),
+ fetchAllByIds(supplierIds, (batch) =>
+ client
+ .from("supplierShipping")
+ .select(
+ `supplierId,
shippingContact:supplierContact!supplierShipping_shippingSupplierContactId_fkey(contact(fullName, email, workPhone)),
shippingLocation:supplierLocation!supplierShipping_shippingSupplierLocationId_fkey(address(addressLine1, addressLine2, city, stateProvince, postalCode, country(name)))`
- )
- .eq("companyId", companyId)
- .in("supplierId", supplierIds)
+ )
+ .eq("companyId", companyId)
+ .in("supplierId", batch)
+ .order("supplierId", { ascending: true })
+ )
]);
}
diff --git a/apps/erp/app/modules/workflows/workflows.service.ts b/apps/erp/app/modules/workflows/workflows.service.ts
index 2d7cfcbb4b..3a0249eb1d 100644
--- a/apps/erp/app/modules/workflows/workflows.service.ts
+++ b/apps/erp/app/modules/workflows/workflows.service.ts
@@ -3,6 +3,7 @@
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import type { Database, Json } from "@carbon/database";
+import { fetchAllByIds } from "@carbon/database";
import { fkDisplayRegistry } from "@carbon/database/audit.config";
import { datetime } from "@carbon/utils";
import type { SupabaseClient } from "@supabase/supabase-js";
@@ -98,11 +99,14 @@ export async function getWorkflowVersionNumbers(
versionIds: string[],
companyId: string
) {
- return client
- .from("workflowVersion")
- .select("id, versionNumber")
- .in("id", versionIds)
- .eq("companyId", companyId);
+ return fetchAllByIds(versionIds, (batch) =>
+ client
+ .from("workflowVersion")
+ .select("id, versionNumber")
+ .in("id", batch)
+ .eq("companyId", companyId)
+ .order("id", { ascending: true })
+ );
}
export async function getWorkflowVersion(
@@ -380,12 +384,14 @@ export async function getWorkflowLastRuns(
workflowIds: string[],
companyId: string
) {
- if (workflowIds.length === 0) return { data: [], error: null };
- return client
- .from("workflowLastRun")
- .select("workflowId, runId, status, createdAt, completedAt, durationMs")
- .in("workflowId", workflowIds)
- .eq("companyId", companyId);
+ return fetchAllByIds(workflowIds, (batch) =>
+ client
+ .from("workflowLastRun")
+ .select("workflowId, runId, status, createdAt, completedAt, durationMs")
+ .in("workflowId", batch)
+ .eq("companyId", companyId)
+ .order("workflowId", { ascending: true })
+ );
}
/** Tables in `fkDisplayRegistry` that have no `companyId` column; the resolver
diff --git a/apps/erp/app/utils/list-registry/production.server.ts b/apps/erp/app/utils/list-registry/production.server.ts
index 1fead5999b..2f626e69c8 100644
--- a/apps/erp/app/utils/list-registry/production.server.ts
+++ b/apps/erp/app/utils/list-registry/production.server.ts
@@ -2,6 +2,7 @@
// 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.
+import { fetchAllByIds } from "@carbon/database";
import {
ASSEMBLY_INSTRUCTIONS_PAGING,
getAssemblyInstructionsPage,
@@ -34,22 +35,28 @@ export async function withJobListExtras<
const [batchedOps, trackedEntities] = await Promise.all([
// The jobs view carries no batch column (batching is per operation),
// so one lookup finds the live (Active/Completing) batches of each job.
- jobIds.length > 0
- ? client
- .from("jobOperation")
- .select("jobId, jobOperationBatch(id, readableId, status)")
- .in("jobId", jobIds)
- .eq("companyId", companyId)
- .not("jobOperationBatchId", "is", null)
- : { data: [] },
- makeMethodIds.length > 0
- ? client
- .from("trackedEntity")
- .select("readableId, attributes")
- .in("attributes->>Job Make Method", makeMethodIds)
- .eq("companyId", companyId)
- : { data: [] }
+ fetchAllByIds(jobIds, (batch) =>
+ client
+ .from("jobOperation")
+ .select("jobId, jobOperationBatch(id, readableId, status)")
+ .in("jobId", batch)
+ .eq("companyId", companyId)
+ .not("jobOperationBatchId", "is", null)
+ .order("id", { ascending: true })
+ ),
+ fetchAllByIds(makeMethodIds, (batch) =>
+ client
+ .from("trackedEntity")
+ .select("readableId, attributes")
+ .in("attributes->>Job Make Method", batch)
+ .eq("companyId", companyId)
+ .order("id", { ascending: true })
+ )
]);
+ // A failed lookup fails the page: a blank Batches or Tracking column would
+ // read as "none".
+ if (batchedOps.error) throw batchedOps.error;
+ if (trackedEntities.error) throw trackedEntities.error;
const batchesByJobId: Record<string, string[]> = {};
for (const op of batchedOps.data ?? []) {
diff --git a/apps/erp/app/utils/list-registry/purchasing.server.ts b/apps/erp/app/utils/list-registry/purchasing.server.ts
index 714a88fca5..16ac626e30 100644
--- a/apps/erp/app/utils/list-registry/purchasing.server.ts
+++ b/apps/erp/app/utils/list-registry/purchasing.server.ts
@@ -85,6 +85,11 @@ export async function withSupplierReportContacts<
const supplierIds = pluckUnique(suppliers, (s) => s.id);
const [purchasingRes, paymentRes, shippingRes] =
await getSupplierReportContacts(client, companyId, supplierIds);
+ // A failed lookup fails the page: a blank contact column would read as
+ // "none".
+ for (const result of [purchasingRes, paymentRes, shippingRes]) {
+ if (result.error) throw result.error;
+ }
const purchasingBySupplierId = indexByMapped(
purchasingRes.data,
diff --git a/apps/erp/app/utils/list-registry/workflows.server.ts b/apps/erp/app/utils/list-registry/workflows.server.ts
index b0e37b3ce6..e833204003 100644
--- a/apps/erp/app/utils/list-registry/workflows.server.ts
+++ b/apps/erp/app/utils/list-registry/workflows.server.ts
@@ -36,6 +36,10 @@ export async function withWorkflowListExtras<
? getWorkflowLastRuns(client, workflowIds, companyId)
: null
]);
+ // A failed lookup fails the page: a blank Version or Last Run column would
+ // read as "none".
+ if (versions?.error) throw versions.error;
+ if (runs?.error) throw runs.error;
const versionNumbers = new Map(
(versions?.data ?? []).map((version) => [version.id, version.versionNumber])