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: modal for making default revision

Brad Barbinbb72302a0606
diff --git a/apps/erp/app/modules/items/items.service.ts b/apps/erp/app/modules/items/items.service.ts
index 66f132dfe4..a9fa0b7766 100644
--- a/apps/erp/app/modules/items/items.service.ts
+++ b/apps/erp/app/modules/items/items.service.ts
@@ -1391,6 +1391,23 @@ export async function updateOperationOrder(
return Promise.all(updatePromises);
}
+export async function updateRevision(
+ client: SupabaseClient<Database>,
+ revision: {
+ id: string;
+ revision: string;
+ updatedBy: string;
+ }
+) {
+ return client
+ .from("item")
+ .update({
+ ...revision,
+ updatedAt: today(getLocalTimeZone()).toString(),
+ })
+ .eq("id", revision.id);
+}
+
export async function upsertConfigurationParameter(
client: SupabaseClient<Database>,
configurationParameter: z.infer<typeof configurationParameterValidator> & {
diff --git a/apps/erp/app/modules/items/ui/Item/ItemWithRevision.tsx b/apps/erp/app/modules/items/ui/Item/ItemWithRevision.tsx
new file mode 100644
index 0000000000..ff01d4f9d6
--- /dev/null
+++ b/apps/erp/app/modules/items/ui/Item/ItemWithRevision.tsx
@@ -0,0 +1,24 @@
+import { Badge } from "@carbon/react";
+
+export function ItemWithRevision({
+ item,
+}: {
+ item?: {
+ readableId?: string | null;
+ revision?: string | null;
+ } | null;
+}) {
+ if (!item) return null;
+ const { readableId, revision } = item;
+ if (!readableId) return null;
+ return (
+ <div className="flex items-center gap-1">
+ <span>{readableId}</span>
+ {revision && revision !== "0" && (
+ <Badge variant="outline" className="font-mono">
+ Rev {revision}
+ </Badge>
+ )}
+ </div>
+ );
+}
diff --git a/apps/erp/app/modules/items/ui/Item/UsedIn.tsx b/apps/erp/app/modules/items/ui/Item/UsedIn.tsx
index 89728ec143..4f077f3f59 100644
--- a/apps/erp/app/modules/items/ui/Item/UsedIn.tsx
+++ b/apps/erp/app/modules/items/ui/Item/UsedIn.tsx
@@ -2,6 +2,11 @@ import type { Database, Json } from "@carbon/database";
import {
cn,
Count,
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuIcon,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
HStack,
IconButton,
Input,
@@ -14,9 +19,17 @@ import {
import { useParams } from "@remix-run/react";
import { useState } from "react";
import { flushSync } from "react-dom";
-import { LuChevronRight, LuPlus, LuSearch, LuTruck } from "react-icons/lu";
+import {
+ LuChevronRight,
+ LuEllipsisVertical,
+ LuPlus,
+ LuSearch,
+ LuStar,
+ LuTruck,
+} from "react-icons/lu";
import { z } from "zod";
import { Hyperlink, MethodIcon } from "~/components";
+import { Confirm } from "~/components/Modals";
import { LevelLine } from "~/components/TreeView";
import { usePermissions } from "~/hooks";
import type { MethodItemType } from "~/modules/shared";
@@ -60,6 +73,7 @@ export type UsedInNode = {
documentParentId?: string;
itemType?: MethodItemType;
methodType?: string;
+ revision?: string;
}[];
};
@@ -93,7 +107,14 @@ export function UsedInTree({
type: r.type,
revision: r.revision,
}))
- .sort((a, b) => b.revision.localeCompare(a.revision));
+ .sort((a, b) => {
+ // First sort by length (descending)
+ if (a.revision.length !== b.revision.length) {
+ return b.revision.length - a.revision.length;
+ }
+ // Then sort alphabetically (descending)
+ return b.revision.localeCompare(a.revision);
+ });
return (
<VStack>
@@ -144,7 +165,8 @@ export function RevisionsItem({
}) {
const { itemId } = useParams();
const permissions = usePermissions();
- const disclosure = useDisclosure();
+ const revisionDisclosure = useDisclosure();
+ const defaultDisclosure = useDisclosure();
const [selectedRevision, setSelectedRevision] = useState<{
id?: string;
@@ -197,14 +219,14 @@ export function RevisionsItem({
type: node.key as "Part",
revision: getNextRevision(maxRevision),
});
- disclosure.onOpen();
+ revisionDisclosure.onOpen();
});
}}
/>
)}
</div>
{isExpanded && (
- <div className="flex flex-col w-full">
+ <div className="flex flex-col w-full relative ">
{node.children.length === 0 ? (
<div className="flex h-8 items-center overflow-hidden rounded-sm px-2 gap-4">
<LevelLine isSelected={false} />
@@ -216,31 +238,95 @@ export function RevisionsItem({
filteredChildren.map((child, index) => {
const isActive = child.id === itemId;
return (
- <Hyperlink
- key={index}
- to={getUseInLink(child, node.key, "")}
- className={cn(
- "flex h-8 cursor-pointer items-center overflow-hidden rounded-sm px-1 gap-4 text-sm hover:bg-muted/90 w-full font-medium whitespace-nowrap",
- isActive && "bg-muted/90"
+ <div className="relative group/used-in" key={index}>
+ <Hyperlink
+ to={getUseInLink(child, node.key, "")}
+ className={cn(
+ "pr-6 flex h-8 cursor-pointer items-center overflow-hidden rounded-sm px-1 gap-4 text-sm hover:bg-muted/90 w-full font-medium whitespace-nowrap",
+ isActive && "bg-muted/90"
+ )}
+ >
+ <LevelLine isSelected={isActive} className="mr-2" />
+ <MethodIcon
+ type={child.methodType ?? "Method"}
+ className="mr-2"
+ />
+ <span className="truncate">{child.documentReadableId}</span>
+ </Hyperlink>
+ {permissions.can("update", "parts") && (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <IconButton
+ size="sm"
+ variant="secondary"
+ icon={<LuEllipsisVertical />}
+ aria-label="Edit"
+ className="absolute right-2 top-1 flex-shrink-0 opacity-0 group-hover/used-in:opacity-100 data-[state=open]:opacity-100"
+ />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ {/* <DropdownMenuItem
+ onSelect={() => {
+ flushSync(() => {
+ setSelectedRevision({
+ id: child.id,
+ type: node.key as "Part",
+ revision: child.revision ?? "",
+ });
+ revisionDisclosure.onOpen();
+ });
+ }}
+ >
+ <DropdownMenuIcon icon={<LuPencil />} />
+ Edit
+ </DropdownMenuItem> */}
+ <DropdownMenuItem
+ destructive
+ onSelect={() => {
+ flushSync(() => {
+ setSelectedRevision({
+ id: child.id,
+ type: node.key as "Part",
+ revision: child.revision ?? "",
+ });
+ defaultDisclosure.onOpen();
+ });
+ }}
+ >
+ <DropdownMenuIcon icon={<LuStar />} />
+ Make Default
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
)}
- >
- <LevelLine isSelected={isActive} className="mr-2" />
- <MethodIcon
- type={child.methodType ?? "Method"}
- className="mr-2"
- />
- <span className="truncate">{child.documentReadableId}</span>
- </Hyperlink>
+ </div>
);
})
)}
</div>
)}
- {disclosure.isOpen && selectedRevision && (
+ {revisionDisclosure.isOpen && selectedRevision && (
<RevisionForm
initialValues={selectedRevision!}
- onClose={disclosure.onClose}
+ onClose={revisionDisclosure.onClose}
+ />
+ )}
+ {defaultDisclosure.isOpen && selectedRevision && (
+ <Confirm
+ action={path.to.defaultRevision(
+ selectedRevision.id,
+ selectedRevision.revision
+ )}
+ confirmText="Make Default"
+ title={`Make revision ${selectedRevision.revision} default?`}
+ text="This will replace all method materials of other revisions with this revision."
+ isOpen
+ onSubmit={() => {
+ defaultDisclosure.onClose();
+ setSelectedRevision(null);
+ }}
+ onCancel={defaultDisclosure.onClose}
/>
)}
</>
@@ -250,10 +336,20 @@ export function RevisionsItem({
function getNextRevision(maxRevision: string) {
if (/^\d+$/.test(maxRevision)) {
return (parseInt(maxRevision) + 1).toString();
- } else if (/^[A-Z]$/.test(maxRevision)) {
- return maxRevision === "Z"
- ? "AA"
- : String.fromCharCode(maxRevision.charCodeAt(0) + 1);
+ } else if (/^[A-Z]{1,2}$/.test(maxRevision)) {
+ // Handle single letter case
+ if (maxRevision.length === 1) {
+ return maxRevision === "Z"
+ ? "AA"
+ : String.fromCharCode(maxRevision.charCodeAt(0) + 1);
+ }
+ // Handle double letter case
+ const firstChar = maxRevision[0];
+ const secondChar = maxRevision[1];
+ if (secondChar === "Z") {
+ return String.fromCharCode(firstChar.charCodeAt(0) + 1) + "A";
+ }
+ return firstChar + String.fromCharCode(secondChar.charCodeAt(0) + 1);
}
return maxRevision;
}
diff --git a/packages/database/supabase/migrations/20250519122022_revisions.sql b/packages/database/supabase/migrations/20250519122022_revisions.sql
index 1e62647150..69620ecfdb 100644
--- a/packages/database/supabase/migrations/20250519122022_revisions.sql
+++ b/packages/database/supabase/migrations/20250519122022_revisions.sql
@@ -206,7 +206,8 @@ BEGIN
'revision', i."revision",
'methodType', i."defaultMethodType",
'type', i."type"
- ) ORDER BY i."revision" DESC NULLS LAST
+ ) ORDER BY
+ i."createdAt"
) as "revisions"
FROM "item" i
GROUP BY i."readableId"
@@ -271,7 +272,7 @@ WITH latest_items AS (
mu."size" as "modelSize"
FROM "item" i
LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"
- ORDER BY i."readableId", i."revision" DESC NULLS LAST
+ ORDER BY i."readableId", i."createdAt" DESC NULLS LAST
),
item_revisions AS (
SELECT
@@ -284,8 +285,8 @@ item_revisions AS (
'description', i."description",
'active', i."active",
'createdAt', i."createdAt"
- ) ORDER BY i."revision" DESC NULLS LAST
- ) as "revisions"
+ ) ORDER BY i."createdAt"
+ ) as "revisions"
FROM "item" i
GROUP BY i."readableId"
)
@@ -322,7 +323,7 @@ SELECT
li."updatedBy",
li."updatedAt"
FROM "part" p
- INNER JOIN latest_items li ON li."readableId" = p."id"
+INNER JOIN latest_items li ON li."readableId" = p."id"
LEFT JOIN item_revisions ir ON ir."readableId" = p."id"
LEFT JOIN (
SELECT
@@ -346,7 +347,7 @@ WITH latest_items AS (
mu."size" as "modelSize"
FROM "item" i
LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"
- ORDER BY i."readableId", i."revision" DESC NULLS LAST
+ ORDER BY i."readableId", i."createdAt" DESC NULLS LAST
),
item_revisions AS (
SELECT
@@ -357,8 +358,8 @@ item_revisions AS (
'revision', i."revision",
'methodType', i."defaultMethodType",
'type', i."type"
- ) ORDER BY i."revision" DESC NULLS LAST
- ) as "revisions"
+ ) ORDER BY i."createdAt"
+ ) as "revisions"
FROM "item" i
GROUP BY i."readableId"
)
@@ -467,7 +468,8 @@ BEGIN
'revision', i."revision",
'methodType', i."defaultMethodType",
'type', i."type"
- ) ORDER BY i."revision" DESC NULLS LAST
+ ) ORDER BY
+ i."createdAt"
) as "revisions"
FROM "item" i
GROUP BY i."readableId"
@@ -541,7 +543,7 @@ WITH latest_items AS (
mu."size" as "modelSize"
FROM "item" i
LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"
- ORDER BY i."readableId", i."revision" DESC NULLS LAST
+ ORDER BY i."readableId", i."createdAt" DESC NULLS LAST
),
item_revisions AS (
SELECT
@@ -552,8 +554,8 @@ item_revisions AS (
'revision', i."revision",
'methodType', i."defaultMethodType",
'type', i."type"
- ) ORDER BY i."revision" DESC NULLS LAST
- ) as "revisions"
+ ) ORDER BY i."createdAt"
+ ) as "revisions"
FROM "item" i
GROUP BY i."readableId"
)
@@ -645,7 +647,8 @@ BEGIN
'revision', i."revision",
'methodType', i."defaultMethodType",
'type', i."type"
- ) ORDER BY i."revision" DESC NULLS LAST
+ ) ORDER BY
+ i."createdAt"
) as "revisions"
FROM "item" i
GROUP BY i."readableId"
@@ -741,7 +744,8 @@ BEGIN
'revision', i."revision",
'methodType', i."defaultMethodType",
'type', i."type"
- ) ORDER BY i."revision" DESC NULLS LAST
+ ) ORDER BY
+ i."createdAt"
) as "revisions"
FROM "item" i
GROUP BY i."readableId"
@@ -806,7 +810,7 @@ WITH latest_items AS (
mu."size" as "modelSize"
FROM "item" i
LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"
- ORDER BY i."readableId", i."revision" DESC NULLS LAST
+ ORDER BY i."readableId", i."createdAt" DESC NULLS LAST
),
item_revisions AS (
SELECT
@@ -817,10 +821,10 @@ item_revisions AS (
'revision', i."revision",
'methodType', i."defaultMethodType",
'type', i."type"
- ) ORDER BY i."revision" DESC NULLS LAST
- ) as "revisions"
- FROM "item" i
- GROUP BY i."readableId"
+ ) ORDER BY i."createdAt"
+ ) as "revisions"
+ FROM "item" i
+ GROUP BY i."readableId"
)
SELECT
li."active",