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: crud routes for method materials

Brad Barbin8f4e7e5af8b7
diff --git a/apps/carbon/app/modules/items/items.service.ts b/apps/carbon/app/modules/items/items.service.ts
index f0edbb20ef..77933375dc 100644
--- a/apps/carbon/app/modules/items/items.service.ts
+++ b/apps/carbon/app/modules/items/items.service.ts
@@ -18,6 +18,7 @@ import type {
materialFormValidator,
materialSubstanceValidator,
materialValidator,
+ methodMaterialValidator,
methodOperationValidator,
partManufacturingValidator,
partValidator,
@@ -800,6 +801,20 @@ export async function insertShelf(
.single();
}
+export async function updateMaterialOrder(
+ client: SupabaseClient<Database>,
+ updates: {
+ id: string;
+ order: number;
+ updatedBy: string;
+ }[]
+) {
+ const updatePromises = updates.map(({ id, order, updatedBy }) =>
+ client.from("methodMaterial").update({ order, updatedBy }).eq("id", id)
+ );
+ return Promise.all(updatePromises);
+}
+
export async function updateOperationOrder(
client: SupabaseClient<Database>,
updates: {
@@ -1233,6 +1248,36 @@ export async function upsertItemUnitSalePrice(
.eq("itemId", itemUnitSalePrice.itemId);
}
+export async function upsertMethodMaterial(
+ client: SupabaseClient<Database>,
+
+ methodMaterial:
+ | (Omit<z.infer<typeof methodMaterialValidator>, "id"> & {
+ companyId: string;
+ createdBy: string;
+ customFields?: Json;
+ })
+ | (Omit<z.infer<typeof methodMaterialValidator>, "id"> & {
+ id: string;
+ updatedBy: string;
+ customFields?: Json;
+ })
+) {
+ if ("createdBy" in methodMaterial) {
+ return client
+ .from("methodMaterial")
+ .insert([methodMaterial])
+ .select("id")
+ .single();
+ }
+ return client
+ .from("methodMaterial")
+ .update(sanitize(methodMaterial))
+ .eq("id", methodMaterial.id)
+ .select("id")
+ .single();
+}
+
export async function upsertMethodOperation(
client: SupabaseClient<Database>,
diff --git a/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.$id.tsx b/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.$id.tsx
new file mode 100644
index 0000000000..0ab7c8df5a
--- /dev/null
+++ b/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.$id.tsx
@@ -0,0 +1,66 @@
+import { validationError, validator } from "@carbon/remix-validated-form";
+import { json, type ActionFunctionArgs } from "@remix-run/node";
+import { methodMaterialValidator, upsertMethodMaterial } from "~/modules/items";
+import { requirePermissions } from "~/services/auth/auth.server";
+import { flash } from "~/services/session.server";
+import { setCustomFields } from "~/utils/form";
+import { assertIsPost } from "~/utils/http";
+import { error } from "~/utils/result";
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ assertIsPost(request);
+ const { client, companyId, userId } = await requirePermissions(request, {
+ create: "parts",
+ });
+
+ const { makeMethodId, id } = params;
+ if (!makeMethodId) {
+ throw new Error("makeMethodId not found");
+ }
+ if (!id) {
+ throw new Error("id not found");
+ }
+
+ const formData = await request.formData();
+ const validation = await validator(methodMaterialValidator).validate(
+ formData
+ );
+
+ if (validation.error) {
+ return validationError(validation.error);
+ }
+
+ const updateMethodMaterial = await upsertMethodMaterial(client, {
+ ...validation.data,
+ id: id,
+ companyId,
+ updatedBy: userId,
+ customFields: setCustomFields(formData),
+ });
+ if (updateMethodMaterial.error) {
+ return json(
+ {
+ id: null,
+ },
+ await flash(
+ request,
+ error(updateMethodMaterial.error, "Failed to update method material")
+ )
+ );
+ }
+
+ const methodMaterialId = updateMethodMaterial.data?.id;
+ if (!methodMaterialId) {
+ return json(
+ {
+ id: null,
+ },
+ await flash(
+ request,
+ error(updateMethodMaterial, "Failed to update method material")
+ )
+ );
+ }
+
+ return json({ id: methodMaterialId });
+}
diff --git a/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.new.tsx b/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.new.tsx
new file mode 100644
index 0000000000..92d83240b0
--- /dev/null
+++ b/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.new.tsx
@@ -0,0 +1,64 @@
+import { validationError, validator } from "@carbon/remix-validated-form";
+import { json, type ActionFunctionArgs } from "@remix-run/node";
+import { methodMaterialValidator, upsertMethodMaterial } from "~/modules/items";
+import { requirePermissions } from "~/services/auth/auth.server";
+import { flash } from "~/services/session.server";
+import { setCustomFields } from "~/utils/form";
+import { assertIsPost } from "~/utils/http";
+import { error } from "~/utils/result";
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ assertIsPost(request);
+ const { client, companyId, userId } = await requirePermissions(request, {
+ create: "parts",
+ });
+
+ const { makeMethodId } = params;
+ if (!makeMethodId) {
+ throw new Error("makeMethodId not found");
+ }
+
+ const formData = await request.formData();
+ const validation = await validator(methodMaterialValidator).validate(
+ formData
+ );
+
+ if (validation.error) {
+ return validationError(validation.error);
+ }
+
+ const { id, ...data } = validation.data;
+
+ const insertMethodMaterial = await upsertMethodMaterial(client, {
+ ...data,
+ companyId,
+ createdBy: userId,
+ customFields: setCustomFields(formData),
+ });
+ if (insertMethodMaterial.error) {
+ return json(
+ {
+ id: null,
+ },
+ await flash(
+ request,
+ error(insertMethodMaterial.error, "Failed to insert method material")
+ )
+ );
+ }
+
+ const methodMaterialId = insertMethodMaterial.data?.id;
+ if (!methodMaterialId) {
+ return json(
+ {
+ id: null,
+ },
+ await flash(
+ request,
+ error(insertMethodMaterial, "Failed to insert method material")
+ )
+ );
+ }
+
+ return json({ id: methodMaterialId });
+}
diff --git a/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.order.tsx b/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.order.tsx
new file mode 100644
index 0000000000..1e54220170
--- /dev/null
+++ b/apps/carbon/app/routes/x+/items+/methods.$makeMethodId.material.order.tsx
@@ -0,0 +1,42 @@
+import { json, type ActionFunctionArgs } from "@remix-run/node";
+
+import { updateMaterialOrder } from "~/modules/items";
+import { requirePermissions } from "~/services/auth/auth.server";
+import { flash } from "~/services/session.server";
+import { assertIsPost } from "~/utils/http";
+import { error } from "~/utils/result";
+
+export async function action({ request }: ActionFunctionArgs) {
+ assertIsPost(request);
+ const { client, userId } = await requirePermissions(request, {
+ update: "parts",
+ });
+
+ const updateMap = (await request.formData()).get("updates") as string;
+ if (!updateMap) {
+ return json(
+ {},
+ await flash(request, error(null, "Failed to receive a new sort order"))
+ );
+ }
+
+ const updates = Object.entries(JSON.parse(updateMap)).map(
+ ([id, orderString]) => ({
+ id,
+ order: Number(orderString),
+ updatedBy: userId,
+ })
+ );
+
+ const updateSortOrders = await updateMaterialOrder(client, updates);
+ if (updateSortOrders.some((update) => update.error))
+ return json(
+ {},
+ await flash(
+ request,
+ error(updateSortOrders, "Failed to update sort order")
+ )
+ );
+
+ return null;
+}