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.

refactor: fix triggers for multi-tenant

Brad Barbin13f96261a05b
diff --git a/apps/carbon/app/components/Layout/Topbar/Breadcrumbs.tsx b/apps/carbon/app/components/Layout/Topbar/Breadcrumbs.tsx
index 42db582b0b..307efab198 100644
--- a/apps/carbon/app/components/Layout/Topbar/Breadcrumbs.tsx
+++ b/apps/carbon/app/components/Layout/Topbar/Breadcrumbs.tsx
@@ -7,6 +7,9 @@ import {
BreadcrumbLink,
Breadcrumbs as BreadcrumbsBase,
} from "~/components";
+import { useRouteData } from "~/hooks";
+import type { Company } from "~/modules/settings";
+import { path } from "~/utils/path";
export const BreadcrumbHandle = z.object({
breadcrumb: z.any(),
@@ -19,6 +22,9 @@ const BreadcrumbHandleMatch = z.object({
});
const Breadcrumbs = () => {
+ const routeData = useRouteData<{ company: Company }>(
+ path.to.authenticatedRoot
+ );
const matches = useMatches();
const breadcrumbs = matches
@@ -38,7 +44,7 @@ const Breadcrumbs = () => {
return (
<HStack className="items-center h-full -ml-2" spacing={0}>
<BreadcrumbsBase className="line-clamp-1">
- {width && width <= 640 && (
+ {width && width <= 640 ? (
<BreadcrumbItem>
<Button isIcon asChild variant="ghost">
<Link to="/">
@@ -46,6 +52,10 @@ const Breadcrumbs = () => {
</Link>
</Button>
</BreadcrumbItem>
+ ) : (
+ <BreadcrumbItem>
+ <BreadcrumbLink to="/">{routeData?.company.name}</BreadcrumbLink>
+ </BreadcrumbItem>
)}
{breadcrumbs.map((breadcrumb, i) => (
<BreadcrumbItem key={i}>
diff --git a/apps/carbon/app/components/Search/Search/Search.tsx b/apps/carbon/app/components/Search/Search/Search.tsx
index 5c6d182195..bff8e642b6 100644
--- a/apps/carbon/app/components/Search/Search/Search.tsx
+++ b/apps/carbon/app/components/Search/Search/Search.tsx
@@ -269,7 +269,7 @@ const SearchButton = () => {
<Button
leftIcon={<RxMagnifyingGlass />}
variant="secondary"
- className="w-[200px] px-2 text-muted-foreground "
+ className="w-[130px] px-2 text-muted-foreground "
onClick={searchModal.onOpen}
>
<HStack className="w-full">
diff --git a/apps/carbon/app/modules/sales/ui/Quotations/QuotationsTable.tsx b/apps/carbon/app/modules/sales/ui/Quotations/QuotationsTable.tsx
index cabe6d45e0..b171d962cf 100644
--- a/apps/carbon/app/modules/sales/ui/Quotations/QuotationsTable.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotations/QuotationsTable.tsx
@@ -165,6 +165,7 @@ const QuotationsTable = memo(({ data, count }: QuotationsTableProps) => {
label: <QuotationStatus status={status} />,
})),
},
+ pluralHeader: "Statuses",
},
},
{
diff --git a/apps/carbon/app/routes/x+/sales-order+/new.tsx b/apps/carbon/app/routes/x+/sales-order+/new.tsx
index 1e5d2787d9..f492b79888 100644
--- a/apps/carbon/app/routes/x+/sales-order+/new.tsx
+++ b/apps/carbon/app/routes/x+/sales-order+/new.tsx
@@ -41,9 +41,11 @@ export async function action({ request }: ActionFunctionArgs) {
);
}
+ const { id, salesOrderId, ...data } = validation.data;
+
const createSalesOrder = await upsertSalesOrder(client, {
salesOrderId: nextSequence.data,
- ...validation.data,
+ ...data,
companyId,
createdBy: userId,
customFields: setCustomFields(formData),
diff --git a/packages/database/package.json b/packages/database/package.json
index 3bf566ddb0..7a3131bad7 100644
--- a/packages/database/package.json
+++ b/packages/database/package.json
@@ -28,7 +28,7 @@
"clean": "rimraf .turbo node_modules dist",
"db:function:new": "supabase functions new",
"db:init": "supabase init",
- "db:kill": "npx --yes supabase stop --no-backup",
+ "db:kill": "supabase stop --no-backup",
"db:migrate": "supabase db reset",
"db:migrate:new": "supabase migration new",
"db:seed": "tsx src/seed.ts",
diff --git a/packages/database/supabase/functions/lib/seed.ts b/packages/database/supabase/functions/lib/seed.ts
index a50743a12a..81497b76e4 100644
--- a/packages/database/supabase/functions/lib/seed.ts
+++ b/packages/database/supabase/functions/lib/seed.ts
@@ -2440,6 +2440,15 @@ export const sequences = [
size: 6,
step: 1,
},
+ {
+ table: "quote",
+ name: "Quote",
+ prefix: "QO",
+ suffix: null,
+ next: 0,
+ size: 6,
+ step: 1,
+ },
];
export const supplierStauses = ["Active", "Inactive", "Pending", "Rejected"];
diff --git a/packages/database/supabase/migrations/20230123212853_search.sql b/packages/database/supabase/migrations/20230123212853_search.sql
index e816cda02d..8ea16ff511 100644
--- a/packages/database/supabase/migrations/20230123212853_search.sql
+++ b/packages/database/supabase/migrations/20230123212853_search.sql
@@ -121,7 +121,7 @@ ALTER TABLE "search" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Employees with sales_view can search for customers and sales orders" ON "search"
FOR SELECT
- USING (has_company_permission('sales_view', "companyId") AND entity IN ('Customer', 'Sales Order') AND (get_my_claim('role'::text)) = '"employee"'::jsonb);
+ USING (has_company_permission('sales_view', "companyId") AND entity IN ('Customer', 'Sales Order', 'Quotation') AND has_role('employee'));
-- TODO: customers should be able to search for their own sales orders
-- CREATE POLICY "Customers with sales_view can search for their own sales orders" ON "search"
@@ -141,7 +141,7 @@ CREATE POLICY "Employees with sales_view can search for customers and sales orde
CREATE POLICY "Employees with purchasing_view can search for suppliers and purchase orders" ON "search"
FOR SELECT
- USING (has_company_permission('purchasing_view', "companyId") AND entity IN ('Supplier', 'Purchase Order') AND (get_my_claim('role'::text)) = '"employee"'::jsonb);
+ USING (has_company_permission('purchasing_view', "companyId") AND entity IN ('Supplier', 'Purchase Order', 'Request for Quotation') AND (get_my_claim('role'::text)) = '"employee"'::jsonb);
CREATE POLICY "Employees with resources_view can search for resources" ON "search"
diff --git a/packages/database/supabase/migrations/20230330024716_parts.sql b/packages/database/supabase/migrations/20230330024716_parts.sql
index cc8fc8b130..7042fc9caf 100644
--- a/packages/database/supabase/migrations/20230330024716_parts.sql
+++ b/packages/database/supabase/migrations/20230330024716_parts.sql
@@ -250,7 +250,7 @@ RETURNS TRIGGER AS $$
BEGIN
IF (old.name <> new.name OR old.description <> new.description) THEN
UPDATE public.search SET name = new.id, description = new.name || ' ' || COALESCE(new.description, '')
- WHERE entity = 'Part' AND uuid = new.id;
+ WHERE entity = 'Part' AND uuid = new.id AND "companyId" = new."companyId";
END IF;
RETURN new;
END;
@@ -264,7 +264,7 @@ CREATE TRIGGER update_part_search_result
CREATE FUNCTION public.delete_part_search_result()
RETURNS TRIGGER AS $$
BEGIN
- DELETE FROM public.search WHERE entity = 'Part' AND uuid = old.id;
+ DELETE FROM public.search WHERE entity = 'Part' AND uuid = old.id AND "companyId" = old."companyId";
RETURN old;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
diff --git a/packages/database/supabase/migrations/20230702114301_purchasing-rls.sql b/packages/database/supabase/migrations/20230702114301_purchasing-rls.sql
index a5ab57e351..41ac8488d5 100644
--- a/packages/database/supabase/migrations/20230702114301_purchasing-rls.sql
+++ b/packages/database/supabase/migrations/20230702114301_purchasing-rls.sql
@@ -79,7 +79,7 @@ RETURNS TRIGGER AS $$
BEGIN
IF (old."purchaseOrderId" <> new."purchaseOrderId") THEN
UPDATE public.search SET name = new."purchaseOrderId"
- WHERE entity = 'Purchase Order' AND uuid = new.id;
+ WHERE entity = 'Purchase Order' AND uuid = new.id AND "companyId" = new."companyId";
END IF;
RETURN new;
END;
@@ -92,7 +92,7 @@ CREATE TRIGGER update_purchase_order_search_result
CREATE FUNCTION public.delete_purchase_order_search_result()
RETURNS TRIGGER AS $$
BEGIN
- DELETE FROM public.search WHERE entity = 'Purchase Order' AND uuid = old.id;
+ DELETE FROM public.search WHERE entity = 'Purchase Order' AND uuid = old.id AND "companyId" = old."companyId";
RETURN old;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
diff --git a/packages/database/supabase/migrations/20240213043906_quote-rls.sql b/packages/database/supabase/migrations/20240213043906_quote-rls.sql
index ef0fd45b30..b5bdc0917d 100644
--- a/packages/database/supabase/migrations/20240213043906_quote-rls.sql
+++ b/packages/database/supabase/migrations/20240213043906_quote-rls.sql
@@ -307,7 +307,7 @@ RETURNS TRIGGER AS $$
BEGIN
IF (old."quoteId" <> new."quoteId" OR old."name" <> new."name") THEN
UPDATE public.search SET name = new."quoteId", description = new.name
- WHERE entity = 'Quotation' AND uuid = new.id;
+ WHERE entity = 'Quotation' AND uuid = new.id AND "companyId" = new."companyId";
END IF;
RETURN new;
END;
@@ -320,7 +320,7 @@ CREATE TRIGGER update_quotation_search_result
CREATE FUNCTION public.delete_quotation_search_result()
RETURNS TRIGGER AS $$
BEGIN
- DELETE FROM public.search WHERE entity = 'Quotation' AND uuid = old.id;
+ DELETE FROM public.search WHERE entity = 'Quotation' AND uuid = old.id AND "companyId" = old."companyId";
RETURN old;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
diff --git a/packages/database/supabase/migrations/20240223235518_search_equipment_and_work_cell_types.sql b/packages/database/supabase/migrations/20240223235518_search_equipment_and_work_cell_types.sql
index 99c13f1f88..95028c8c04 100644
--- a/packages/database/supabase/migrations/20240223235518_search_equipment_and_work_cell_types.sql
+++ b/packages/database/supabase/migrations/20240223235518_search_equipment_and_work_cell_types.sql
@@ -19,7 +19,7 @@ RETURNS TRIGGER AS $$
BEGIN
IF (old.name <> new.name OR old.description <> new.description) THEN
UPDATE public.search SET name = new.name, description = COALESCE(new.description, '')
- WHERE entity = 'Resource' AND uuid = new.id;
+ WHERE entity = 'Resource' AND uuid = new.id AND "companyId" = new."companyId";
END IF;
RETURN new;
END;
@@ -32,7 +32,7 @@ CREATE TRIGGER update_work_cell_type_search_result
CREATE FUNCTION public.delete_work_cell_type_search_result()
RETURNS TRIGGER AS $$
BEGIN
- DELETE FROM public.search WHERE entity = 'Resource' AND uuid = old.id;
+ DELETE FROM public.search WHERE entity = 'Resource' AND uuid = old.id AND "companyId" = old."companyId";
RETURN old;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
@@ -61,7 +61,7 @@ RETURNS TRIGGER AS $$
BEGIN
IF (old.name <> new.name OR old.description <> new.description) THEN
UPDATE public.search SET name = new.name, description = COALESCE(new.description, '')
- WHERE entity = 'Resource' AND uuid = new.id;
+ WHERE entity = 'Resource' AND uuid = new.id AND "companyId" = new."companyId";
END IF;
RETURN new;
END;
@@ -74,7 +74,7 @@ CREATE TRIGGER update_equipment_type_search_result
CREATE FUNCTION public.delete_equipment_type_search_result()
RETURNS TRIGGER AS $$
BEGIN
- DELETE FROM public.search WHERE entity = 'Resource' AND uuid = old.id;
+ DELETE FROM public.search WHERE entity = 'Resource' AND uuid = old.id AND "companyId" = old."companyId";
RETURN old;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
diff --git a/packages/database/supabase/migrations/20240412224648_sales-orders.sql b/packages/database/supabase/migrations/20240412224648_sales-orders.sql
index 4f1d09be24..ddcf77817a 100644
--- a/packages/database/supabase/migrations/20240412224648_sales-orders.sql
+++ b/packages/database/supabase/migrations/20240412224648_sales-orders.sql
@@ -382,7 +382,7 @@ RETURNS TRIGGER AS $$
BEGIN
IF (old."salesOrderId" <> new."salesOrderId") THEN
UPDATE public.search SET name = new."salesOrderId"
- WHERE entity = 'Sales Order' AND uuid = new.id;
+ WHERE entity = 'Sales Order' AND uuid = new.id AND "companyId" = new."companyId";
END IF;
RETURN new;
END;
@@ -395,7 +395,7 @@ CREATE TRIGGER update_sales_order_search_result
CREATE FUNCTION public.delete_sales_order_search_result()
RETURNS TRIGGER AS $$
BEGIN
- DELETE FROM public.search WHERE entity = 'Sales Order' AND uuid = old.id;
+ DELETE FROM public.search WHERE entity = 'Sales Order' AND uuid = old.id AND "companyId" = old."companyId";
RETURN old;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;