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.

13.9 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 { getCarbonServiceRole } from "@carbon/auth/client.server";
import { flash } from "@carbon/auth/session.server";
import { activeJobStatuses } from "@carbon/database";
import { getLogger } from "@carbon/logger";
import type { LoaderFunctionArgs } from "react-router";
import { useLoaderData, useParams } from "react-router";
import { JobOperation } from "~/components/JobOperation";
import { getCompanySettings } from "~/services/inventory.service";
import {
  getAbilityShortfall,
  getBatchMaterialTotals,
  getBatchWorkInstructions,
  getJobByOperationId,
  getJobFiles,
  getJobMakeMethod,
  getJobMaterialsByOperationId,
  getJobMethodBomIdMap,
  getJobOperationBatch,
  getJobOperationById,
  getJobOperationForCompany,
  getJobOperationProcedure,
  getKanbanByJobId,
  getNextIncompleteSerialEntity,
  getNonConformanceActions,
  getProductionEventsForBatch,
  getProductionEventsForJobOperation,
  getProductionQuantitiesForJobOperation,
  getThumbnailPathByItemId,
  getTrackedEntitiesByMakeMethodId,
  getWorkCenter,
  isFirstOperationOfMakeMethod,
  isSerialEntityIncompleteForOperation
} from "~/services/operations.service";
import type { OperationWithDetails } from "~/services/types";

type ExpiredEntityPolicy = "Warn" | "Block" | "BlockWithOverride";

import { redirect } from "@carbon/utils";
import { makeDurations } from "~/utils/durations";
import type { Handle } from "~/utils/handle";
import {
  OUTSIDE_PROCESSING_REFUSAL,
  resolveOperationView
} from "~/utils/operationView";
import { path } from "~/utils/path";

export const handle: Handle = {
  realtime: [
    // One operation of one job, unless it runs in a batch: a batch shows its
    // members' steps, materials and timers, which belong to other jobs.
    {
      table: "job",
      filter: ({ data }) =>
        data?.batch || !data?.job?.id ? undefined : `id=eq.${data?.job?.id}`
    },
    {
      // Every operation of the job: the one before this one gates it.
      table: "jobOperation",
      filter: ({ data }) =>
        data?.batch || !data?.job?.id ? undefined : `jobId=eq.${data?.job?.id}`
    },
    {
      table: "jobMaterial",
      filter: ({ data }) =>
        data?.batch || !data?.job?.id ? undefined : `jobId=eq.${data?.job?.id}`
    },
    {
      table: "jobOperationStep",
      filter: ({ data, params }) =>
        data?.batch || !data?.job?.id
          ? undefined
          : `operationId=eq.${params.operationId}`
    },
    {
      table: "jobOperationStepRecord",
      filter: ({ data, params }) =>
        data?.batch || !data?.job?.id
          ? undefined
          : `operationId=eq.${params.operationId}`
    },
    {
      table: "pickingListLine",
      filter: ({ data }) =>
        data?.batch || !data?.job?.id ? undefined : `jobId=eq.${data?.job?.id}`
    }
  ]
};

