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): normalize quantity when preserving quote line price fields
sidgaikwad82ab7b4d8b10
diff --git a/.ai/lessons.md b/.ai/lessons.mdindex 3e9fa38a96..8a1b447c6d 100644--- a/.ai/lessons.md+++ b/.ai/lessons.md@@ -703,3 +703,13 @@ Format: `Context → Problem → Rule → Applies to`**Rule:** Never commit `packages/database/src/types.ts` (or `supabase/functions/lib/types.ts`) generated after a `crbn restore` — regenerate against a migration-built database first. `crbn restore` now warns about this. Stage generated types explicitly rather than with `git add -A`, and diff them before committing: any relation appearing that no migration defines is drift from the source environment, not a schema change. Separately, `SELECT … INTO <name>` in a SQL (non-plpgsql) context silently creates a table — a real hazard when copying plpgsql bodies into migrations.**Applies to:** `packages/dev/src/commands/restore.ts`, `packages/database/src/types.ts`, `packages/database/supabase/functions/lib/types.ts`.++## Kysely returns NUMERIC as a string; supabase-js returns it as a number++**Context:** `upsertQuoteLinePrices` was converted from supabase-js to a Kysely transaction so its delete + reinsert would roll back as a unit. The conversion typechecked, and the shipped result silently stopped preserving `shippingCost`, `discountPercent` and `leadTime` on every markup change.++**Problem:** The function keys the snapshot of existing rows by quantity and looks each one up while building the reinsert. Through PostgREST a `NUMERIC` column arrives as a JS `number`, so `pricesByQuantity[10]` matched. Through node-postgres — which Kysely uses — the same column arrives as the string `"10.00000"`, because pg does not parse `NUMERIC` (oid 1700) to float and this repo sets no `setTypeParser` anywhere. Every `Map.get(10)` therefore missed, `existing` was always `undefined`, and each preserved field fell back to the caller's value: shipping to the column default `0`, discount and lead time to the zeros the recalculate route passes. The generated types say `number` on both paths, so `tsgo` cannot see it — the mismatch exists only at runtime.++**Rule:** When porting a query from supabase-js to Kysely, treat every `NUMERIC`/`DECIMAL`/`BIGINT` read as a **string** regardless of what the generated type claims. Normalize with `Number(...)` before using such a value as an object key, `Map` key, or in `===`/arithmetic. Writing them back is safe — pg accepts numeric strings for numeric params, and passing the string through preserves precision. More generally: a client swap can change runtime value types without changing a single TypeScript type, so a typecheck is not evidence that a port behaves identically — exercise it against a real database.++**Applies to:** `apps/erp/app/modules/sales/sales.service.ts` (`upsertQuoteLinePrices`), any `Kysely<KyselyDatabase>` service in `apps/erp/app/modules/**` or `packages/database/supabase/functions/**`, and the `getPostgresClient` pool in `packages/database/supabase/functions/lib/postgres/index.ts`.diff --git a/apps/erp/app/modules/sales/sales.service.ts b/apps/erp/app/modules/sales/sales.service.tsindex 39bd9de239..535efd2c22 100644--- a/apps/erp/app/modules/sales/sales.service.ts+++ b/apps/erp/app/modules/sales/sales.service.ts@@ -3918,15 +3918,18 @@ export async function upsertQuoteLinePrices(.where("companyId", "=", companyId).executeTakeFirst();+ // node-postgres returns NUMERIC as a string ("10.00000"), where PostgREST+ // returned it as a number — so the quantity has to be normalized on both+ // sides or every lookup misses and nothing gets preserved.const existingByQuantity = new Map(- existingPrices.map((price) => [price.quantity, price])+ existingPrices.map((price) => [Number(price.quantity), price]));await trx.insertInto("quoteLinePrice").values(quoteLinePrices.map((p) => {- const existing = existingByQuantity.get(p.quantity);+ const existing = existingByQuantity.get(Number(p.quantity));return {...p,