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: rls and search for rfqs

Brad Barbin34b9f290e130
diff --git a/apps/carbon/app/components/Search/Search/Search.tsx b/apps/carbon/app/components/Search/Search/Search.tsx
index a022ba5bec..455bebce55 100644
--- a/apps/carbon/app/components/Search/Search/Search.tsx
+++ b/apps/carbon/app/components/Search/Search/Search.tsx
@@ -22,11 +22,16 @@ import { nanoid } from "nanoid";
import { useCallback, useEffect, useState } from "react";
import { AiOutlinePartition } from "react-icons/ai";
import { BiListCheck } from "react-icons/bi";
-import { BsCartDash, BsCartPlus } from "react-icons/bs";
+import { BsCartDash } from "react-icons/bs";
import { CgProfile } from "react-icons/cg";
import { HiOutlineDocumentDuplicate } from "react-icons/hi";
-import { LuSearch } from "react-icons/lu";
+import { LuSearch, LuUserSquare } from "react-icons/lu";
import { PiShareNetworkFill } from "react-icons/pi";
+import {
+ RiProgress2Line,
+ RiProgress4Line,
+ RiProgress8Line,
+} from "react-icons/ri";
import { RxMagnifyingGlass } from "react-icons/rx";
import { useModules } from "~/components/Layout/Navigation/useModules";
import { ShortcutKey } from "~/components/ShortcutKey";
@@ -178,7 +183,7 @@ const SearchModal = ({
// append with : so we're not sharing a value with a static result
value={`:${result.to}`}
>
- <RxMagnifyingGlass className="w-4 h-4 mr-2 " />
+ <RxMagnifyingGlass className="w-4 h-4 mr-2" />
{result.name}
</CommandItem>
))}
@@ -196,7 +201,7 @@ const SearchModal = ({
value={`${module} ${submodule.name}`}
>
{submodule.icon && (
- <submodule.icon className="w-4 h-4 mr-2 " />
+ <submodule.icon className="w-4 h-4 mr-2" />
)}
<span>{submodule.name}</span>
</CommandItem>
@@ -243,26 +248,29 @@ const SearchModal = ({
function ResultIcon({ entity }: { entity: SearchResult["entity"] | "Module" }) {
switch (entity) {
case "Customer":
- return <PiShareNetworkFill className="w-4 h-4 mr-2 " />;
+ return <LuUserSquare className="w-4 h-4 mr-2" />;
case "Document":
- return <HiOutlineDocumentDuplicate className="w-4 h-4 mr-2 " />;
+ return <HiOutlineDocumentDuplicate className="w-4 h-4 mr-2" />;
case "Job":
- return <BiListCheck className="w-4 h-4 mr-2 " />;
+ return <BiListCheck className="w-4 h-4 mr-2" />;
case "Part":
- return <AiOutlinePartition className="w-4 h-4 mr-2 " />;
+ return <AiOutlinePartition className="w-4 h-4 mr-2" />;
case "Person":
- return <CgProfile className="w-4 h-4 mr-2 " />;
+ return <CgProfile className="w-4 h-4 mr-2" />;
case "Resource":
- return <CgProfile className="w-4 h-4 mr-2 " />;
+ return <CgProfile className="w-4 h-4 mr-2" />;
case "Purchase Order":
- return <BsCartDash className="w-4 h-4 mr-2 " />;
+ return <BsCartDash className="w-4 h-4 mr-2" />;
case "Opportunity":
case "Lead":
+ case "Sales RFQ":
+ return <RiProgress2Line className="w-4 h-4 mr-2" />;
case "Quotation":
+ return <RiProgress4Line className="w-4 h-4 mr-2" />;
case "Sales Order":
- return <BsCartPlus className="w-4 h-4 mr-2 " />;
+ return <RiProgress8Line className="w-4 h-4 mr-2" />;
case "Supplier":
- return <PiShareNetworkFill className="w-4 h-4 mr-2 " />;
+ return <PiShareNetworkFill className="w-4 h-4 mr-2" />;
default:
return null;
}
diff --git a/apps/carbon/app/modules/sales/ui/SalesRFQ/SalesRFQLineDocuments.tsx b/apps/carbon/app/modules/sales/ui/SalesRFQ/SalesRFQLineDocuments.tsx
index ef66d3aaa0..310a66b7f5 100644
--- a/apps/carbon/app/modules/sales/ui/SalesRFQ/SalesRFQLineDocuments.tsx
+++ b/apps/carbon/app/modules/sales/ui/SalesRFQ/SalesRFQLineDocuments.tsx
@@ -166,8 +166,6 @@ const SalesRFQLineDocuments = ({
salesRfqLineId,
});
- console.log({ files });
-
return (
<div className="min-h-[300px]">
<Table>
diff --git a/apps/carbon/app/routes/x+/sales-rfq+/$rfqId.tsx b/apps/carbon/app/routes/x+/sales-rfq+/$rfqId.tsx
index ae57911f2f..f42aa7b308 100644
--- a/apps/carbon/app/routes/x+/sales-rfq+/$rfqId.tsx
+++ b/apps/carbon/app/routes/x+/sales-rfq+/$rfqId.tsx
@@ -41,8 +41,6 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
);
}
- console.log({ files: files.data?.files });
-
return json({
rfqSummary: rfqSummary.data,
files: files.data?.files ?? [],
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index e3d3e280c1..ab08a23498 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -25401,6 +25401,7 @@ export type Database = {
| "Sales Invoice"
| "Purchase Invoice"
| "Document"
+ | "Sales RFQ"
serviceType: "Internal" | "External"
shippingCarrier: "UPS" | "FedEx" | "USPS" | "DHL" | "Other"
supplierLedgerDocumentType:
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index e3d3e280c1..ab08a23498 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -25401,6 +25401,7 @@ export type Database = {
| "Sales Invoice"
| "Purchase Invoice"
| "Document"
+ | "Sales RFQ"
serviceType: "Internal" | "External"
shippingCarrier: "UPS" | "FedEx" | "USPS" | "DHL" | "Other"
supplierLedgerDocumentType:
diff --git a/packages/database/supabase/migrations/20240713130609_rfq-rls.sql b/packages/database/supabase/migrations/20240713130609_rfq-rls.sql
new file mode 100644
index 0000000000..c610052302
--- /dev/null
+++ b/packages/database/supabase/migrations/20240713130609_rfq-rls.sql
@@ -0,0 +1,146 @@
+-- Search
+ALTER TYPE "searchEntity" ADD VALUE 'Sales RFQ';
+COMMIT;
+
+CREATE FUNCTION public.create_sales_rfq_search_result()
+RETURNS TRIGGER AS $$
+BEGIN
+ INSERT INTO public.search(name, entity, uuid, link, "companyId")
+ VALUES (new."rfqId", 'Sales RFQ', new.id, '/x/sales-rfq/' || new.id, new."companyId");
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE TRIGGER create_sales_rfq_search_result
+ AFTER INSERT on public."salesRfq"
+ FOR EACH ROW EXECUTE PROCEDURE public.create_sales_rfq_search_result();
+
+CREATE FUNCTION public.update_sales_rfq_search_result()
+RETURNS TRIGGER AS $$
+BEGIN
+ IF (old."rfqId" <> new."rfqId") THEN
+ UPDATE public.search SET name = new."rfqId"
+ WHERE entity = 'Sales RFQ' AND uuid = new.id AND "companyId" = new."companyId";
+ END IF;
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE TRIGGER update_sales_rfq_search_result
+ AFTER UPDATE on public."salesRfq"
+ FOR EACH ROW EXECUTE PROCEDURE public.update_sales_rfq_search_result();
+
+CREATE FUNCTION public.delete_sales_rfq_search_result()
+RETURNS TRIGGER AS $$
+BEGIN
+ DELETE FROM public.search WHERE entity = 'Sales RFQ' AND uuid = old.id AND "companyId" = old."companyId";
+ RETURN old;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE TRIGGER delete_sales_rfq_search_result
+ AFTER DELETE on public."salesRfq"
+ FOR EACH ROW EXECUTE PROCEDURE public.delete_sales_rfq_search_result();
+
+
+
+CREATE POLICY "Employees with sales_view can search for sales rfqs" ON "search"
+ FOR SELECT
+ USING (
+ has_company_permission('sales_view', "companyId") AND
+ entity IN ('Sales RFQ') AND
+ has_role('employee', "companyId")
+ );
+
+
+
+-- Custom Fields
+
+INSERT INTO "customFieldTable" ("table", "name", "module")
+VALUES ('salesRfq', 'RFQ (Sales)', 'Sales');
+
+INSERT INTO "customFieldTable" ("table", "name", "module")
+VALUES ('salesRfqLine', 'RFQ Line (Sales)', 'Sales');
+
+-- RLS
+ALTER TABLE "salesRfq" ENABLE ROW LEVEL SECURITY;
+CREATE POLICY "Employees with sales_view can view sales rfqs" ON "salesRfq"
+ FOR SELECT
+ USING (
+ has_company_permission('sales_view', "companyId") AND
+ has_role('employee', "companyId")
+ );
+
+CREATE POLICY "Employees with sales_create can create sales rfqs" ON "salesRfq"
+ FOR INSERT
+ WITH CHECK (
+ has_company_permission('sales_create', "companyId") AND
+ has_role('employee', "companyId")
+ );
+
+CREATE POLICY "Employees with sales_update can edit sales rfqs" ON "salesRfq"
+ FOR UPDATE
+ USING (
+ has_company_permission('sales_update', "companyId") AND
+ has_role('employee', "companyId")
+ );
+
+CREATE POLICY "Employees with sales_delete can delete sales rfqs" ON "salesRfq"
+ FOR DELETE
+ USING (
+ has_company_permission('sales_delete', "companyId") AND
+ has_role('employee', "companyId")
+ );
+
+CREATE POLICY "Customer with sales_view can view their own sales rfqs" ON "salesRfq"
+ FOR SELECT
+ USING (
+ has_company_permission('sales_view', "companyId") AND
+ has_role('customer', "companyId") AND
+ "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = auth.uid()
+ )
+ );
+
+ALTER TABLE "salesRfq" ENABLE ROW LEVEL SECURITY;
+CREATE POLICY "Employees with sales_view can view sales rfq lines" ON "salesRfqLine"
+ FOR SELECT
+ USING (
+ has_company_permission('sales_view', "companyId") AND
+ has_role('employee', "companyId")
+ );
+
+CREATE POLICY "Employees with sales_create can create sales rfq lines" ON "salesRfqLine"
+ FOR INSERT
+ WITH CHECK (
+ has_company_permission('sales_create', "companyId") AND
+ has_role('employee', "companyId")
+ );
+
+CREATE POLICY "Employees with sales_update can edit sales rfq lines" ON "salesRfqLine"
+ FOR UPDATE
+ USING (
+ has_company_permission('sales_update', "companyId") AND
+ has_role('employee', "companyId")
+ );
+
+CREATE POLICY "Employees with sales_delete can delete sales rfq lines" ON "salesRfqLine"
+ FOR DELETE
+ USING (
+ has_company_permission('sales_delete', "companyId") AND
+ has_role('employee', "companyId")
+ );
+
+CREATE POLICY "Customers with sales_view can their own purchase order lines" ON "salesRfqLine"
+ FOR SELECT
+ USING (
+ has_company_permission('sales_view', "companyId") AND
+ has_role('customer', "companyId") AND
+ "salesRfqId" IN (
+ SELECT id FROM "salesRfq" WHERE "customerId" IN (
+ SELECT "customerId" FROM "salesRfq" WHERE "customerId" IN (
+ SELECT "customerId" FROM "customerAccount" WHERE id::uuid = auth.uid()
+ )
+ )
+ )
+ );
\ No newline at end of file