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(search): one quote helper for PostgREST filter values, so the sync activity export escapes its cursor like every other list
aashu65b5f7575c6f
diff --git a/apps/erp/app/utils/cursor.ts b/apps/erp/app/utils/cursor.tsindex 95948ca0f1..46d9f4a5f2 100644--- a/apps/erp/app/utils/cursor.ts+++ b/apps/erp/app/utils/cursor.ts@@ -3,6 +3,7 @@// including ports, remain AGPLv3; serving them over a network requires releasing their source.import { getLogger } from "@carbon/logger";+import { quoteFilterValue } from "@carbon/utils";/*** Cursor (keyset) paging for list loaders.@@ -167,25 +168,12 @@ function isCursor(value: unknown): value is Cursor {);}-// A value inside a PostgREST logic tree (`or=(...)`): quoting keeps `,`, `(`,-// `)`, `.` and `:` from changing the filter's shape.-// `packages/ee/src/accounting/core/operations.ts` repeats this escape (ee-// cannot import the app).-function escapeQuoted(value: string) {- return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');-}--/** A value quoted for a PostgREST `or=(...)` filter. */-export function quoteFilterValue(value: string) {- return `"${escapeQuoted(value)}"`;-}-+// An array value is a Postgres array literal, itself quoted.function quote(value: Exclude<CursorValue, null>) {if (Array.isArray(value)) {- const literal = `{${value.map((v) => `"${escapeQuoted(v)}"`).join(",")}}`;- return `"${escapeQuoted(literal)}"`;+ return quoteFilterValue(`{${value.map(quoteFilterValue).join(",")}}`);}- return `"${escapeQuoted(String(value))}"`;+ return quoteFilterValue(String(value));}function equalTerm(column: string, value: CursorValue) {diff --git a/packages/ee/src/accounting/core/operations.ts b/packages/ee/src/accounting/core/operations.tsindex e41bbf7a30..6e4121c3ac 100644--- a/packages/ee/src/accounting/core/operations.ts+++ b/packages/ee/src/accounting/core/operations.ts@@ -3,7 +3,7 @@// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.import type { Database } from "@carbon/database";-import { isUniqueViolation } from "@carbon/utils";+import { isUniqueViolation, quoteFilterValue } from "@carbon/utils";import type { SupabaseClient } from "@supabase/supabase-js";import { SYNC_OPERATION_ALLOWED_TRANSITIONS } from "./models";import type {@@ -789,10 +789,16 @@ export async function transitionOperation(return { data: updated.data as SyncOperation, error: null };}-// A value quoted for a PostgREST `or=(...)` filter: the escape of-// `apps/erp/app/utils/cursor.ts` (`quoteFilterValue`), which ee cannot import.-function quoteFilterValue(value: string) {- return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;+/**+ * The rows after a cursor of the sync operation lists: newest first, `id`+ * ascending breaking `createdAt` ties, matching the index. Quoted: a+ * timestamp carries `.`, `:` and `+`, all reserved in an or-filter, and the+ * cursor comes from the URL.+ */+function afterSyncOperation(after: { createdAt: string; id: string }) {+ const createdAt = quoteFilterValue(after.createdAt);+ const id = quoteFilterValue(after.id);+ return `createdAt.lt.${createdAt},and(createdAt.eq.${createdAt},id.gt.${id})`;}/**@@ -842,16 +848,7 @@ export async function getSyncOperations(query = query.eq("entityType", args.entityType);}- if (args.after) {- // Quoted: a timestamp carries `.`, `:` and `+`, all reserved in an- // or-filter. The cursor comes from the URL, so a `"` or `\` in it is- // escaped too.- const createdAt = quoteFilterValue(args.after.createdAt);- const id = quoteFilterValue(args.after.id);- query = query.or(- `createdAt.lt.${createdAt},and(createdAt.eq.${createdAt},id.gt.${id})`- );- }+ if (args.after) query = query.or(afterSyncOperation(args.after));// `id` ascending breaks createdAt ties, so every row has one place.const result = await query@@ -925,14 +922,7 @@ export async function getSyncOperationsExportPage(: query.eq("status", args.status);}- if (args.after) {- // Quoted: a timestamp carries `.`, `:` and `+`, all reserved in an- // or-filter. `id` ascending breaks createdAt ties, matching the index.- const { createdAt, id } = args.after;- query = query.or(- `createdAt.lt."${createdAt}",and(createdAt.eq."${createdAt}",id.gt."${id}")`- );- }+ if (args.after) query = query.or(afterSyncOperation(args.after));const result = await query.order("createdAt", { ascending: false })diff --git a/packages/utils/src/supabase.ts b/packages/utils/src/supabase.tsindex 3ae7471d12..3265dfbd85 100644--- a/packages/utils/src/supabase.ts+++ b/packages/utils/src/supabase.ts@@ -26,10 +26,19 @@ export function sanitize<T extends Record<string, any>>(* the filter early and the request failed.*/export function searchFilter(columns: string[], term: string): string {- const quoted = `"%${escapeLike(term).replace(/["\\]/g, (char) => `\\${char}`)}%"`;+ const quoted = quoteFilterValue(`%${escapeLike(term)}%`);return columns.map((column) => `${column}.ilike.${quoted}`).join(",");}+/**+ * A value quoted for a PostgREST logic tree (`or=(...)`, `and(...)`): the+ * quotes keep `,`, `(`, `)`, `.` and `:` from changing the filter's shape,+ * and a `"` or `\` inside is escaped.+ */+export function quoteFilterValue(value: string): string {+ return `"${value.replace(/["\\]/g, (char) => `\\${char}`)}"`;+}+/** `term` for a LIKE / ILIKE pattern: `\\`, `%` and `_` match themselves. */export function escapeLike(term: string): string {return term.replace(/[\\%_]/g, (char) => `\\${char}`);