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.

1.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { requirePermissions } from "@carbon/auth/auth.server";
import type { LoaderFunctionArgs } from "react-router";

// The item's most relevant assembly instruction for the BOP editors' steps-source
// panel: a Published one wins (it can be synced), else the newest Draft/Archived
// (it can be opened/finished), else null (one can be created from the item's model).
export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {
    view: "production"
  });
  const { itemId } = params;
  if (!itemId) throw new Error("Could not find itemId");

  const instructions = await client
    .from("assemblyInstruction")
    .select("id, status, name")
    .eq("itemId", itemId)
    .eq("companyId", companyId)
    .order("updatedAt", { ascending: false, nullsFirst: false });

  const rows = instructions.data ?? [];
  const instruction =
    rows.find((row) => row.status === "Published") ??
    rows.find((row) => row.status === "Draft") ??
    rows[0] ??
    null;

  return { instruction };
}