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: add model to files in mes

Brad Barbin1aa885773b17
diff --git a/apps/erp/app/components/Documents.tsx b/apps/erp/app/components/Documents.tsx
index e1466f0142..34e0afa089 100644
--- a/apps/erp/app/components/Documents.tsx
+++ b/apps/erp/app/components/Documents.tsx
@@ -112,6 +112,38 @@ const Documents = ({
[getReadPath, carbon?.storage, revalidator]
);
+ const downloadModel = useCallback(
+ async (model: ModelUpload) => {
+ if (!model.modelPath || !model.modelName) {
+ toast.error("Model data is missing");
+ return;
+ }
+
+ const result = await carbon?.storage
+ .from("private")
+ .download(model.modelPath);
+
+ if (!result || result.error) {
+ toast.error(result?.error?.message || "Error downloading file");
+ return;
+ }
+
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ const url = window.URL.createObjectURL(result.data);
+ a.href = url;
+ a.download = model.modelName;
+ a.click();
+
+ setTimeout(() => {
+ window.URL.revokeObjectURL(url);
+ document.body.removeChild(a);
+ }, 0);
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ []
+ );
+
const deleteModel = useCallback(async () => {
if (!carbon) return;
@@ -284,6 +316,11 @@ const Documents = ({
/>
</DropdownMenuTrigger>
<DropdownMenuContent>
+ <DropdownMenuItem
+ onClick={() => downloadModel(modelUpload)}
+ >
+ Download
+ </DropdownMenuItem>
<DropdownMenuItem asChild>
<Link
to={
diff --git a/apps/mes/app/components/JobOperation.tsx b/apps/mes/app/components/JobOperation.tsx
index f31caf24d3..aae986c8ef 100644
--- a/apps/mes/app/components/JobOperation.tsx
+++ b/apps/mes/app/components/JobOperation.tsx
@@ -124,6 +124,7 @@ import { FaTasks } from "react-icons/fa";
import { FaCheck, FaPause, FaPlay, FaPlus, FaTrash } from "react-icons/fa6";
import {
LuAlertTriangle,
+ LuAxis3D,
LuChevronLeft,
LuClipboardCheck,
LuDownload,
@@ -160,7 +161,7 @@ export const JobOperation = ({
}: JobOperationProps) => {
const navigate = useNavigate();
- const { downloadFile, getFilePath } = useFiles(job);
+ const { downloadFile, downloadModel, getFilePath } = useFiles(job);
const {
active,
@@ -199,6 +200,16 @@ export const JobOperation = ({
const mode = useMode();
const { operationId } = useParams();
+ const modelUpload =
+ job.modelPath || operation.itemModelPath
+ ? {
+ modelPath: operation.itemModelPath ?? job.modelPath,
+ modelId: operation.itemModelId ?? job.modelId,
+ modelName: operation.itemModelName ?? job.modelName,
+ modelSize: operation.itemModelSize ?? job.modelSize,
+ }
+ : null;
+
return (
<>
<Tabs
@@ -530,7 +541,7 @@ export const JobOperation = ({
</Tr>
</Thead>
<Tbody>
- {resolvedFiles.length === 0 ? (
+ {resolvedFiles.length === 0 && !modelUpload ? (
<Tr>
<Td
colSpan={24}
@@ -540,35 +551,23 @@ export const JobOperation = ({
</Td>
</Tr>
) : (
- resolvedFiles.map((file) => {
- const type = getFileType(file.name);
- return (
- <Tr key={file.id}>
+ <>
+ {modelUpload?.modelName && (
+ <Tr>
<Td>
<HStack>
- <FileIcon type={type} />
- <span className="font-medium">
- {["PDF", "Image"].includes(type) ? (
- <FilePreview
- bucket="private"
- pathToFile={getFilePath(file)}
- // @ts-ignore
- type={getFileType(file.name)}
- >
- {file.name}
- </FilePreview>
- ) : (
- file.name
- )}
- </span>
+ <LuAxis3D className="text-emerald-500 w-6 h-6" />
+ <span>{modelUpload.modelName}</span>
</HStack>
</Td>
<Td className="text-xs font-mono">
- {convertKbToString(
- Math.floor(
- (file.metadata?.size ?? 0) / 1024
- )
- )}
+ {modelUpload.modelSize
+ ? convertKbToString(
+ Math.floor(
+ (modelUpload.modelSize ?? 0) / 1024
+ )
+ )
+ : "--"}
</Td>
<Td>
<div className="flex justify-end w-full">
@@ -580,9 +579,11 @@ export const JobOperation = ({
variant="secondary"
/>
</DropdownMenuTrigger>
- <DropdownMenuContent>
+ <DropdownMenuContent align="end">
<DropdownMenuItem
- onClick={() => downloadFile(file)}
+ onClick={() =>
+ downloadModel(modelUpload)
+ }
>
<DropdownMenuIcon
icon={<LuDownload />}
@@ -594,8 +595,64 @@ export const JobOperation = ({
</div>
</Td>
</Tr>
- );
- })
+ )}
+ {resolvedFiles.map((file) => {
+ const type = getFileType(file.name);
+ return (
+ <Tr key={file.id}>
+ <Td>
+ <HStack>
+ <FileIcon type={type} />
+ <span className="font-medium">
+ {["PDF", "Image"].includes(type) ? (
+ <FilePreview
+ bucket="private"
+ pathToFile={getFilePath(file)}
+ // @ts-ignore
+ type={getFileType(file.name)}
+ >
+ {file.name}
+ </FilePreview>
+ ) : (
+ file.name
+ )}
+ </span>
+ </HStack>
+ </Td>
+ <Td className="text-xs font-mono">
+ {convertKbToString(
+ Math.floor(
+ (file.metadata?.size ?? 0) / 1024
+ )
+ )}
+ </Td>
+ <Td>
+ <div className="flex justify-end w-full">
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <IconButton
+ aria-label="More"
+ icon={<LuMoreVertical />}
+ variant="secondary"
+ />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem
+ onClick={() => downloadFile(file)}
+ >
+ <DropdownMenuIcon
+ icon={<LuDownload />}
+ />
+ Download
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ </Td>
+ </Tr>
+ );
+ })}
+ </>
)}
</Tbody>
</Table>
@@ -1193,8 +1250,41 @@ function useFiles(job: Job) {
[carbon?.storage, getFilePath]
);
+ const downloadModel = useCallback(
+ async (model: { modelPath: string; modelName: string }) => {
+ if (!model.modelPath || !model.modelName) {
+ toast.error("Model data is missing");
+ return;
+ }
+
+ const result = await carbon?.storage
+ .from("private")
+ .download(model.modelPath);
+
+ if (!result || result.error) {
+ toast.error(result?.error?.message || "Error downloading file");
+ return;
+ }
+
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ const url = window.URL.createObjectURL(result.data);
+ a.href = url;
+ a.download = model.modelName;
+ a.click();
+
+ setTimeout(() => {
+ window.URL.revokeObjectURL(url);
+ document.body.removeChild(a);
+ }, 0);
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ []
+ );
+
return {
downloadFile,
+ downloadModel,
getFilePath,
};
}
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 783c88ffae..fb0309cbf6 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -32256,6 +32256,9 @@ export type Database = {
itemDescription: string
itemUnitOfMeasure: string
itemModelPath: string
+ itemModelId: string
+ itemModelName: string
+ itemModelSize: number
operationStatus: Database["public"]["Enums"]["jobOperationStatus"]
operationQuantity: number
quantityComplete: number
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 783c88ffae..fb0309cbf6 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -32256,6 +32256,9 @@ export type Database = {
itemDescription: string
itemUnitOfMeasure: string
itemModelPath: string
+ itemModelId: string
+ itemModelName: string
+ itemModelSize: number
operationStatus: Database["public"]["Enums"]["jobOperationStatus"]
operationQuantity: number
quantityComplete: number
diff --git a/packages/database/supabase/migrations/20250109045327_job-operation-model-file.sql b/packages/database/supabase/migrations/20250109045327_job-operation-model-file.sql
new file mode 100644
index 0000000000..f5c16ae707
--- /dev/null
+++ b/packages/database/supabase/migrations/20250109045327_job-operation-model-file.sql
@@ -0,0 +1,81 @@
+DROP FUNCTION get_job_operation_by_id(TEXT);
+CREATE OR REPLACE FUNCTION get_job_operation_by_id(operation_id TEXT)
+RETURNS TABLE (
+ id TEXT,
+ "jobId" TEXT,
+ "jobMakeMethodId" TEXT,
+ "operationOrder" DOUBLE PRECISION,
+ "processId" TEXT,
+ "workCenterId" TEXT,
+ description TEXT,
+ "setupTime" NUMERIC,
+ "setupUnit" factor,
+ "laborTime" NUMERIC,
+ "laborUnit" factor,
+ "machineTime" NUMERIC,
+ "machineUnit" factor,
+ "operationOrderType" "methodOperationOrder",
+ "jobReadableId" TEXT,
+ "jobStatus" "jobStatus",
+ "jobDueDate" DATE,
+ "jobDeadlineType" "deadlineType",
+ "parentMaterialId" TEXT,
+ "itemReadableId" TEXT,
+ "itemDescription" TEXT,
+ "itemUnitOfMeasure" TEXT,
+ "itemModelPath" TEXT,
+ "itemModelId" TEXT,
+ "itemModelName" TEXT,
+ "itemModelSize" BIGINT,
+ "operationStatus" "jobOperationStatus",
+ "operationQuantity" NUMERIC,
+ "quantityComplete" NUMERIC,
+ "quantityReworked" NUMERIC,
+ "quantityScrapped" NUMERIC,
+ "workInstruction" JSON
+) AS $$
+BEGIN
+ RETURN QUERY
+ SELECT
+ jo."id",
+ jo."jobId",
+ jo."jobMakeMethodId",
+ jo."order" AS "operationOrder",
+ jo."processId",
+ jo."workCenterId",
+ jo."description",
+ jo."setupTime",
+ jo."setupUnit",
+ jo."laborTime",
+ jo."laborUnit",
+ jo."machineTime",
+ jo."machineUnit",
+ jo."operationOrder" AS "operationOrderType",
+ j."jobId" AS "jobReadableId",
+ j."status" AS "jobStatus",
+ j."dueDate"::DATE AS "jobDueDate",
+ j."deadlineType" AS "jobDeadlineType",
+ jmm."parentMaterialId",
+ i."readableId" as "itemReadableId",
+ i."name" as "itemDescription",
+ uom."name" as "itemUnitOfMeasure",
+ m."modelPath" as "itemModelPath",
+ m."id" as "itemModelId",
+ m."name" as "itemModelName",
+ m."size" as "itemModelSize",
+ jo."status" AS "operationStatus",
+ jo."operationQuantity",
+ jo."quantityComplete",
+ jo."quantityReworked",
+ jo."quantityScrapped",
+ jo."workInstruction"
+ FROM "jobOperation" jo
+ JOIN "job" j ON j.id = jo."jobId"
+ LEFT JOIN "jobMakeMethod" jmm ON jo."jobMakeMethodId" = jmm.id
+ LEFT JOIN "item" i ON jmm."itemId" = i.id
+ LEFT JOIN "unitOfMeasure" uom ON i."unitOfMeasureCode" = uom."code" AND i."companyId" = uom."companyId"
+ LEFT JOIN "modelUpload" m ON i."modelUploadId" = m.id
+ WHERE jo.id = operation_id
+ LIMIT 1;
+END;
+$$ LANGUAGE plpgsql;