const logger = getLogger("mes", "operation");

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { userId, companyId } = await requirePermissions(request, {});

  const { operationId } = params;
  if (!operationId) throw new Error("Operation ID is required");

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

  const serviceRole = await getCarbonServiceRole();

  // Every read below is service-role, so verify the caller-supplied ids belong
  // to this company first (the tracked entity feeds the genealogy lookup).
  const [ownedOperation, ownedEntity] = await Promise.all([
    getJobOperationForCompany(serviceRole, operationId, companyId),
    trackedEntityId
      ? serviceRole
          .from("trackedEntity")
          .select("id")
          .eq("id", trackedEntityId)
          .eq("companyId", companyId)
          .maybeSingle()
      : null
  ]);
  if (!ownedOperation.data || (ownedEntity && !ownedEntity.data)) {
    logger.warn("Operation or tracked entity not found in company", {
      companyId,
      operationId,
      trackedEntityId,
      error: ownedOperation.error ?? ownedEntity?.error
    });
    throw redirect(
      path.to.operations,
      await flash(request, error(null, "Operation not found"))
    );
  }

  let [events, quantities, job, operation] = await Promise.all([
    getProductionEventsForJobOperation(serviceRole, {
      operationId,
      userId
    }),
    getProductionQuantitiesForJobOperation(serviceRole, operationId),
    getJobByOperationId(serviceRole, operationId),
    getJobOperationById(serviceRole, operationId)
  ]);

  if (job.error) {
    throw redirect(
      path.to.operations,
      await flash(request, error(job.error, "Failed to fetch job"))
    );
  }

  if (operation.error) {
    throw redirect(
      path.to.operations,
      await flash(request, error(operation.error, "Failed to fetch operation"))
    );
  }

  if (!job.data.itemId) {
    throw redirect(
      path.to.operations,
      await flash(request, error("Item ID is required", "Failed to fetch item"))
    );
  }

  const op = operation.data?.[0];

  // Subcontracted work runs at the supplier, never on the shop floor. The
  // boards hide it; this refuses a job-view or direct link too.
  if (op?.operationType === "Outside Processing") {
    throw redirect(
      path.to.operations,
      await flash(request, error(null, OUTSIDE_PROCESSING_REFUSAL))
    );
  }

  // Redirect guard (ADR-0005): each view has its own route. Guards only
  // redirect kinds they don't serve, so no loop.
  if (resolveOperationView(op?.operationType) === "assembly") {
    throw redirect(path.to.assembly(operationId) + url.search);
  }
  if (resolveOperationView(op?.operationType) === "inspection") {
    throw redirect(path.to.inspection(operationId) + url.search);
  }

  // Batch membership. get_job_operation_by_id omits jobOperationBatchId, so read
  // it directly. When the op belongs to a batch that is still Active/Completing,
  // the operation view runs in batch mode: it shows the shared batch timer and
  // completes the whole batch. A Completed batch was already re-sliced per member
  // — it renders as a plain operation view.
  const batchMembership = await serviceRole
    .from("jobOperation")
    .select("jobOperationBatchId")
    .eq("id", operationId)
    .single();
  let batch: Awaited<ReturnType<typeof getJobOperationBatch>>["data"] | null =
    null;
  const batchId = batchMembership.data?.jobOperationBatchId ?? null;
  if (batchId) {
    const batchResult = await getJobOperationBatch(
      serviceRole,
      batchId,
      companyId
    );
    // Floor rule: a batched operation is only floor-visible once its batch
    // has been released (Active/Completing). A Planned batch stays off the
    // floor regardless of its members' job statuses.
    if (batchResult.data?.status === "Planned") {
      throw redirect(
        path.to.operations,
        await flash(
          request,
          error(
            null,
            "This operation is part of a batch that has not been released to the floor"
          )
        )
      );
    }
    if (
      batchResult.data &&
      (batchResult.data.status === "Active" ||
        batchResult.data.status === "Completing")
    ) {
      batch = batchResult.data;
      // Read the batch's events (all members' timers) instead of this op's.
      events = await getProductionEventsForBatch(serviceRole, batchId);
    }
  } else if (
    !job.data.status ||
    !(activeJobStatuses as readonly string[]).includes(job.data.status)
  ) {
    // Floor rule: an unbatched operation is only floor-visible while its job
    // is released (Ready/In Progress/Paused).
    throw redirect(
      path.to.operations,
      await flash(
        request,
        error(null, "This operation's job has not been released to the floor")
      )
    );
  }

  const [
    thumbnailPath,
    trackedEntities,
    jobMakeMethod,
    kanban,
    bomIdMap,
    companySettings,
    abilityWarning
  ] = await Promise.all([
    getThumbnailPathByItemId(serviceRole, operation.data?.[0].itemId),
    getTrackedEntitiesByMakeMethodId(
      serviceRole,
      operation.data?.[0].jobMakeMethodId,
      companyId
    ),
    getJobMakeMethod(serviceRole, operation.data?.[0].jobMakeMethodId),
    getKanbanByJobId(serviceRole, job.data.id),
    getJobMethodBomIdMap(serviceRole, job.data.id!),
    getCompanySettings(serviceRole, companyId),
    getAbilityShortfall(serviceRole, {
      operationId,
      employeeId: userId,
      companyId
    })
  ]);

  const inventoryShelfLife = (companySettings.data?.inventoryShelfLife ??
    null) as { expiredEntityPolicy?: ExpiredEntityPolicy } | null;
  const expiredEntityPolicy: ExpiredEntityPolicy =
    inventoryShelfLife?.expiredEntityPolicy ?? "Block";
  const autoSelectMaterialWithoutPickingList =
    companySettings.data?.autoSelectMaterialWithoutPickingList ?? false;

  // Is this the first operation in the routing? A serial unit only earns a
  // printed label when it is completed at its first operation, so:
  //  - first operation: no labels exist yet → the operator flows unit-by-unit
  //    (auto-select here on arrival, and in the client after each completion);
  //  - later operations: every unit already has a label → the operator
  //    scans/selects each unit (no auto-select, here or in the client).
  const isFirstOperation = await isFirstOperationOfMakeMethod(
    serviceRole,
    operationId,
    op.jobMakeMethodId
  );

  // On the first operation only, auto-select the first incomplete unit when none
  // is in the URL. Later operations leave it unset so the client presents the
  // scan/select picker for every unit (including the first one picked up).
  if (
    !trackedEntityId &&
    isFirstOperation &&
    trackedEntities.data &&
    trackedEntities.data.length > 0
  ) {
    const nextTrackedEntity = trackedEntities.data.find((entity) =>
      isSerialEntityIncompleteForOperation(entity, operationId)
    );
    if (nextTrackedEntity) {
      const redirectUrl = new URL(request.url);
      redirectUrl.searchParams.set("trackedEntityId", nextTrackedEntity.id);
      throw redirect(`${redirectUrl.pathname}${redirectUrl.search}`);
    }
  }

  return {
    batch,
    bomIdMap: Object.fromEntries(bomIdMap),
    events: events.data ?? [],
    quantities: (quantities.data ?? []).reduce(
      (acc, curr) => {
        if (curr.type === "Scrap") {
          acc.scrap += curr.quantity;
        } else if (curr.type === "Production") {
          acc.production += curr.quantity;
        } else if (curr.type === "Rework") {
          acc.rework += curr.quantity;
        }
        return acc;
      },
      { scrap: 0, production: 0, rework: 0 }
    ),
    job: job.data,
    jobMakeMethod: jobMakeMethod.data,
    kanban: kanban.data,
    files: getJobFiles(serviceRole, companyId, job.data, operation.data),
    // Batch mode: the combined per-item requirement across every member, so
    // the materials panel can show the one shared pick.
    batchMaterialTotals: batch
      ? await getBatchMaterialTotals(serviceRole, {
          batchId: batch.id as string,
          companyId
        })
      : null,
    // Batch mode: steps, parameters and files across every member (deferred).
    batchWorkInstructions: batch
      ? getBatchWorkInstructions(serviceRole, {
          batchId: batch.id as string,
          companyId
        })
      : null,
    materials: getJobMaterialsByOperationId(serviceRole, {
      operation: operation.data?.[0],
      trackedEntityId:
        trackedEntityId ??
        getNextIncompleteSerialEntity(trackedEntities.data ?? [], operationId)
          ?.id,
      requiresSerialTracking:
        jobMakeMethod.data?.requiresSerialTracking ?? false
    }),
    trackedEntities: trackedEntities.data ?? [],
    isFirstOperation,
    nonConformanceActions: getNonConformanceActions(serviceRole, {
      itemId: operation.data?.[0].itemId,
      processId: operation.data?.[0].processId,
      companyId
    }),
    operation: makeDurations(operation.data?.[0]) as OperationWithDetails,
    abilityWarning,
    expiredEntityPolicy,
    autoSelectMaterialWithoutPickingList,
    procedure: getJobOperationProcedure(serviceRole, operation.data?.[0].id),
    workCenter: getWorkCenter(
      serviceRole,
      operation.data?.[0].workCenterId
    ) as Promise<
      import("@supabase/supabase-js").PostgrestSingleResponse<{
        name: string;
        id: string;
        isBlocked: boolean | null;
        blockingDispatchId: string | null;
        blockingDispatchReadableId: string | null;
      }>
    >,
    thumbnailPath
  };
}

