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.

2.6 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 { ASSEMBLER_SERVICE_API_KEY, ASSEMBLER_SERVICE_URL } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { getCarbonServiceRole } from "@carbon/auth/client.server";
import type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";

// Cancels an in-flight (or stuck) eager optimise — backs the model tab's
// progress Cancel (parity with the ERP api+/model.optimize-cancel route).
// Stamps the row Failed so the viewer settles immediately, and best-effort
// cancels the assembler job so the waiting Inngest run fails non-retriably.
export async function action({ request }: ActionFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {});

  const formData = await request.formData();
  const modelUploadId = formData.get("modelUploadId") as string | null;
  if (!modelUploadId) {
    return data({ success: false }, { status: 400 });
  }

  // Tenant check with the RLS client, then stamp via service role — the
  // RLS UPDATE policy needs an edit permission, and a viewer-only user's
  // cancel would otherwise silently match zero rows (chip spins forever).
  const owned = await client
    .from("modelUpload")
    .select("id")
    .eq("id", modelUploadId)
    .eq("companyId", companyId)
    .maybeSingle();
  if (owned.error || !owned.data) {
    return data({ success: false }, { status: 404 });
  }
  const cancelled = await getCarbonServiceRole()
    .from("modelUpload")
    .update({
      optimizeStatus: "Failed",
      optimizeError: "Cancelled by user"
    })
    .eq("id", modelUploadId)
    .in("optimizeStatus", ["Queued", "Processing"])
    .select("id");

  if (cancelled.error) {
    return data({ success: false }, { status: 500 });
  }

  if (ASSEMBLER_SERVICE_URL && cancelled.data.length > 0) {
    await fetch(
      `${ASSEMBLER_SERVICE_URL}/v1/jobs/optimize-${modelUploadId}/cancel`,
      {
        method: "POST",
        headers: ASSEMBLER_SERVICE_API_KEY
          ? { Authorization: `Bearer ${ASSEMBLER_SERVICE_API_KEY}` }
          : {},
        signal: AbortSignal.timeout(5000)
      }
    ).catch(() => {
      // Best-effort — the row is already stamped Failed. A missed cancel just
      // means the compute finishes anyway and flips the row to Success, which
      // is harmless (the artifact is real).
    });
  }

  return { success: true, cancelled: cancelled.data.length > 0 };
}