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: shared namespace for item and search ids

Brad Barbineeef1e97a130
diff --git a/apps/carbon/app/components/Layout/Topbar/Search.tsx b/apps/carbon/app/components/Layout/Topbar/Search.tsx
index 81020bb391..bd834f51a2 100644
--- a/apps/carbon/app/components/Layout/Topbar/Search.tsx
+++ b/apps/carbon/app/components/Layout/Topbar/Search.tsx
@@ -24,7 +24,6 @@ import { useNavigate } from "@remix-run/react";
import idb from "localforage";
import { nanoid } from "nanoid";
import { useCallback, useEffect, useState } from "react";
-import { AiOutlinePartition } from "react-icons/ai";
import { HiOutlineDocumentDuplicate } from "react-icons/hi";
import {
LuHardHat,
@@ -41,6 +40,7 @@ import {
RiProgress8Line,
} from "react-icons/ri";
import { RxMagnifyingGlass } from "react-icons/rx";
+import { MethodItemTypeIcon } from "~/components/Icons";
import { useModules } from "~/components/Layout/Navigation/useModules";
import { useUser } from "~/hooks";
import { useAccountSubmodules } from "~/modules/account";
@@ -261,7 +261,11 @@ function ResultIcon({ entity }: { entity: SearchResult["entity"] | "Module" }) {
case "Job":
return <LuHardHat className="w-4 h-4 mr-2" />;
case "Part":
- return <AiOutlinePartition className="w-4 h-4 mr-2" />;
+ case "Material":
+ case "Tool":
+ case "Fixture":
+ case "Consumable":
+ return <MethodItemTypeIcon type={entity} className="w-4 h-4 mr-2" />;
case "Person":
return <LuUser className="w-4 h-4 mr-2" />;
case "Resource":
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 075576cb2a..05e42ca86e 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -30005,6 +30005,11 @@ export type Database = {
| "Purchase Invoice"
| "Document"
| "Sales RFQ"
+ | "Service"
+ | "Tool"
+ | "Consumable"
+ | "Material"
+ | "Fixture"
serviceType: "Internal" | "External"
shippingCarrier: "UPS" | "FedEx" | "USPS" | "DHL" | "Other"
supplierLedgerDocumentType:
diff --git a/packages/database/supabase/functions/import-csv/index.ts b/packages/database/supabase/functions/import-csv/index.ts
index 04fd010352..799817a877 100644
--- a/packages/database/supabase/functions/import-csv/index.ts
+++ b/packages/database/supabase/functions/import-csv/index.ts
@@ -275,6 +275,10 @@ serve(async (req: Request) => {
case "tool":
case "fixture":
case "part": {
+ const getExternalId = (id: string) => {
+ return `${table}:${id}`;
+ };
+
const currentItems = await db
.selectFrom("item")
.where("companyId", "=", companyId)
@@ -349,10 +353,10 @@ serve(async (req: Request) => {
const { id, ...rest } = item.data;
if (
- externalIdMap.has(id) &&
+ externalIdMap.has(getExternalId(id)) &&
!readableIds.has(item.data.readableId)
) {
- const existingItem = externalIdMap.get(id)!;
+ const existingItem = externalIdMap.get(getExternalId(id))!;
readableIds.add(item.data.readableId);
itemUpdates.push({
@@ -403,7 +407,7 @@ serve(async (req: Request) => {
createdAt: new Date().toISOString(),
createdBy: userId,
externalId: {
- [EXTERNAL_ID_KEY]: id,
+ [EXTERNAL_ID_KEY]: getExternalId(id),
},
};
itemInserts.push(newItem);
@@ -422,7 +426,7 @@ serve(async (req: Request) => {
createdAt: new Date().toISOString(),
createdBy: userId,
externalId: {
- [EXTERNAL_ID_KEY]: id,
+ [EXTERNAL_ID_KEY]: getExternalId(id),
},
};
}
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 075576cb2a..05e42ca86e 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -30005,6 +30005,11 @@ export type Database = {
| "Purchase Invoice"
| "Document"
| "Sales RFQ"
+ | "Service"
+ | "Tool"
+ | "Consumable"
+ | "Material"
+ | "Fixture"
serviceType: "Internal" | "External"
shippingCarrier: "UPS" | "FedEx" | "USPS" | "DHL" | "Other"
supplierLedgerDocumentType:
diff --git a/packages/database/supabase/migrations/20241014130404_item-search-uuid.sql b/packages/database/supabase/migrations/20241014130404_item-search-uuid.sql
new file mode 100644
index 0000000000..f03fd50d20
--- /dev/null
+++ b/packages/database/supabase/migrations/20241014130404_item-search-uuid.sql
@@ -0,0 +1,100 @@
+-- Drop existing triggers and functions
+DROP TRIGGER IF EXISTS create_item_search_result ON public.item;
+DROP FUNCTION IF EXISTS public.create_item_search_result();
+
+DROP TRIGGER IF EXISTS update_item_search_result ON public.item;
+DROP FUNCTION IF EXISTS public.update_item_search_result();
+
+DROP TRIGGER IF EXISTS delete_item_search_result ON public.item;
+DROP FUNCTION IF EXISTS public.delete_item_search_result();
+
+-- Add new values to the search entity enum
+ALTER TYPE "searchEntity" ADD VALUE IF NOT EXISTS 'Service';
+ALTER TYPE "searchEntity" ADD VALUE IF NOT EXISTS 'Tool';
+ALTER TYPE "searchEntity" ADD VALUE IF NOT EXISTS 'Consumable';
+ALTER TYPE "searchEntity" ADD VALUE IF NOT EXISTS 'Material';
+ALTER TYPE "searchEntity" ADD VALUE IF NOT EXISTS 'Fixture';
+COMMIT;
+
+-- Update the search policy to include the new entity types
+DROP POLICY IF EXISTS "Employees with parts can search for parts" ON "search";
+
+CREATE POLICY "Employees with parts can search for parts" ON "search"
+ FOR SELECT
+ USING (
+ has_company_permission('parts_view', "companyId")
+ AND entity IN ('Part', 'Service', 'Tool', 'Consumable', 'Material', 'Fixture')
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+
+
+-- Recreate functions with updated logic
+CREATE OR REPLACE FUNCTION public.create_item_search_result()
+RETURNS TRIGGER AS $$
+BEGIN
+ CASE new.type
+ WHEN 'Part' THEN
+ INSERT INTO public.search(name, description, entity, uuid, link, "companyId")
+ VALUES (new."readableId", new.name || ' ' || COALESCE(new.description, ''), 'Part', new.id, '/x/part/' || new.id, new."companyId");
+ WHEN 'Service' THEN
+ INSERT INTO public.search(name, description, entity, uuid, link, "companyId")
+ VALUES (new."readableId", new.name || ' ' || COALESCE(new.description, ''), 'Service', new.id, '/x/service/' || new.id, new."companyId");
+ WHEN 'Tool' THEN
+ INSERT INTO public.search(name, description, entity, uuid, link, "companyId")
+ VALUES (new."readableId", new.name || ' ' || COALESCE(new.description, ''), 'Tool', new.id, '/x/tool/' || new.id, new."companyId");
+ WHEN 'Consumable' THEN
+ INSERT INTO public.search(name, description, entity, uuid, link, "companyId")
+ VALUES (new."readableId", new.name || ' ' || COALESCE(new.description, ''), 'Consumable', new.id, '/x/consumable/' || new.id, new."companyId");
+ WHEN 'Material' THEN
+ INSERT INTO public.search(name, description, entity, uuid, link, "companyId")
+ VALUES (new."readableId", new.name || ' ' || COALESCE(new.description, ''), 'Material', new.id, '/x/material/' || new.id, new."companyId");
+ WHEN 'Fixture' THEN
+ INSERT INTO public.search(name, description, entity, uuid, link, "companyId")
+ VALUES (new."readableId", new.name || ' ' || COALESCE(new.description, ''), 'Fixture', new.id, '/x/fixture/' || new.id, new."companyId");
+ END CASE;
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE OR REPLACE FUNCTION public.update_item_search_result()
+RETURNS TRIGGER AS $$
+BEGIN
+ IF (old.name <> new.name OR old.description <> new.description OR old."readableId" <> new."readableId" OR old.type <> new.type) THEN
+ UPDATE public.search
+ SET name = new."readableId",
+ description = new.name || ' ' || COALESCE(new.description, ''),
+ link = CASE new.type
+ WHEN 'Part' THEN '/x/part/' || new.id
+ WHEN 'Service' THEN '/x/service/' || new.id
+ WHEN 'Tool' THEN '/x/tool/' || new.id
+ WHEN 'Consumable' THEN '/x/consumable/' || new.id
+ WHEN 'Material' THEN '/x/material/' || new.id
+ WHEN 'Fixture' THEN '/x/fixture/' || new.id
+ END
+ WHERE entity = 'Part' AND uuid = new.id AND "companyId" = new."companyId";
+ END IF;
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE OR REPLACE FUNCTION public.delete_item_search_result()
+RETURNS TRIGGER AS $$
+BEGIN
+ DELETE FROM public.search WHERE entity = 'Part' AND uuid = old.id AND "companyId" = old."companyId";
+ RETURN old;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+-- Recreate triggers
+CREATE TRIGGER create_item_search_result
+ AFTER INSERT ON public.item
+ FOR EACH ROW EXECUTE PROCEDURE public.create_item_search_result();
+
+CREATE TRIGGER update_item_search_result
+ AFTER UPDATE ON public.item
+ FOR EACH ROW EXECUTE PROCEDURE public.update_item_search_result();
+
+CREATE TRIGGER delete_item_search_result
+ AFTER DELETE ON public.item
+ FOR EACH ROW EXECUTE PROCEDURE public.delete_item_search_result();