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.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 { assertIsPost, 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 type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";
import { getJobExpediteForecast } from "~/modules/production/production.service";
import { getDatabaseClient } from "~/services/database.server";

export async function action({ request, params }: ActionFunctionArgs) {
  assertIsPost(request);
  // Read-only "best case" what-if — no writes happen, so view is sufficient.
  const { companyId, userId } = await requirePermissions(request, {
    view: "production"
  });

  const { jobId } = params;
  if (!jobId) throw new Error("Could not find jobId");

  const expedite = await getJobExpediteForecast(
    getCarbonServiceRole(),
    getDatabaseClient(),
    jobId,
    companyId,
    userId
  );

  if (expedite.error) {
    return data(
      { expedite: null },
      await flash(
        request,
        error(expedite.error, "Failed to compute best case forecast")
      )
    );
  }

  return data({ expedite: expedite.data });
}