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: operation step description

Sidwebworks5af23c91a8b0
diff --git a/apps/erp/app/modules/shared/shared.models.ts b/apps/erp/app/modules/shared/shared.models.ts
index 13d43bbad3..7f5a520a82 100644
--- a/apps/erp/app/modules/shared/shared.models.ts
+++ b/apps/erp/app/modules/shared/shared.models.ts
@@ -1,4 +1,5 @@
import type { Database } from "@carbon/database";
+import { textToTiptap } from "@carbon/utils";
import { z } from "zod";
import { zfd } from "zod-form-data";
@@ -226,12 +227,18 @@ export const operationStepValidator = z
.string()
.min(1, { message: "Description is required" })
.transform((val) => {
+ let parsed: unknown;
try {
- return JSON.parse(val);
- // biome-ignore lint/correctness/noUnusedVariables: suppressed due to migration
+ parsed = JSON.parse(val);
+ // biome-ignore lint/correctness/noUnusedVariables: raw text is not JSON
} catch (e) {
- return {};
+ parsed = val;
}
+ // Always store a tiptap doc object, never a scalar string (jsonb scalar
+ // strings break method copies) and never silently drop content to {}.
+ if (typeof parsed === "string") return textToTiptap(parsed);
+ if (parsed && typeof parsed === "object") return parsed;
+ return textToTiptap(String(val));
}),
type: z.enum(procedureStepType, {
errorMap: () => ({ message: "Type is required" })
diff --git a/packages/database/supabase/functions/get-method/index.ts b/packages/database/supabase/functions/get-method/index.ts
index 5f5cb4b047..a324309847 100644
--- a/packages/database/supabase/functions/get-method/index.ts
+++ b/packages/database/supabase/functions/get-method/index.ts
@@ -32,6 +32,7 @@ import {
import { KyselyDatabase } from "../lib/postgres/index.ts";
import { importTypeScript } from "../lib/sandbox.ee.ts";
import { getStorageUnitId } from "../lib/storage-units.ts";
+import { toTiptapDoc } from "../shared/tiptap.ts";
import {
getNextRevisionSequence,
getNextSequence,
@@ -289,6 +290,7 @@ serve(async (req: Request) => {
.values(
methodOperationStep.map(({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId: operationId!,
companyId,
createdBy: userId,
@@ -758,6 +760,7 @@ serve(async (req: Request) => {
methodOperationStep.map(
async ({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
minValue: await getConfiguredValue({
id: operation.id,
@@ -1351,6 +1354,7 @@ serve(async (req: Request) => {
methodOperationStep.map(
({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
companyId,
createdBy: userId,
@@ -1967,6 +1971,7 @@ serve(async (req: Request) => {
methodOperationStep.map(
async ({ id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
minValue: await getConfiguredValue({
id: operation.id,
@@ -2477,6 +2482,7 @@ serve(async (req: Request) => {
methodOperationStep.map(
({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
companyId,
createdBy: userId,
@@ -2866,6 +2872,7 @@ serve(async (req: Request) => {
.values(
jobOperationStep.map(({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
companyId,
createdBy: userId,
@@ -3169,7 +3176,7 @@ serve(async (req: Request) => {
operationId,
name: step.name,
type: step.type,
- description: step.description,
+ description: toTiptapDoc(step.description),
required: step.required,
sortOrder: step.sortOrder,
unitOfMeasureCode: step.unitOfMeasureCode,
@@ -3357,6 +3364,7 @@ serve(async (req: Request) => {
.values(
methodOperationStep.map(({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId: operationId!,
companyId,
createdBy: userId,
@@ -3458,7 +3466,7 @@ serve(async (req: Request) => {
operationId: operationId,
name: attr.name,
type: attr.type,
- description: attr.description,
+ description: toTiptapDoc(attr.description),
minValue: attr.minValue,
maxValue: attr.maxValue,
companyId,
@@ -3788,6 +3796,7 @@ serve(async (req: Request) => {
.values(
quoteOperationStep.map(({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
companyId,
createdBy: userId,
@@ -4087,6 +4096,7 @@ serve(async (req: Request) => {
.values(
quoteOperationStep.map(({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
companyId,
createdBy: userId,
@@ -4515,6 +4525,7 @@ serve(async (req: Request) => {
.values(
quoteOperationStep.map(({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
companyId,
createdBy: userId,
@@ -4816,6 +4827,7 @@ serve(async (req: Request) => {
.values(
quoteOperationStep.map(({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
companyId,
createdBy: userId,
@@ -5296,6 +5308,7 @@ serve(async (req: Request) => {
.values(
quoteOperationStep.map(({ id: _id, ...attribute }) => ({
...attribute,
+ description: toTiptapDoc(attribute.description),
operationId,
companyId,
createdBy: userId,
@@ -5456,6 +5469,7 @@ async function insertProcedureDataForJobOperation(
} = attr;
return {
...rest,
+ description: toTiptapDoc(rest.description),
operationId,
companyId,
createdBy: userId,
diff --git a/packages/database/supabase/functions/shared/tiptap.ts b/packages/database/supabase/functions/shared/tiptap.ts
new file mode 100644
index 0000000000..b8c298ad14
--- /dev/null
+++ b/packages/database/supabase/functions/shared/tiptap.ts
@@ -0,0 +1,27 @@
+import type { Json } from "../lib/types.ts";
+
+// Mirrors `textToTiptap` from @carbon/utils. Kept local because edge functions run on
+// Deno and don't resolve the workspace import map.
+function textToTiptap(text: string): Json {
+ const lines = text.split("\n");
+ const content = lines.map((line) =>
+ // ProseMirror text nodes cannot be empty, so blank lines become a bare paragraph
+ line === ""
+ ? { type: "paragraph" }
+ : { type: "paragraph", content: [{ type: "text", text: line }] }
+ );
+ return { type: "doc", content };
+}
+
+/**
+ * Normalize an operation-step `description` into a valid tiptap doc (jsonb object).
+ *
+ * The Supabase client returns jsonb scalar strings as JS strings; re-inserting a JS
+ * string into a jsonb column makes node-pg send unquoted text and Postgres rejects it.
+ * Objects pass through, strings are wrapped into a doc, and null/empty become {}.
+ */
+export function toTiptapDoc(value: unknown): Json {
+ if (value && typeof value === "object") return value as Json;
+ if (typeof value === "string" && value.length > 0) return textToTiptap(value);
+ return {};
+}
diff --git a/packages/database/supabase/migrations/20260603230838_operation-step-description-to-tiptap.sql b/packages/database/supabase/migrations/20260603230838_operation-step-description-to-tiptap.sql
new file mode 100644
index 0000000000..640454e799
--- /dev/null
+++ b/packages/database/supabase/migrations/20260603230838_operation-step-description-to-tiptap.sql
@@ -0,0 +1,26 @@
+-- Some operation-step `description` values are stored as jsonb scalar strings
+-- (e.g. "Frame cube...\n...") instead of tiptap doc objects {"type":"doc","content":[...]}.
+-- The Supabase client returns those as JS strings and re-inserting them into a jsonb
+-- column breaks method copies (get-method edge function: invalid input syntax for json).
+-- Convert every scalar-string description into a tiptap doc, splitting on newlines into
+-- paragraphs (blank line -> bare paragraph, matching @carbon/utils textToTiptap).
+
+do $$
+declare
+ tbl text;
+begin
+ foreach tbl in array array['methodOperationStep', 'jobOperationStep', 'quoteOperationStep']
+ loop
+ execute format($fmt$
+ update %I s
+ set description = jsonb_build_object('type', 'doc', 'content', (
+ select coalesce(jsonb_agg(
+ case when line = '' then jsonb_build_object('type', 'paragraph')
+ else jsonb_build_object('type', 'paragraph', 'content',
+ jsonb_build_array(jsonb_build_object('type', 'text', 'text', line))) end
+ ), '[]'::jsonb)
+ from regexp_split_to_table(s.description #>> '{}', E'\n') as line))
+ where jsonb_typeof(to_jsonb(s.description)) = 'string'
+ $fmt$, tbl);
+ end loop;
+end $$;
diff --git a/packages/utils/src/tiptap.ts b/packages/utils/src/tiptap.ts
index 65e9bbab10..a281da6b07 100644
--- a/packages/utils/src/tiptap.ts
+++ b/packages/utils/src/tiptap.ts
@@ -32,10 +32,12 @@ export function parseMentionsFromDocument(content: JSONContent): string[] {
export const textToTiptap = (text: string) => {
const lines = text.split("\n");
- const content = lines.map((line) => ({
- type: "paragraph",
- content: [{ type: "text", text: line }]
- }));
+ const content = lines.map((line) =>
+ // ProseMirror text nodes cannot be empty, so blank lines become a bare paragraph
+ line === ""
+ ? { type: "paragraph" }
+ : { type: "paragraph", content: [{ type: "text", text: line }] }
+ );
return { type: "doc", content };
};