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.

feat: work instructions on method operation

Brad Barbin40626b11c3b7
diff --git a/apps/carbon/app/modules/items/items.models.ts b/apps/carbon/app/modules/items/items.models.ts
index 631405cd6c..4fbd189894 100644
--- a/apps/carbon/app/modules/items/items.models.ts
+++ b/apps/carbon/app/modules/items/items.models.ts
@@ -2,6 +2,17 @@ import { z } from "zod";
import { zfd } from "zod-form-data";
import { standardFactorType } from "../shared";
+export const defaultWorkInstruction = {
+ type: "doc",
+ content: [
+ {
+ type: "heading",
+ attrs: { level: 2 },
+ content: [{ type: "text", text: "Work Instructions" }],
+ },
+ { type: "paragraph" },
+ ],
+};
export const itemInventoryTypes = ["Inventory", "Non-Inventory"] as const;
export const partReplenishmentSystems = [
"Buy",
diff --git a/apps/carbon/app/modules/items/items.service.ts b/apps/carbon/app/modules/items/items.service.ts
index 4194079e96..fc9dff0c00 100644
--- a/apps/carbon/app/modules/items/items.service.ts
+++ b/apps/carbon/app/modules/items/items.service.ts
@@ -490,7 +490,7 @@ export async function getMethodOperations(
) {
return client
.from("methodOperation")
- .select("*")
+ .select("*, methodOperationWorkInstruction(content)")
.eq("makeMethodId", makeMethodId)
.order("order", { ascending: true });
}
diff --git a/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx b/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
index 1d179773a4..612844ae2e 100644
--- a/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
+++ b/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
@@ -13,6 +13,7 @@ import {
useDebounce,
} from "@carbon/react";
import { ValidatedForm } from "@carbon/remix-validated-form";
+import { getLocalTimeZone, today } from "@internationalized/date";
import { useFetcher } from "@remix-run/react";
import { AnimatePresence, LayoutGroup, motion } from "framer-motion";
import type { Dispatch, SetStateAction } from "react";
@@ -34,151 +35,21 @@ import {
} from "~/components/Form";
import type { Item, SortableItemRenderProps } from "~/components/SortableList";
import { SortableList, SortableListItem } from "~/components/SortableList";
+import { useUser } from "~/hooks";
import { useSupabase } from "~/lib/supabase";
import { path } from "~/utils/path";
import {
+ defaultWorkInstruction,
methodOperationOrders,
methodOperationValidator,
} from "../../items.models";
-export const workInstructions = {
- type: "doc",
- content: [
- {
- type: "heading",
- attrs: {
- level: 2,
- },
- content: [
- {
- type: "text",
- text: "Work Instruction",
- },
- ],
- },
- {
- type: "heading",
- attrs: {
- level: 3,
- },
- content: [
- {
- type: "text",
- text: "Setup ",
- },
- ],
- },
- {
- type: "paragraph",
- content: [
- {
- type: "text",
- text: "I'm baby letterpress franzen palo santo offal. XOXO lumbersexual farm-to-table mustache neutra selfies chillwave aesthetic green juice blue bottle letterpress fanny pack try-hard gorpcore. Selvage marfa butcher kale chips craft beer fashion axe lumbersexual mlkshk truffaut etsy same salvia activated charcoal kogi woke. Hoodie green juice put a bird on it, echo park swag disrupt ugh air plant vaporware vice hammock.",
- },
- ],
- },
- {
- type: "taskList",
- content: [
- {
- type: "taskItem",
- attrs: {
- checked: false,
- },
- content: [
- {
- type: "paragraph",
- content: [
- {
- type: "text",
-
- text: "Slow-carb fam same vexillologist bitters.",
- },
- ],
- },
- ],
- },
- {
- type: "taskItem",
- attrs: {
- checked: false,
- },
- content: [
- {
- type: "paragraph",
- content: [
- {
- type: "text",
-
- text: "Roof party franzen slow-carb heirloom viral small batch. ",
- },
- ],
- },
- ],
- },
- {
- type: "taskItem",
- attrs: {
- checked: false,
- },
- content: [
- {
- type: "paragraph",
- content: [
- {
- type: "text",
-
- text: "Bitters next level listicle, +1 same godard 90's big mood heirloom shabby chic hella.",
- },
- ],
- },
- ],
- },
- ],
- },
- {
- type: "heading",
- attrs: {
- level: 3,
- },
- content: [
- {
- type: "text",
- text: "Run",
- },
- ],
- },
- {
- type: "taskList",
- content: [
- {
- type: "taskItem",
- attrs: {
- checked: false,
- },
- content: [
- {
- type: "paragraph",
- content: [
- {
- type: "text",
-
- text: "DIY enamel pin viral ramps banjo DSA chartreuse.",
- },
- ],
- },
- ],
- },
- ],
- },
- {
- type: "paragraph",
- },
- ],
+type Operation = z.infer<typeof methodOperationValidator> & {
+ methodOperationWorkInstruction: {
+ content: JSONContent | null;
+ };
};
-type Operation = z.infer<typeof methodOperationValidator>;
-
type ItemWithData = Item & {
data: Operation;
};
@@ -210,11 +81,15 @@ const initialMethodOperation: Omit<Operation, "makeMethodId" | "order"> = {
productionStandard: 0,
standardFactor: "Hours/Piece",
operationOrder: "After Previous",
+ methodOperationWorkInstruction: {
+ content: defaultWorkInstruction,
+ },
};
const BillOfProcess = ({ makeMethodId, operations }: BillOfProcessProps) => {
const { supabase } = useSupabase();
const sortOrderFetcher = useFetcher();
+ const { id: userId } = useUser();
const [items, setItems] = useState<ItemWithData[]>(
makeItems(operations ?? [])
@@ -317,6 +192,32 @@ const BillOfProcess = ({ makeMethodId, operations }: BillOfProcessProps) => {
});
}, []);
+ const onUpdateWorkInstruction = useDebounce(async (content: JSONContent) => {
+ setItems((prevItems) =>
+ prevItems.map((item) =>
+ item.id === selectedItemId
+ ? {
+ ...item,
+ data: {
+ ...item.data,
+ methodOperationWorkInstruction: {
+ content,
+ },
+ },
+ }
+ : item
+ )
+ );
+ await supabase
+ ?.from("methodOperationWorkInstruction")
+ .update({
+ content,
+ updatedAt: today(getLocalTimeZone()).toString(),
+ updatedBy: userId,
+ })
+ .eq("methodOperationId", selectedItemId!);
+ }, 1000);
+
const [tabChangeRerender, setTabChangeRerender] = useState<number>(1);
const renderListItem = ({
item,
@@ -369,8 +270,11 @@ const BillOfProcess = ({ makeMethodId, operations }: BillOfProcessProps) => {
}}
>
<Editor
- initialValue={workInstructions as JSONContent}
- onChange={(value) => console.log(value)}
+ initialValue={
+ item.data.methodOperationWorkInstruction?.content ??
+ ({} as JSONContent)
+ }
+ onChange={onUpdateWorkInstruction}
/>
</motion.div>
</div>
@@ -623,7 +527,17 @@ function OperationForm({
onSubmit={(values) => {
setItems((prevItems) =>
prevItems.map((i) =>
- i.id === item.id ? { ...makeItem(values), id: item.id } : i
+ i.id === item.id
+ ? {
+ ...makeItem({
+ ...values,
+ methodOperationWorkInstruction: {
+ content: i.data.methodOperationWorkInstruction?.content,
+ },
+ }),
+ id: item.id,
+ }
+ : i
)
);
}}
diff --git a/apps/carbon/app/modules/users/users.server.ts b/apps/carbon/app/modules/users/users.server.ts
index cc0421e262..421e7b1e49 100644
--- a/apps/carbon/app/modules/users/users.server.ts
+++ b/apps/carbon/app/modules/users/users.server.ts
@@ -469,7 +469,6 @@ export async function getUserClaims(userId: string) {
} finally {
// if we don't have permissions from redis, get them from the database
if (!claims) {
- console.log("refetching claims from db");
// TODO: remove service role from here, and move it up a level
const rawClaims = await getClaims(getSupabaseServiceRole(), userId);
if (rawClaims.error || rawClaims.data === null) {
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 6255d1c2a5..25627f80e4 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -8506,6 +8506,114 @@ export type Database = {
},
]
}
+ methodOperationWorkInstruction: {
+ Row: {
+ companyId: string
+ content: Json | null
+ createdAt: string
+ createdBy: string
+ methodOperationId: string
+ updatedAt: string | null
+ updatedBy: string | null
+ }
+ Insert: {
+ companyId: string
+ content?: Json | null
+ createdAt?: string
+ createdBy: string
+ methodOperationId: string
+ updatedAt?: string | null
+ updatedBy?: string | null
+ }
+ Update: {
+ companyId?: string
+ content?: Json | null
+ createdAt?: string
+ createdBy?: string
+ methodOperationId?: string
+ updatedAt?: string | null
+ updatedBy?: string | null
+ }
+ Relationships: [
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_methodOperationId_fkey"
+ columns: ["methodOperationId"]
+ isOneToOne: true
+ referencedRelation: "methodOperation"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
note: {
Row: {
active: boolean
@@ -21638,14 +21746,14 @@ export type Database = {
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["supplierLocationId"]
+ columns: ["id"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["id"]
+ columns: ["supplierLocationId"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 6255d1c2a5..25627f80e4 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -8506,6 +8506,114 @@ export type Database = {
},
]
}
+ methodOperationWorkInstruction: {
+ Row: {
+ companyId: string
+ content: Json | null
+ createdAt: string
+ createdBy: string
+ methodOperationId: string
+ updatedAt: string | null
+ updatedBy: string | null
+ }
+ Insert: {
+ companyId: string
+ content?: Json | null
+ createdAt?: string
+ createdBy: string
+ methodOperationId: string
+ updatedAt?: string | null
+ updatedBy?: string | null
+ }
+ Update: {
+ companyId?: string
+ content?: Json | null
+ createdAt?: string
+ createdBy?: string
+ methodOperationId?: string
+ updatedAt?: string | null
+ updatedBy?: string | null
+ }
+ Relationships: [
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_methodOperationId_fkey"
+ columns: ["methodOperationId"]
+ isOneToOne: true
+ referencedRelation: "methodOperation"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "methodOperationWorkInstruction_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
note: {
Row: {
active: boolean
@@ -21638,14 +21746,14 @@ export type Database = {
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["supplierLocationId"]
+ columns: ["id"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["id"]
+ columns: ["supplierLocationId"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
diff --git a/packages/database/supabase/migrations/20240621114604_method-rls.sql b/packages/database/supabase/migrations/20240621114604_method-rls.sql
index 0e8d0e62e8..ca330d92f8 100644
--- a/packages/database/supabase/migrations/20240621114604_method-rls.sql
+++ b/packages/database/supabase/migrations/20240621114604_method-rls.sql
@@ -32,3 +32,33 @@ CREATE TRIGGER create_part_make_method_related_records
ALTER TABLE "methodOperation" ALTER COLUMN "description" SET NOT NULL;
+
+CREATE TABLE "methodOperationWorkInstruction" (
+ "methodOperationId" TEXT NOT NULL,
+ "content" JSON DEFAULT '{"type": "doc","content": [{"type": "heading","attrs": {"level": 2},"content": [{"type": "text","text": "Work Instructions"}]},{"type": "paragraph"}]}'::json,
+ "companyId" TEXT NOT NULL,
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
+ "createdBy" TEXT NOT NULL,
+ "updatedAt" TIMESTAMP WITH TIME ZONE,
+ "updatedBy" TEXT,
+
+ CONSTRAINT "methodOperationWorkInstruction_pkey" PRIMARY KEY ("methodOperationId"),
+ CONSTRAINT "methodOperationWorkInstruction_methodOperationId_fkey" FOREIGN KEY ("methodOperationId") REFERENCES "methodOperation" ("id") ON DELETE CASCADE,
+ CONSTRAINT "methodOperationWorkInstruction_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL,
+ CONSTRAINT "methodOperationWorkInstruction_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE RESTRICT
+);
+
+CREATE FUNCTION public.create_method_operation_work_instruction()
+RETURNS TRIGGER AS $$
+BEGIN
+ INSERT INTO public."methodOperationWorkInstruction"("methodOperationId", "createdBy", "companyId")
+ VALUES (new."id", new."createdBy", new."companyId");
+
+
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE TRIGGER create_part_make_method_related_records
+ AFTER INSERT on public."methodOperation"
+ FOR EACH ROW EXECUTE PROCEDURE public.create_method_operation_work_instruction();
\ No newline at end of file