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 name field to customerLocation and supplierLocation

Rob Carrington4542b7adacbc
diff --git a/apps/carbon/app/modules/purchasing/purchasing.models.ts b/apps/carbon/app/modules/purchasing/purchasing.models.ts
index 3c96ff83d6..9da62ae753 100644
--- a/apps/carbon/app/modules/purchasing/purchasing.models.ts
+++ b/apps/carbon/app/modules/purchasing/purchasing.models.ts
@@ -183,6 +183,7 @@ export const supplierContactValidator = z.object({
export const supplierLocationValidator = z.object({
id: zfd.text(z.string().optional()),
+ name: zfd.text(z.string()),
...address,
});
diff --git a/apps/carbon/app/modules/purchasing/purchasing.service.ts b/apps/carbon/app/modules/purchasing/purchasing.service.ts
index 8ed4558063..009ab7c933 100644
--- a/apps/carbon/app/modules/purchasing/purchasing.service.ts
+++ b/apps/carbon/app/modules/purchasing/purchasing.service.ts
@@ -492,6 +492,7 @@ export async function insertSupplierLocation(
supplierLocation: {
supplierId: string;
companyId: string;
+ name: string;
address: {
addressLine1?: string;
addressLine2?: string;
@@ -525,6 +526,7 @@ export async function insertSupplierLocation(
{
supplierId: supplierLocation.supplierId,
addressId,
+ name: supplierLocation.name,
customFields: supplierLocation.customFields,
},
])
@@ -643,6 +645,7 @@ export async function updateSupplierLocation(
client: SupabaseClient<Database>,
supplierLocation: {
addressId: string;
+ name: string;
address: {
addressLine1?: string;
addressLine2?: string;
@@ -657,7 +660,10 @@ export async function updateSupplierLocation(
if (supplierLocation.customFields) {
const customFieldUpdate = await client
.from("supplierLocation")
- .update({ customFields: supplierLocation.customFields })
+ .update({
+ name: supplierLocation.name,
+ customFields: supplierLocation.customFields,
+ })
.eq("addressId", supplierLocation.addressId);
if (customFieldUpdate.error) {
diff --git a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
index bd4f132d98..5f634a404c 100644
--- a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
@@ -85,6 +85,7 @@ const SupplierLocationForm = ({
<Hidden name="type" value={type} />
<Hidden name="addressId" />
<VStack spacing={4}>
+ <Input name="name" label="Name" />
<Input name="addressLine1" label="Address Line 1" />
<Input name="addressLine2" label="Address Line 2" />
<Input name="city" label="City" />
diff --git a/apps/carbon/app/modules/sales/sales.models.ts b/apps/carbon/app/modules/sales/sales.models.ts
index 7fe380b172..da8c21e641 100644
--- a/apps/carbon/app/modules/sales/sales.models.ts
+++ b/apps/carbon/app/modules/sales/sales.models.ts
@@ -32,6 +32,7 @@ export const customerContactValidator = z.object({
export const customerLocationValidator = z.object({
id: zfd.text(z.string().optional()),
+ name: zfd.text(z.string()),
...address,
});
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index 0683d223a1..494a818a57 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -906,6 +906,7 @@ export async function insertCustomerLocation(
customerLocation: {
customerId: string;
companyId: string;
+ name: string;
address: {
addressLine1?: string;
addressLine2?: string;
@@ -939,6 +940,7 @@ export async function insertCustomerLocation(
{
customerId: customerLocation.customerId,
addressId,
+ name: customerLocation.name,
customFields: customerLocation.customFields,
},
])
@@ -1121,6 +1123,7 @@ export async function updateCustomerLocation(
client: SupabaseClient<Database>,
customerLocation: {
addressId: string;
+ name: string;
address: {
addressLine1?: string;
addressLine2?: string;
@@ -1135,7 +1138,10 @@ export async function updateCustomerLocation(
if (customerLocation.customFields) {
const customFieldUpdate = await client
.from("customerLocation")
- .update({ customFields: customerLocation.customFields })
+ .update({
+ name: customerLocation.name,
+ customFields: customerLocation.customFields,
+ })
.eq("addressId", customerLocation.addressId);
if (customFieldUpdate.error) {
diff --git a/apps/carbon/app/modules/sales/ui/Customer/CustomerLocationForm.tsx b/apps/carbon/app/modules/sales/ui/Customer/CustomerLocationForm.tsx
index bcaff087ff..b5e1f2d1a5 100644
--- a/apps/carbon/app/modules/sales/ui/Customer/CustomerLocationForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customer/CustomerLocationForm.tsx
@@ -74,6 +74,7 @@ const CustomerLocationForm = ({
<Hidden name="type" value={type} />
<Hidden name="addressId" />
<VStack spacing={4}>
+ <Input name="name" label="Name" />
<Input name="addressLine1" label="Address Line 1" />
<Input name="addressLine2" label="Address Line 2" />
<Input name="city" label="City" />
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.locations.$customerLocationId.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.locations.$customerLocationId.tsx
index 9734f14666..c8f4e5d975 100644
--- a/apps/carbon/app/routes/x+/customer+/$customerId.locations.$customerLocationId.tsx
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.locations.$customerLocationId.tsx
@@ -59,13 +59,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const { id, addressId, ...address } = validation.data;
+ const { id, addressId, name, ...address } = validation.data;
if (addressId === undefined)
throw badRequest("addressId is undefined in form data");
const update = await updateCustomerLocation(client, {
addressId,
+ name,
address,
customFields: setCustomFields(formData),
});
@@ -95,6 +96,7 @@ export default function EditCustomerLocationRoute() {
const initialValues = {
id: location?.id ?? undefined,
addressId: location?.address?.id ?? undefined,
+ name: location?.name ?? undefined,
addressLine1: location?.address?.addressLine1 ?? "",
addressLine2: location?.address?.addressLine2 ?? "",
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.locations.new.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.locations.new.tsx
index 4896a08db5..71dd38d31f 100644
--- a/apps/carbon/app/routes/x+/customer+/$customerId.locations.new.tsx
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.locations.new.tsx
@@ -34,11 +34,12 @@ export async function action({ request, params }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const { id, addressId, ...address } = validation.data;
+ const { id, addressId, name, ...address } = validation.data;
const createCustomerLocation = await insertCustomerLocation(client, {
customerId,
companyId,
+ name,
address,
customFields: setCustomFields(formData),
});
@@ -71,7 +72,9 @@ export default function CustomerLocationsNewRoute() {
const { customerId } = useParams();
if (!customerId) throw new Error("customerId not found");
- const initialValues = {};
+ const initialValues = {
+ name: "", // Ask Brad how to avoid this janky solution
+ };
return (
<CustomerLocationForm
diff --git a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.$supplierLocationId.tsx b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.$supplierLocationId.tsx
index 450d0af8b9..ce3a9ca92b 100644
--- a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.$supplierLocationId.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.$supplierLocationId.tsx
@@ -59,13 +59,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const { id, addressId, ...address } = validation.data;
+ const { id, addressId, name, ...address } = validation.data;
if (addressId === undefined)
throw badRequest("addressId is undefined in form data");
const update = await updateSupplierLocation(client, {
addressId,
+ name,
address,
customFields: setCustomFields(formData),
});
@@ -95,6 +96,7 @@ export default function EditSupplierLocationRoute() {
const initialValues = {
id: location?.id ?? undefined,
addressId: location?.address?.id ?? undefined,
+ name: location?.name ?? undefined,
addressLine1: location?.address?.addressLine1 ?? "",
addressLine2: location?.address?.addressLine2 ?? "",
diff --git a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.new.tsx b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.new.tsx
index 2af8689d77..f25dae254c 100644
--- a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.new.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.new.tsx
@@ -34,11 +34,12 @@ export async function action({ request, params }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const { id, addressId, ...address } = validation.data;
+ const { id, addressId, name, ...address } = validation.data;
const createSupplierLocation = await insertSupplierLocation(client, {
supplierId,
companyId,
+ name,
address,
customFields: setCustomFields(formData),
});
@@ -71,7 +72,9 @@ export default function SupplierLocationsNewRoute() {
const { supplierId } = useParams();
if (!supplierId) throw new Error("supplierId not found");
- const initialValues = {};
+ const initialValues = {
+ name: "", // Ask Brad how to avoid this janky solution
+ };
return (
<SupplierLocationForm
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 05a495755b..90f1602510 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -3329,18 +3329,21 @@ export type Database = {
customerId: string
customFields: Json | null
id: string
+ name: string
}
Insert: {
addressId: string
customerId: string
customFields?: Json | null
id?: string
+ name: string
}
Update: {
addressId?: string
customerId?: string
customFields?: Json | null
id?: string
+ name?: string
}
Relationships: [
{
@@ -17797,18 +17800,21 @@ export type Database = {
addressId: string
customFields: Json | null
id: string
+ name: string
supplierId: string
}
Insert: {
addressId: string
customFields?: Json | null
id?: string
+ name: string
supplierId: string
}
Update: {
addressId?: string
customFields?: Json | null
id?: string
+ name?: string
supplierId?: string
}
Relationships: [
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 05a495755b..90f1602510 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -3329,18 +3329,21 @@ export type Database = {
customerId: string
customFields: Json | null
id: string
+ name: string
}
Insert: {
addressId: string
customerId: string
customFields?: Json | null
id?: string
+ name: string
}
Update: {
addressId?: string
customerId?: string
customFields?: Json | null
id?: string
+ name?: string
}
Relationships: [
{
@@ -17797,18 +17800,21 @@ export type Database = {
addressId: string
customFields: Json | null
id: string
+ name: string
supplierId: string
}
Insert: {
addressId: string
customFields?: Json | null
id?: string
+ name: string
supplierId: string
}
Update: {
addressId?: string
customFields?: Json | null
id?: string
+ name?: string
supplierId?: string
}
Relationships: [
diff --git a/packages/database/supabase/migrations/20240813213122_add-customer-supplier-location-name.sql b/packages/database/supabase/migrations/20240813213122_add-customer-supplier-location-name.sql
new file mode 100644
index 0000000000..162c1e83d6
--- /dev/null
+++ b/packages/database/supabase/migrations/20240813213122_add-customer-supplier-location-name.sql
@@ -0,0 +1,31 @@
+-- Add name column to customerLocation
+-- 1. Add the new column with a temporary default value
+ALTER TABLE public."customerLocation"
+ ADD COLUMN "name" text DEFAULT 'Temporary Name';
+
+-- 2. Update the new column with values from the related "address" table
+UPDATE public."customerLocation" cl
+ SET "name" = a."city"
+ FROM public."address" a
+ WHERE cl."addressId" = a."id";
+
+-- 3. Alter the column to be non-nullable
+ALTER TABLE public."customerLocation"
+ ALTER COLUMN "name" SET NOT NULL,
+ ALTER COLUMN "name" DROP DEFAULT;
+
+-- Add name column to supplierLocation
+-- 1. Add the new column with a temporary default value
+ALTER TABLE public."supplierLocation"
+ ADD COLUMN "name" text DEFAULT 'Temporary Name';
+
+-- 2. Update the new column with values from the related "address" table
+UPDATE public."supplierLocation" sl
+ SET "name" = a."city"
+ FROM public."address" a
+ WHERE sl."addressId" = a."id";
+
+-- 3. Alter the column to be non-nullable
+ALTER TABLE public."supplierLocation"
+ ALTER COLUMN "name" SET NOT NULL,
+ ALTER COLUMN "name" DROP DEFAULT;