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.

5.4 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 { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { useCloseRoute } from "@carbon/react";
import type { BatchRules } from "@carbon/utils";
import { redirect } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { useLoaderData } from "react-router";
import {
  getBatchableProcesses,
  getJobOperationBatchWithMembers
} from "~/modules/production/production.service";
import { BatchBuilder } from "~/modules/production/ui/Batches/BatchBuilder";
import {
  getLocationsList,
  getWorkCentersList
} from "~/modules/resources/resources.service";
import { getUserDefaults } from "~/modules/users/users.service";
import { path } from "~/utils/path";

export async function loader({ request }: LoaderFunctionArgs) {
  // The wizard's mutations submit to batching.update (update: "production") and
  // the batch-operations server fn also requires update: "production"; gate the
  // wizard on the same permission so opening it never dead-ends at submit.
  const { client, companyId, userId } = await requirePermissions(request, {
    update: "production"
  });

  const url = new URL(request.url);
  const batchId = url.searchParams.get("batchId");

  const [processes, locations, workCenters, userDefaults] = await Promise.all([
    getBatchableProcesses(client, companyId),
    getLocationsList(client, companyId),
    getWorkCentersList(client, companyId),
    getUserDefaults(client, userId, companyId)
  ]);

  const defaultLocationId =
    userDefaults.data?.locationId ?? locations.data?.[0]?.id ?? "";

  // Deep-linkable scope (?location=&process=) — validated against the loaded
  // lists so a stale link falls back silently instead of scoping to nothing.
  const paramLocation = url.searchParams.get("location");
  const paramProcess = url.searchParams.get("process");
  const initialLocationId =
    paramLocation && locations.data?.some((l) => l.id === paramLocation)
      ? paramLocation
      : null;
  const initialProcessId =
    paramProcess && processes.data?.some((p) => p.id === paramProcess)
      ? paramProcess
      : null;

  // Add-mode: pre-scope to an existing Planned or Active batch. A Completing/
  // Completed batch can't take members — bounce back to its drawer.
  let batch: {
    id: string;
    readableId: string;
    processId: string;
    locationId: string;
    members: {
      id: string;
      jobReadableId: string | null;
      itemReadableId: string | null;
      description: string | null;
      operationQuantity: number | null;
    }[];
  } | null = null;

  if (batchId) {
    const result = await getJobOperationBatchWithMembers(
      client,
      batchId,
      companyId
    );
    if (result.error || !result.data) {
      throw redirect(
        path.to.operationBatches,
        await flash(request, error(result.error, "Failed to load batch"))
      );
    }
    if (result.data.status !== "Planned" && result.data.status !== "Active") {
      throw redirect(
        path.to.operationBatch(batchId),
        await flash(
          request,
          error(null, "Only a planned or active batch can take more operations")
        )
      );
    }
    batch = {
      id: result.data.id,
      readableId: result.data.readableId,
      processId: result.data.processId,
      locationId: result.data.locationId,
      members: (result.data.members ?? []).map((m) => ({
        id: m.id,
        jobReadableId: m.job?.jobId ?? null,
        itemReadableId: m.jobMakeMethod?.item?.readableIdWithRevision ?? null,
        description: m.description,
        operationQuantity: m.operationQuantity
      }))
    };
  }

  return {
    processes: (processes.data ?? []).map((p) => ({
      id: p.id,
      name: p.name,
      // Drives the review preview's duration model (Sequential Σ vs
      // Simultaneous max) in the builder.
      batchType: p.batchType,
      batchRules: (p.batchRules ?? null) as BatchRules | null
    })),
    locations: (locations.data ?? []).map((l) => ({ id: l.id, name: l.name })),
    workCenters: (workCenters.data ?? [])
      .filter((wc): wc is typeof wc & { id: string; name: string } =>
        Boolean(wc.id && wc.name)
      )
      .map((wc) => ({
        id: wc.id,
        name: wc.name,
        locationId: wc.locationId,
        // Which processes this center can run (workCenterProcess links, from
        // the view) — the builder's picker filters on the scoped process.
        processes: (wc.processes as string[] | null) ?? [],
        batchCapacity: wc.batchCapacity,
        minimumBatchQuantity: wc.minimumBatchQuantity
      })),
    defaultLocationId,
    initialLocationId,
    initialProcessId,
    batch
  };
}

export default function NewBatchRoute() {
  const {
    processes,
    locations,
    workCenters,
    defaultLocationId,
    initialLocationId,
    initialProcessId,
    batch
  } = useLoaderData<typeof loader>();
  const closeRoute = useCloseRoute();

  return (
    <BatchBuilder
      onClose={() => closeRoute()}
      defaultLocationId={defaultLocationId}
      initialLocationId={initialLocationId}
      initialProcessId={initialProcessId}
      locations={locations}
      processes={processes}
      workCenters={workCenters}
      batch={batch}
    />
  );
}