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 tags to customer and supplier header

Brad Barbin4d7f34d6ab4f
diff --git a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierHeader.tsx b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierHeader.tsx
index 3f464fcd62..7354f19296 100644
--- a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierHeader.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierHeader.tsx
@@ -1,3 +1,4 @@
+import { ValidatedForm } from "@carbon/form";
import {
Card,
CardAction,
@@ -12,20 +13,26 @@ import {
VStack,
} from "@carbon/react";
-import { useParams } from "@remix-run/react";
+import { useFetcher, useParams } from "@remix-run/react";
+import { useCallback } from "react";
+import { z } from "zod";
import { EmployeeAvatar } from "~/components";
import { Enumerable } from "~/components/Enumerable";
+import { Tags } from "~/components/Form";
import { useRouteData } from "~/hooks";
import type { SupplierDetail, SupplierStatus } from "~/modules/purchasing";
+import type { action } from "~/routes/x+/settings+/tags";
import { path } from "~/utils/path";
const SupplierHeader = () => {
const { supplierId } = useParams();
if (!supplierId) throw new Error("Could not find supplierId");
- const routeData = useRouteData<{ supplier: SupplierDetail }>(
- path.to.supplier(supplierId)
- );
+ const fetcher = useFetcher<typeof action>();
+ const routeData = useRouteData<{
+ supplier: SupplierDetail;
+ tags: { name: string }[];
+ }>(path.to.supplier(supplierId));
const sharedSupplierData = useRouteData<{
supplierStatuses: SupplierStatus[];
@@ -35,14 +42,25 @@ const SupplierHeader = () => {
(status) => status.id === routeData?.supplier?.supplierStatusId
)?.name;
- // const optimisticAssignment = useOptimisticAssignment({
- // id: supplierId,
- // table: "supplier",
- // });
- // const assignee =
- // optimisticAssignment !== undefined
- // ? optimisticAssignment
- // : routeData?.supplier?.assignee;
+ const onUpdateTags = useCallback(
+ (value: string[]) => {
+ const formData = new FormData();
+
+ formData.append("ids", supplierId);
+ formData.append("table", "supplier");
+
+ value.forEach((v) => {
+ formData.append("value", v);
+ });
+
+ fetcher.submit(formData, {
+ method: "post",
+ action: path.to.tags,
+ });
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ [supplierId]
+ );
return (
<VStack>
@@ -77,6 +95,28 @@ const SupplierHeader = () => {
)}
</CardAttributeValue>
</CardAttribute>
+ <CardAttribute>
+ <CardAttributeValue>
+ <ValidatedForm
+ defaultValues={{
+ tags: routeData?.supplier?.tags ?? [],
+ }}
+ validator={z.object({
+ tags: z.array(z.string()).optional(),
+ })}
+ className="w-full"
+ >
+ <Tags
+ label="Tags"
+ name="tags"
+ availableTags={routeData?.tags ?? []}
+ table="supplier"
+ inline
+ onChange={onUpdateTags}
+ />
+ </ValidatedForm>
+ </CardAttributeValue>
+ </CardAttribute>
{/* {permissions.is("employee") && (
<CardAttribute>
diff --git a/apps/carbon/app/modules/purchasing/ui/Supplier/SuppliersTable.tsx b/apps/carbon/app/modules/purchasing/ui/Supplier/SuppliersTable.tsx
index 712f0a5ecb..a2c6ce8176 100644
--- a/apps/carbon/app/modules/purchasing/ui/Supplier/SuppliersTable.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/Supplier/SuppliersTable.tsx
@@ -1,4 +1,4 @@
-import { MenuIcon, MenuItem } from "@carbon/react";
+import { Badge, HStack, MenuIcon, MenuItem } from "@carbon/react";
import { formatDate } from "@carbon/utils";
import { useNavigate } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
@@ -12,6 +12,7 @@ import {
LuPhone,
LuPrinter,
LuStar,
+ LuTag,
LuUser,
} from "react-icons/lu";
import {
@@ -31,11 +32,12 @@ import { path } from "~/utils/path";
type SuppliersTableProps = {
data: Supplier[];
count: number;
+ tags: { name: string }[];
supplierStatuses: SupplierStatus[];
};
const SuppliersTable = memo(
- ({ data, count, supplierStatuses }: SuppliersTableProps) => {
+ ({ data, count, tags, supplierStatuses }: SuppliersTableProps) => {
const navigate = useNavigate();
const permissions = usePermissions();
const [people] = usePeople();
@@ -89,6 +91,30 @@ const SuppliersTable = memo(
icon: <LuUser />,
},
},
+ {
+ accessorKey: "tags",
+ header: "Tags",
+ cell: ({ row }) => (
+ <HStack spacing={0} className="gap-1">
+ {row.original.tags?.map((tag) => (
+ <Badge key={tag} variant="secondary">
+ {tag}
+ </Badge>
+ ))}
+ </HStack>
+ ),
+ meta: {
+ filter: {
+ type: "static",
+ options: tags?.map((tag) => ({
+ value: tag.name,
+ label: <Badge variant="secondary">{tag.name}</Badge>,
+ })),
+ isArray: true,
+ },
+ icon: <LuTag />,
+ },
+ },
// {
// id: "assignee",
// header: "Assignee",
diff --git a/apps/carbon/app/modules/sales/ui/Customer/CustomerHeader.tsx b/apps/carbon/app/modules/sales/ui/Customer/CustomerHeader.tsx
index c6f1c570cb..17fed0d6d1 100644
--- a/apps/carbon/app/modules/sales/ui/Customer/CustomerHeader.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customer/CustomerHeader.tsx
@@ -1,3 +1,4 @@
+import { ValidatedForm } from "@carbon/form";
import {
Card,
CardAction,
@@ -12,10 +13,14 @@ import {
VStack,
} from "@carbon/react";
-import { useParams } from "@remix-run/react";
+import { useFetcher, useParams } from "@remix-run/react";
+import { useCallback } from "react";
+import { z } from "zod";
import { EmployeeAvatar } from "~/components";
import { Enumerable } from "~/components/Enumerable";
+import { Tags } from "~/components/Form";
import { useRouteData } from "~/hooks";
+import type { action } from "~/routes/x+/settings+/tags";
import { path } from "~/utils/path";
import type { CustomerDetail, CustomerStatus } from "../../types";
@@ -23,9 +28,11 @@ const CustomerHeader = () => {
const { customerId } = useParams();
if (!customerId) throw new Error("Could not find customerId");
- const routeData = useRouteData<{ customer: CustomerDetail }>(
- path.to.customer(customerId)
- );
+ const fetcher = useFetcher<typeof action>();
+ const routeData = useRouteData<{
+ customer: CustomerDetail;
+ tags: { name: string }[];
+ }>(path.to.customer(customerId));
const sharedCustomerData = useRouteData<{
customerStatuses: CustomerStatus[];
@@ -35,14 +42,25 @@ const CustomerHeader = () => {
(status) => status.id === routeData?.customer?.customerStatusId
)?.name;
- // const optimisticAssignment = useOptimisticAssignment({
- // id: customerId,
- // table: "customer",
- // });
- // const assignee =
- // optimisticAssignment !== undefined
- // ? optimisticAssignment
- // : routeData?.customer?.assignee;
+ const onUpdateTags = useCallback(
+ (value: string[]) => {
+ const formData = new FormData();
+
+ formData.append("ids", customerId);
+ formData.append("table", "customer");
+
+ value.forEach((v) => {
+ formData.append("value", v);
+ });
+
+ fetcher.submit(formData, {
+ method: "post",
+ action: path.to.tags,
+ });
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ [customerId]
+ );
return (
<VStack>
@@ -77,6 +95,28 @@ const CustomerHeader = () => {
)}
</CardAttributeValue>
</CardAttribute>
+ <CardAttribute>
+ <CardAttributeValue>
+ <ValidatedForm
+ defaultValues={{
+ tags: routeData?.customer?.tags ?? [],
+ }}
+ validator={z.object({
+ tags: z.array(z.string()).optional(),
+ })}
+ className="w-full"
+ >
+ <Tags
+ label="Tags"
+ name="tags"
+ availableTags={routeData?.tags ?? []}
+ table="customer"
+ inline
+ onChange={onUpdateTags}
+ />
+ </ValidatedForm>
+ </CardAttributeValue>
+ </CardAttribute>
{/* {permissions.is("employee") && (
<CardAttribute>
<CardAttributeLabel>Assignee</CardAttributeLabel>
diff --git a/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx b/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
index a0fa0af660..60132e5c98 100644
--- a/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
@@ -1,4 +1,10 @@
-import { MenuIcon, MenuItem, useDisclosure } from "@carbon/react";
+import {
+ Badge,
+ HStack,
+ MenuIcon,
+ MenuItem,
+ useDisclosure,
+} from "@carbon/react";
import { formatDate } from "@carbon/utils";
import { useNavigate } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
@@ -12,6 +18,7 @@ import {
LuPhone,
LuPrinter,
LuStar,
+ LuTag,
LuTrash,
LuUser,
} from "react-icons/lu";
@@ -34,10 +41,11 @@ type CustomersTableProps = {
data: Customer[];
count: number;
customerStatuses: CustomerStatus[];
+ tags: { name: string }[];
};
const CustomersTable = memo(
- ({ data, count, customerStatuses }: CustomersTableProps) => {
+ ({ data, count, customerStatuses, tags }: CustomersTableProps) => {
const navigate = useNavigate();
const permissions = usePermissions();
const [people] = usePeople();
@@ -95,22 +103,30 @@ const CustomersTable = memo(
icon: <LuUser />,
},
},
- // {
- // id: "assignee",
- // header: "Assignee",
- // cell: ({ row }) => (
- // <EmployeeAvatar employeeId={row.original.assignee} />
- // ),
- // meta: {
- // filter: {
- // type: "static",
- // options: people.map((employee) => ({
- // value: employee.id,
- // label: employee.name,
- // })),
- // },
- // },
- // },
+ {
+ accessorKey: "tags",
+ header: "Tags",
+ cell: ({ row }) => (
+ <HStack spacing={0} className="gap-1">
+ {row.original.tags?.map((tag) => (
+ <Badge key={tag} variant="secondary">
+ {tag}
+ </Badge>
+ ))}
+ </HStack>
+ ),
+ meta: {
+ filter: {
+ type: "static",
+ options: tags?.map((tag) => ({
+ value: tag.name,
+ label: <Badge variant="secondary">{tag.name}</Badge>,
+ })),
+ isArray: true,
+ },
+ icon: <LuTag />,
+ },
+ },
{
accessorKey: "currencyCode",
header: "Currency",
@@ -196,7 +212,7 @@ const CustomersTable = memo(
];
return [...defaultColumns, ...customColumns];
- }, [customerStatuses, people, customColumns]);
+ }, [customerStatuses, people, customColumns, tags]);
const renderContextMenu = useMemo(
// eslint-disable-next-line react/display-name
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.tsx
index 2d94faa226..6f11f8eb55 100644
--- a/apps/carbon/app/routes/x+/customer+/$customerId.tsx
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.tsx
@@ -10,6 +10,7 @@ import {
getCustomerLocations,
} from "~/modules/sales";
import { CustomerHeader, CustomerSidebar } from "~/modules/sales/ui/Customer";
+import { getTagsList } from "~/modules/shared";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
@@ -19,17 +20,18 @@ export const handle: Handle = {
};
export async function loader({ request, params }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "sales",
});
const { customerId } = params;
if (!customerId) throw new Error("Could not find customerId");
- const [customer, contacts, locations] = await Promise.all([
+ const [customer, contacts, locations, tags] = await Promise.all([
getCustomer(client, customerId),
getCustomerContacts(client, customerId),
getCustomerLocations(client, customerId),
+ getTagsList(client, companyId, "customer"),
]);
if (customer.error) {
@@ -46,6 +48,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
customer: customer.data,
contacts: contacts.data ?? [],
locations: locations.data ?? [],
+ tags: tags.data ?? [],
});
}
diff --git a/apps/carbon/app/routes/x+/purchasing+/suppliers.tsx b/apps/carbon/app/routes/x+/purchasing+/suppliers.tsx
index e8f694d49b..eb37f9d2dd 100644
--- a/apps/carbon/app/routes/x+/purchasing+/suppliers.tsx
+++ b/apps/carbon/app/routes/x+/purchasing+/suppliers.tsx
@@ -7,6 +7,7 @@ import type { LoaderFunctionArgs } from "@vercel/remix";
import { json, redirect } from "@vercel/remix";
import { getSupplierStatuses, getSuppliers } from "~/modules/purchasing";
import { SuppliersTable } from "~/modules/purchasing/ui/Supplier";
+import { getTagsList } from "~/modules/shared";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
import { getGenericQueryFilters } from "~/utils/query";
@@ -31,7 +32,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { limit, offset, sorts, filters } =
getGenericQueryFilters(searchParams);
- const [suppliers, supplierStatuses] = await Promise.all([
+ const [suppliers, supplierStatuses, tags] = await Promise.all([
getSuppliers(client, companyId, {
search,
type,
@@ -42,6 +43,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
filters,
}),
getSupplierStatuses(client, companyId),
+ getTagsList(client, companyId, "supplier"),
]);
if (suppliers.error) {
@@ -55,11 +57,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
count: suppliers.count ?? 0,
suppliers: suppliers.data ?? [],
supplierStatuses: supplierStatuses.data ?? [],
+ tags: tags.data ?? [],
});
}
export default function PurchasingSuppliersRoute() {
- const { count, suppliers, supplierStatuses } = useLoaderData<typeof loader>();
+ const { count, suppliers, supplierStatuses, tags } =
+ useLoaderData<typeof loader>();
return (
<VStack spacing={0} className="h-full">
@@ -67,6 +71,7 @@ export default function PurchasingSuppliersRoute() {
data={suppliers}
count={count}
supplierStatuses={supplierStatuses}
+ tags={tags}
/>
<Outlet />
</VStack>
diff --git a/apps/carbon/app/routes/x+/sales+/customers.tsx b/apps/carbon/app/routes/x+/sales+/customers.tsx
index e25dbaabb9..a0428936ea 100644
--- a/apps/carbon/app/routes/x+/sales+/customers.tsx
+++ b/apps/carbon/app/routes/x+/sales+/customers.tsx
@@ -7,6 +7,7 @@ import type { LoaderFunctionArgs } from "@vercel/remix";
import { json, redirect } from "@vercel/remix";
import { getCustomerStatuses, getCustomers } from "~/modules/sales";
import { CustomersTable } from "~/modules/sales/ui/Customers";
+import { getTagsList } from "~/modules/shared";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
import { getGenericQueryFilters } from "~/utils/query";
@@ -29,7 +30,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { limit, offset, sorts, filters } =
getGenericQueryFilters(searchParams);
- const [customers, customerStatuses] = await Promise.all([
+ const [customers, customerStatuses, tags] = await Promise.all([
getCustomers(client, companyId, {
search,
limit,
@@ -38,6 +39,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
filters,
}),
getCustomerStatuses(client, companyId),
+ getTagsList(client, companyId, "customer"),
]);
if (customers.error) {
@@ -51,11 +53,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
count: customers.count ?? 0,
customers: customers.data ?? [],
customerStatuses: customerStatuses.data ?? [],
+ tags: tags.data ?? [],
});
}
export default function SalesCustomersRoute() {
- const { count, customers, customerStatuses } = useLoaderData<typeof loader>();
+ const { count, customers, customerStatuses, tags } =
+ useLoaderData<typeof loader>();
return (
<VStack spacing={0} className="h-full">
@@ -63,8 +67,8 @@ export default function SalesCustomersRoute() {
data={customers}
count={count}
customerStatuses={customerStatuses}
+ tags={tags}
/>
-
<Outlet />
</VStack>
);
diff --git a/apps/carbon/app/routes/x+/supplier+/$supplierId.tsx b/apps/carbon/app/routes/x+/supplier+/$supplierId.tsx
index 3cc45300b1..553b97acf0 100644
--- a/apps/carbon/app/routes/x+/supplier+/$supplierId.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/$supplierId.tsx
@@ -11,6 +11,7 @@ import {
} from "~/modules/purchasing";
import SupplierHeader from "~/modules/purchasing/ui/Supplier/SupplierHeader";
import SupplierSidebar from "~/modules/purchasing/ui/Supplier/SupplierSidebar";
+import { getTagsList } from "~/modules/shared";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
@@ -20,17 +21,18 @@ export const handle: Handle = {
};
export async function loader({ request, params }: LoaderFunctionArgs) {
- const { client } = await requirePermissions(request, {
+ const { client, companyId } = await requirePermissions(request, {
view: "purchasing",
});
const { supplierId } = params;
if (!supplierId) throw new Error("Could not find supplierId");
- const [supplier, contacts, locations] = await Promise.all([
+ const [supplier, contacts, locations, tags] = await Promise.all([
getSupplier(client, supplierId),
getSupplierContacts(client, supplierId),
getSupplierLocations(client, supplierId),
+ getTagsList(client, companyId, "supplier"),
]);
if (supplier.error) {
@@ -47,6 +49,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
supplier: supplier.data,
contacts: contacts.data ?? [],
locations: locations.data ?? [],
+ tags: tags.data ?? [],
});
}
diff --git a/apps/mes/app/components/JobOperation.tsx b/apps/mes/app/components/JobOperation.tsx
index 6f8bdb996e..fbd7372181 100644
--- a/apps/mes/app/components/JobOperation.tsx
+++ b/apps/mes/app/components/JobOperation.tsx
@@ -21,7 +21,6 @@ import {
Separator,
SidebarTrigger,
Skeleton,
- Slider,
Table,
Tabs,
TabsContent,
@@ -1601,14 +1600,6 @@ function QuantityModal({
onChange={setQuantity}
minValue={1}
/>
- <Slider
- className="py-3"
- value={[quantity]}
- onValueChange={(value) => setQuantity(value[0])}
- step={1}
- min={1}
- max={operation.operationQuantity}
- />
</>
)}
{type === "scrap" ? (
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 168f9b4544..c8ad699d7b 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -25131,6 +25131,7 @@ export type Database = {
orderCount: number | null
phone: string | null
status: string | null
+ tags: string[] | null
taxId: string | null
taxPercent: number | null
type: string | null
@@ -27992,14 +27993,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 168f9b4544..c8ad699d7b 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -25131,6 +25131,7 @@ export type Database = {
orderCount: number | null
phone: string | null
status: string | null
+ tags: string[] | null
taxId: string | null
taxPercent: number | null
type: string | null
@@ -27992,14 +27993,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/migrations/20241217130431_api-key-tags.sql b/packages/database/supabase/migrations/20241217130431_api-key-tags.sql
new file mode 100644
index 0000000000..74c0443f28
--- /dev/null
+++ b/packages/database/supabase/migrations/20241217130431_api-key-tags.sql
@@ -0,0 +1,22 @@
+CREATE POLICY "Requests with an API key can access tags" ON "tag"
+ FOR ALL USING (
+ has_valid_api_key_for_company("companyId")
+ );
+
+DROP VIEW "customers";
+CREATE OR REPLACE VIEW "customers" WITH(SECURITY_INVOKER=true) AS
+ SELECT
+ c.*,
+ ct.name AS "type",
+ cs.name AS "status",
+ so.count AS "orderCount"
+ FROM "customer" c
+ LEFT JOIN "customerType" ct ON ct.id = c."customerTypeId"
+ LEFT JOIN "customerStatus" cs ON cs.id = c."customerStatusId"
+ LEFT JOIN (
+ SELECT
+ "customerId",
+ COUNT(*) AS "count"
+ FROM "salesOrder"
+ GROUP BY "customerId"
+ ) so ON so."customerId" = c.id;
\ No newline at end of file