export default function OperationRoute() {
  const { operationId } = useParams();
  if (!operationId) throw new Error("Operation ID is required");

  const {
    abilityWarning,
    batch,
    batchMaterialTotals,
    batchWorkInstructions,
    events,
    expiredEntityPolicy,
    autoSelectMaterialWithoutPickingList,
    files,
    job,
    jobMakeMethod,
    kanban,
    materials,
    operation,
    procedure,
    thumbnailPath,
    trackedEntities,
    isFirstOperation,
    workCenter,
    nonConformanceActions
  } = useLoaderData<typeof loader>();

  return (
    <JobOperation
      key={`job-operation-${operationId}`}
      abilityWarning={abilityWarning}
      batch={batch}
      batchMaterialTotals={batchMaterialTotals}
      batchWorkInstructions={batchWorkInstructions}
      events={events}
      expiredEntityPolicy={expiredEntityPolicy}
      autoSelectMaterialWithoutPickingList={
        autoSelectMaterialWithoutPickingList
      }
      files={files}
      kanban={kanban}
      materials={materials}
      method={jobMakeMethod}
      trackedEntities={trackedEntities}
      isFirstOperation={isFirstOperation}
      nonConformanceActions={nonConformanceActions}
      operation={operation}
      procedure={procedure}
      job={job}
      thumbnailPath={thumbnailPath}
      workCenter={workCenter}
    />
  );
}