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: custom fields for customer form

Brad Barbin392edf2b9d61
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index 4e885781d7..d6f5cb11d9 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -1,4 +1,4 @@
-import type { Database } from "@carbon/database";
+import type { Database, Json } from "@carbon/database";
import type { SupabaseClient } from "@supabase/supabase-js";
import type { z } from "zod";
import type { GenericQueryFilters } from "~/utils/query";
@@ -448,20 +448,6 @@ export async function getQuoteOperations(
return client.from("quoteOperation").select("*").eq("quoteId", quoteId);
}
-export async function insertCustomer(
- client: SupabaseClient<Database>,
- customer:
- | (Omit<z.infer<typeof customerValidator>, "id"> & {
- createdBy: string;
- })
- | (Omit<z.infer<typeof customerValidator>, "id"> & {
- id: string;
- updatedBy: string;
- })
-) {
- return client.from("customer").insert([customer]).select("*").single();
-}
-
export async function insertCustomerContact(
client: SupabaseClient<Database>,
customerContact: {
@@ -572,13 +558,22 @@ export async function insertQuoteLineQuantity(
]);
}
-export async function updateCustomer(
+export async function upsertCustomer(
client: SupabaseClient<Database>,
- customer: Omit<z.infer<typeof customerValidator>, "id"> & {
- id: string;
- updatedBy: string;
- }
+ customer:
+ | (Omit<z.infer<typeof customerValidator>, "id"> & {
+ createdBy: string;
+ customFields?: Json;
+ })
+ | (Omit<z.infer<typeof customerValidator>, "id"> & {
+ id: string;
+ updatedBy: string;
+ customFields?: Json;
+ })
) {
+ if ("createdBy" in customer) {
+ return client.from("customer").insert([customer]).select("*").single();
+ }
return client
.from("customer")
.update(sanitize(customer))
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.index.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.index.tsx
index aa5f85e842..2889c0cb81 100644
--- a/apps/carbon/app/routes/x+/customer+/$customerId.index.tsx
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.index.tsx
@@ -7,10 +7,11 @@ import type { CustomerDetail } from "~/modules/sales";
import {
CustomerForm,
customerValidator,
- updateCustomer,
+ upsertCustomer,
} from "~/modules/sales";
import { requirePermissions } from "~/services/auth";
import { flash } from "~/services/session.server";
+import { getCustomFields, setCustomFields } from "~/utils/form";
import { assertIsPost } from "~/utils/http";
import { path } from "~/utils/path";
import { error, success } from "~/utils/result";
@@ -21,9 +22,8 @@ export async function action({ request }: ActionFunctionArgs) {
create: "sales",
});
- const validation = await validator(customerValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const validation = await validator(customerValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -38,9 +38,10 @@ export async function action({ request }: ActionFunctionArgs) {
);
}
- const update = await updateCustomer(client, {
+ const update = await upsertCustomer(client, {
id,
...data,
+ customFields: setCustomFields(formData),
updatedBy: userId,
});
if (update.error) {
@@ -69,6 +70,7 @@ export default function CustomerEditRoute() {
customerStatusId: routeData?.customer?.customerStatusId ?? undefined,
accountManagerId: routeData?.customer?.accountManagerId ?? undefined,
taxId: routeData?.customer?.taxId ?? "",
+ ...getCustomFields(routeData?.customer?.customFields),
};
return <CustomerForm key={initialValues.id} initialValues={initialValues} />;
diff --git a/apps/carbon/app/routes/x+/customer+/new.tsx b/apps/carbon/app/routes/x+/customer+/new.tsx
index 701d35d0b7..896d585efe 100644
--- a/apps/carbon/app/routes/x+/customer+/new.tsx
+++ b/apps/carbon/app/routes/x+/customer+/new.tsx
@@ -4,10 +4,11 @@ import { json, redirect } from "@remix-run/node";
import {
CustomerForm,
customerValidator,
- insertCustomer,
+ upsertCustomer,
} from "~/modules/sales";
import { requirePermissions } from "~/services/auth";
import { flash } from "~/services/session.server";
+import { setCustomFields } from "~/utils/form";
import { assertIsPost } from "~/utils/http";
import { path } from "~/utils/path";
import { error } from "~/utils/result";
@@ -29,8 +30,9 @@ export async function action({ request }: ActionFunctionArgs) {
const { id, ...data } = validation.data;
- const createCustomer = await insertCustomer(client, {
+ const createCustomer = await upsertCustomer(client, {
...data,
+ customFields: setCustomFields(formData),
createdBy: userId,
});
if (createCustomer.error) {