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
fix(sales): a failed price list read shows Retry and fails the export instead of ending the list early
aashub39997247248
diff --git a/apps/erp/app/modules/sales/sales.server.ts b/apps/erp/app/modules/sales/sales.server.tsindex d0c9a702d7..b7cfbf82be 100644--- a/apps/erp/app/modules/sales/sales.server.ts+++ b/apps/erp/app/modules/sales/sales.server.ts@@ -6946,11 +6946,14 @@ export const resolvePriceList = defineServiceFn({}),permission: { update: "sales" },async run(ctx, input) {- return await service.resolvePriceList(+ const { error, ...result } = await service.resolvePriceList(ctx.asCaller.supabase,ctx.company.id,payload(ctx, input).build() as never);+ if (error)+ return ctx.error("internal", "Failed to load the price list", error);+ return result;}});@@ -6971,11 +6974,14 @@ export const getBaseCatalog = defineServiceFn({}),permission: { view: "sales" },async run(ctx, input) {- return await service.getBaseCatalog(+ const { error, ...result } = await service.getBaseCatalog(ctx.asCaller.supabase,ctx.company.id,payload(ctx, input).build() as never);+ if (error)+ return ctx.error("internal", "Failed to load the catalog", error);+ return result;}});diff --git a/apps/erp/app/modules/sales/sales.service.ts b/apps/erp/app/modules/sales/sales.service.tsindex 19ab7429de..bc0b19ea9c 100644--- a/apps/erp/app/modules/sales/sales.service.ts+++ b/apps/erp/app/modules/sales/sales.service.ts@@ -2476,28 +2476,29 @@ async function resolvePostingGroupFilter(): Promise<{itemIds: string[] | null;filters: GenericQueryFilters["filters"];+ error: PostgrestError | null;}> {if (!filters || filters.length === 0) {- return { itemIds: null, filters };+ return { itemIds: null, filters, error: null };}const postingGroupFilters = filters.filter((f): f is { column: string; operator: string; value: string } =>f.column === "itemPostingGroupId" && Boolean(f.value));if (postingGroupFilters.length === 0) {- return { itemIds: null, filters };+ return { itemIds: null, filters, error: null };}const remaining = filters.filter((f) => f.column !== "itemPostingGroupId");const groupIds = postingGroupFilters.flatMap((f) =>f.operator === "in" ? f.value.split(",") : [f.value]);- const { data } = await client+ const { data, error } = await client.from("itemCost").select("itemId").eq("companyId", companyId).in("itemPostingGroupId", groupIds);const itemIds = (data ?? []).map((r) => r.itemId);- return { itemIds, filters: remaining };+ return { itemIds, filters: remaining, error };}// The price list (`resolvePriceList`, `getBaseCatalog`) has no default order:@@ -2535,13 +2536,14 @@ export async function resolvePriceList(} else if (args.customerTypeId) {scopeQuery = scopeQuery.eq("customerTypeId", args.customerTypeId);} else {- return { data: [], count: 0 };+ return { data: [], count: 0, error: null };}- const { data: scopedOverrides } = await scopeQuery;+ const { data: scopedOverrides, error: scopeError } = await scopeQuery;+ if (scopeError) return { data: [], count: 0, error: scopeError };const overriddenItemIds = (scopedOverrides ?? []).map((r) => r.itemId);if (overriddenItemIds.length === 0) {- return { data: [], count: 0 };+ return { data: [], count: 0, error: null };}let itemQuery = client@@ -2557,11 +2559,17 @@ export async function resolvePriceList(itemQuery = itemQuery.or(searchFilter(["name", "readableId"], args.search));}- const { itemIds: postingGroupItemIds, filters: filtersWithoutPostingGroup } =- await resolvePostingGroupFilter(client, companyId, args.filters);+ const {+ itemIds: postingGroupItemIds,+ filters: filtersWithoutPostingGroup,+ error: postingGroupError+ } = await resolvePostingGroupFilter(client, companyId, args.filters);+ if (postingGroupError) {+ return { data: [], count: 0, error: postingGroupError };+ }if (postingGroupItemIds !== null) {if (postingGroupItemIds.length === 0) {- return { data: [], count: 0 };+ return { data: [], count: 0, error: null };}itemQuery = itemQuery.in("id", postingGroupItemIds);}@@ -2572,20 +2580,22 @@ export async function resolvePriceList(PRICE_LIST_PAGING.defaultSorts);- const { data: items, count } = await itemQuery;+ const { data: items, count, error: itemsError } = await itemQuery;+ if (itemsError) return { data: [], count: 0, error: itemsError };if (!items || items.length === 0) {- return { data: [], count: count ?? 0 };+ return { data: [], count: count ?? 0, error: null };}const itemIds = items.map((i) => i.id);let resolvedCustomerTypeId = args.customerTypeId ?? null;if (args.customerId && !resolvedCustomerTypeId) {- const { data: cust } = await client+ const { data: cust, error: customerError } = await client.from("customer").select("customerTypeId").eq("id", args.customerId).maybeSingle();+ if (customerError) return { data: [], count: 0, error: customerError };resolvedCustomerTypeId = cust?.customerTypeId ?? null;}@@ -2618,28 +2628,30 @@ export async function resolvePriceList(const allOverrideMap = new Map<string, OverrideEntry>();if (args.customerId) {- const { data: rows } = await client+ const { data: rows, error } = await client.from("customerItemPriceOverride").select(overrideSelect).eq("companyId", companyId).eq("customerId", args.customerId).eq("active", true).in("itemId", itemIds);+ if (error) return { data: [], count: 0, error };fillMap(rows as unknown as ParentRow[] | null, overrideMap);}if (resolvedCustomerTypeId) {- const { data: rows } = await client+ const { data: rows, error } = await client.from("customerItemPriceOverride").select(overrideSelect).eq("companyId", companyId).eq("customerTypeId", resolvedCustomerTypeId).eq("active", true).in("itemId", itemIds);+ if (error) return { data: [], count: 0, error };fillMap(rows as unknown as ParentRow[] | null, typeOverrideMap);}- const { data: allRows } = await client+ const { data: allRows, error: allRowsError } = await client.from("customerItemPriceOverride").select(overrideSelect).eq("companyId", companyId)@@ -2647,6 +2659,7 @@ export async function resolvePriceList(.is("customerTypeId", null).eq("active", true).in("itemId", itemIds);+ if (allRowsError) return { data: [], count: 0, error: allRowsError };fillMap(allRows as unknown as ParentRow[] | null, allOverrideMap);let rulesQuery = client@@ -2658,7 +2671,8 @@ export async function resolvePriceList(rulesQuery = rulesQuery.or(`validFrom.is.null,validFrom.lte.${date}`);rulesQuery = rulesQuery.or(`validTo.is.null,validTo.gte.${date}`);- const { data: allRules } = await rulesQuery;+ const { data: allRules, error: rulesError } = await rulesQuery;+ if (rulesError) return { data: [], count: 0, error: rulesError };const rows: PriceListRow[] = items.map((item) => {const salePriceRow = Array.isArray(item.itemUnitSalePrice)@@ -2820,7 +2834,8 @@ export async function resolvePriceList(return {data: rows,- count: count ?? 0+ count: count ?? 0,+ error: null};}@@ -2842,11 +2857,17 @@ export async function getBaseCatalog(query = query.or(searchFilter(["name", "readableId"], args.search));}- const { itemIds: postingGroupItemIds, filters: filtersWithoutPostingGroup } =- await resolvePostingGroupFilter(client, companyId, args.filters);+ const {+ itemIds: postingGroupItemIds,+ filters: filtersWithoutPostingGroup,+ error: postingGroupError+ } = await resolvePostingGroupFilter(client, companyId, args.filters);+ if (postingGroupError) {+ return { data: [], count: 0, error: postingGroupError };+ }if (postingGroupItemIds !== null) {if (postingGroupItemIds.length === 0) {- return { data: [], count: 0 };+ return { data: [], count: 0, error: null };}query = query.in("id", postingGroupItemIds);}@@ -2857,9 +2878,10 @@ export async function getBaseCatalog(PRICE_LIST_PAGING.defaultSorts);- const { data: items, count } = await query;+ const { data: items, count, error: itemsError } = await query;+ if (itemsError) return { data: [], count: 0, error: itemsError };if (!items || items.length === 0) {- return { data: [], count: count ?? 0 };+ return { data: [], count: count ?? 0, error: null };}const rows: PriceListRow[] = items.map((item) => {@@ -2889,7 +2911,7 @@ export async function getBaseCatalog(};});- return { data: rows, count: count ?? 0 };+ return { data: rows, count: count ?? 0, error: null };}export async function upsertCustomer(diff --git a/apps/erp/app/modules/sales/types.ts b/apps/erp/app/modules/sales/types.tsindex 2eb7088d8e..5e97f6ac33 100644--- a/apps/erp/app/modules/sales/types.ts+++ b/apps/erp/app/modules/sales/types.ts@@ -3,6 +3,7 @@// including ports, remain AGPLv3; serving them over a network requires releasing their source.import type { Database } from "@carbon/database";+import type { PostgrestError } from "@supabase/supabase-js";import type { z } from "zod";import type { jobStatus } from "../production/production.models";import type { QuantityEffect } from "../shared/types";@@ -95,6 +96,8 @@ export type PriceListRow = {export type PriceListResult = {data: PriceListRow[];count: number;+ /** A failed read; the rows are then incomplete and must not be shown as the list. */+ error: PostgrestError | null;};export type PriceResolutionInput = {diff --git a/apps/erp/app/utils/list-registry/sales.server.ts b/apps/erp/app/utils/list-registry/sales.server.tsindex ab1467d331..a006ec7809 100644--- a/apps/erp/app/utils/list-registry/sales.server.ts+++ b/apps/erp/app/utils/list-registry/sales.server.ts@@ -129,7 +129,7 @@ export const priceListList = {filters});- return { ...result, error: null };+ return result;}} satisfies ListDefinition;