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: routes for quote assemblies

Brad Barbin7972ff936aff
diff --git a/apps/carbon/app/modules/sales/sales.models.ts b/apps/carbon/app/modules/sales/sales.models.ts
index d262c52730..758981416c 100644
--- a/apps/carbon/app/modules/sales/sales.models.ts
+++ b/apps/carbon/app/modules/sales/sales.models.ts
@@ -89,8 +89,6 @@ export const quotationValidator = withZod(
export const quotationAssemblyValidator = withZod(
z.object({
id: zfd.text(z.string().optional()),
- quoteId: z.string().min(20, { message: "Quote is required" }),
- quoteLineId: z.string().min(20, { message: "Quote line is required" }),
parentAssemblyId: zfd.text(z.string().optional()),
partId: z.string().min(1, { message: "Part is required" }),
description: z.string().min(1, { message: "Description is required" }),
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index f4f453b7c1..17389a6c29 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -10,6 +10,7 @@ import type {
customerShippingValidator,
customerTypeValidator,
customerValidator,
+ quotationAssemblyValidator,
quotationLineValidator,
quotationValidator,
} from "./sales.models";
@@ -263,6 +264,17 @@ export async function getQuotes(
return query;
}
+export async function getQuoteAssembly(
+ client: SupabaseClient<Database>,
+ quoteAssemblyId: string
+) {
+ return client
+ .from("quoteAssembly")
+ .select("*")
+ .eq("id", quoteAssemblyId)
+ .single();
+}
+
export async function getQuoteExternalDocuments(
client: SupabaseClient<Database>,
quoteId: string
@@ -574,6 +586,36 @@ export async function upsertQuote(
}
}
+export async function upsertQuoteAssembly(
+ client: SupabaseClient<Database>,
+ quotationAssembly:
+ | (Omit<TypeOfValidator<typeof quotationAssemblyValidator>, "id"> & {
+ quoteId: string;
+ quoteLineId: string;
+ createdBy: string;
+ })
+ | (Omit<TypeOfValidator<typeof quotationAssemblyValidator>, "id"> & {
+ id: string;
+ quoteId: string;
+ quoteLineId: string;
+ updatedBy: string;
+ })
+) {
+ if ("id" in quotationAssembly) {
+ return client
+ .from("quoteAssembly")
+ .update(sanitize(quotationAssembly))
+ .eq("id", quotationAssembly.id)
+ .select("id")
+ .single();
+ }
+ return client
+ .from("quoteAssembly")
+ .insert([quotationAssembly])
+ .select("id")
+ .single();
+}
+
export async function upsertQuoteLine(
client: SupabaseClient<Database>,
quotationLine:
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationAssemblyForm.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationAssemblyForm.tsx
index 545b4cab0f..fd3f659888 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationAssemblyForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/QuotationAssemblyForm.tsx
@@ -87,18 +87,15 @@ const QuotationAssemblyForm = ({
>
<Card className={cn(!isEditing && "mt-4")}>
<CardHeader>
- <CardTitle>{partData?.partId || "New Assembly"}</CardTitle>
- {!isEditing && (
- <CardDescription>
- A quote assembly is a collection of operations, materials, and
- subassemblies that are used to build a product.
- </CardDescription>
- )}
+ <CardTitle>{isEditing ? partData?.partId : "New Assembly"}</CardTitle>
+ <CardDescription>
+ {isEditing
+ ? partData?.description
+ : "A quote assembly is a collection of operations, materials, and subassemblies that are used to build a product."}
+ </CardDescription>
</CardHeader>
<CardContent>
<Hidden name="id" />
- <Hidden name="quoteId" />
- <Hidden name="quoteLineId" />
<Hidden name="parentAssemblyId" />
<Hidden name="unitOfMeasureCode" value={partData?.uom} />
<VStack>
diff --git a/apps/carbon/app/routes/x+/quote+/$id.lines.$lineId.assembly.$assemblyId.tsx b/apps/carbon/app/routes/x+/quote+/$id.lines.$lineId.assembly.$assemblyId.tsx
new file mode 100644
index 0000000000..51ff93e7ff
--- /dev/null
+++ b/apps/carbon/app/routes/x+/quote+/$id.lines.$lineId.assembly.$assemblyId.tsx
@@ -0,0 +1,100 @@
+import { json, redirect, useLoaderData } from "@remix-run/react";
+import QuotationAssemblyForm from "~/modules/sales/ui/Quotation/QuotationAssemblyForm";
+
+import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";
+import { validationError } from "remix-validated-form";
+import {
+ getQuoteAssembly,
+ quotationAssemblyValidator,
+ upsertQuoteAssembly,
+} from "~/modules/sales";
+import { requirePermissions } from "~/services/auth";
+import { flash } from "~/services/session.server";
+import { assertIsPost } from "~/utils/http";
+import { path } from "~/utils/path";
+import { error } from "~/utils/result";
+
+export async function loader({ request, params }: LoaderFunctionArgs) {
+ const { client } = await requirePermissions(request, {
+ view: "sales",
+ });
+
+ const { id, assemblyId } = params;
+ if (!id) throw new Error("Could not find id");
+ if (!assemblyId) throw new Error("Could not find assemblyId");
+
+ const assembly = await getQuoteAssembly(client, assemblyId);
+ if (assembly.error) {
+ return redirect(
+ path.to.quote(id),
+ await flash(
+ request,
+ error(assembly.error, "Failed to load quote assembly.")
+ )
+ );
+ }
+
+ return json({
+ quoteAssembly: assembly.data,
+ });
+}
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ assertIsPost(request);
+ const { client, userId } = await requirePermissions(request, {
+ create: "sales",
+ });
+
+ const { id: quoteId, lineId: quoteLineId } = params;
+ if (!quoteId) throw new Error("Could not find quoteId");
+ if (!quoteLineId) throw new Error("Could not find quoteLineId");
+
+ const validation = await quotationAssemblyValidator.validate(
+ await request.formData()
+ );
+
+ if (validation.error) {
+ return validationError(validation.error);
+ }
+
+ const { id, ...data } = validation.data;
+
+ const createQuotationAssembly = await upsertQuoteAssembly(client, {
+ quoteId,
+ quoteLineId,
+ ...data,
+ createdBy: userId,
+ });
+
+ if (createQuotationAssembly.error) {
+ return json(
+ path.to.quote(quoteId),
+ await flash(
+ request,
+ error(createQuotationAssembly.error, "Failed to update quote assembly")
+ )
+ );
+ }
+
+ const assemblyId = createQuotationAssembly.data.id;
+ if (assemblyId) {
+ return redirect(path.to.quoteAssembly(quoteId, quoteLineId, assemblyId));
+ }
+}
+
+export default function QuoteAssembly() {
+ const { quoteAssembly } = useLoaderData<typeof loader>();
+
+ const initialValues = {
+ id: quoteAssembly.id,
+ quoteId: quoteAssembly.quoteId,
+ quoteLineId: quoteAssembly.quoteLineId,
+ parentAssemblyId: quoteAssembly.parentAssemblyId ?? undefined,
+ partId: quoteAssembly.partId,
+ description: quoteAssembly.description ?? "",
+ quantityPerParent: quoteAssembly.quantityPerParent,
+ unitOfMeasureCode: quoteAssembly.unitOfMeasureCode ?? "",
+ };
+
+ return <QuotationAssemblyForm initialValues={initialValues} />;
+}
diff --git a/apps/carbon/app/routes/x+/quote+/$id.lines.$lineId.assembly.new.tsx b/apps/carbon/app/routes/x+/quote+/$id.lines.$lineId.assembly.new.tsx
index 3dc2e32022..99d3f09960 100644
--- a/apps/carbon/app/routes/x+/quote+/$id.lines.$lineId.assembly.new.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$id.lines.$lineId.assembly.new.tsx
@@ -1,7 +1,62 @@
-import { useParams } from "@remix-run/react";
+import { json, redirect, useParams } from "@remix-run/react";
import { useUrlParams } from "~/hooks";
import QuotationAssemblyForm from "~/modules/sales/ui/Quotation/QuotationAssemblyForm";
+import type { ActionFunctionArgs } from "@remix-run/node";
+import { validationError } from "remix-validated-form";
+import {
+ quotationAssemblyValidator,
+ upsertQuoteAssembly,
+} from "~/modules/sales";
+import { requirePermissions } from "~/services/auth";
+import { flash } from "~/services/session.server";
+import { assertIsPost } from "~/utils/http";
+import { path } from "~/utils/path";
+import { error } from "~/utils/result";
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ assertIsPost(request);
+ const { client, userId } = await requirePermissions(request, {
+ create: "sales",
+ });
+
+ const { id: quoteId, lineId: quoteLineId } = params;
+ if (!quoteId) throw new Error("Could not find quoteId");
+ if (!quoteLineId) throw new Error("Could not find quoteLineId");
+
+ const validation = await quotationAssemblyValidator.validate(
+ await request.formData()
+ );
+
+ if (validation.error) {
+ return validationError(validation.error);
+ }
+
+ const { id, ...data } = validation.data;
+
+ const createQuotationAssembly = await upsertQuoteAssembly(client, {
+ quoteId,
+ quoteLineId,
+ ...data,
+ createdBy: userId,
+ });
+
+ if (createQuotationAssembly.error) {
+ return json(
+ path.to.quote(quoteId),
+ await flash(
+ request,
+ error(createQuotationAssembly.error, "Failed to create quote assembly")
+ )
+ );
+ }
+
+ const assemblyId = createQuotationAssembly.data.id;
+ if (assemblyId) {
+ return redirect(path.to.quoteAssembly(quoteId, quoteLineId, assemblyId));
+ }
+}
+
export default function NewQuoteAssembly() {
const { id: quoteId, lineId } = useParams();
const [params] = useUrlParams();
diff --git a/apps/carbon/app/routes/x+/quote+/$id.tsx b/apps/carbon/app/routes/x+/quote+/$id.tsx
index 0533743d5e..13f5ee7aff 100644
--- a/apps/carbon/app/routes/x+/quote+/$id.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$id.tsx
@@ -158,6 +158,7 @@ type BillOfMaterialNodeType =
type BillOfMaterialNode = {
id: string;
+ parentId?: string;
label: string;
type: BillOfMaterialNodeType;
children?: BillOfMaterialNode[];
@@ -175,12 +176,13 @@ const data: BillOfMaterialNode[] = [
type: "line",
children: [
{
- id: "cn8g57bau0l38ambhoeg",
+ id: "cn9edn5dq0l1fglkrqig",
+ parentId: "cn9v3tldq0l1lvlkrqmg",
label: "Assemblies",
type: "assemblies",
},
{
- id: "cn8g57bau0l38ambhoeg",
+ id: "cn9edn5dq0l1fglkrqig",
label: "Operations",
type: "operations",
},
@@ -289,6 +291,7 @@ const data: BillOfMaterialNode[] = [
const BillOfMaterialItem = ({
type,
id,
+ parentId,
label,
}: Omit<BillOfMaterialNode, "children">) => {
const { id: quoteId } = useParams();
@@ -296,13 +299,14 @@ const BillOfMaterialItem = ({
switch (type) {
case "assemblies":
+ console.log({ type, id, parentId, label });
return (
<Button
variant="ghost"
className="w-full justify-between text-muted-foreground"
asChild
>
- <Link to={path.to.newQuoteAssembly(quoteId, id)}>
+ <Link to={path.to.newQuoteAssembly(quoteId, id, parentId)}>
<span>{label}</span>
<IoMdAdd />
</Link>
@@ -443,6 +447,7 @@ const BillOfMaterialExplorer = () => {
type={node.type}
id={node.id}
label={node.label}
+ parentId={node.parentId}
/>
</HStack>
{node.children &&
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index f5bc8eebb1..34ea304773 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -276,8 +276,12 @@ export const path = {
newPurchaseOrderLine: (id: string) =>
generatePath(`${x}/purchase-order/${id}/lines/new`),
newQuote: `${x}/quote/new`,
- newQuoteAssembly: (quoteId: string, lineId: string) =>
- generatePath(`${x}/quote/${quoteId}/lines/${lineId}/assembly/new`),
+ newQuoteAssembly: (quoteId: string, lineId: string, parentId?: string) =>
+ generatePath(
+ `${x}/quote/${quoteId}/lines/${lineId}/assembly/new${
+ parentId ? `?parentAssemblyId=${parentId}` : ""
+ }`
+ ),
newQuoteLine: (id: string) => generatePath(`${x}/quote/${id}/new`),
newQuoteLineQuantity: (id: string, lineId: string) =>
generatePath(`${x}/quote/${id}/lines/${lineId}/new`),