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.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, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { isForeignKeyViolation, redirect } from "@carbon/utils";
import type { ActionFunctionArgs } from "react-router";
import { deleteItem } from "~/modules/items/items.service";
import { getDatabaseClient } from "~/services/database.server";
import { path, requestReferrer } from "~/utils/path";

export async function action({ request, params }: ActionFunctionArgs) {
  assertIsPost(request);
  const { client, companyId } = await requirePermissions(request, {
    delete: "parts"
  });

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

  const deletion = await deleteItem(
    client,
    getDatabaseClient(),
    itemId,
    companyId
  );
  if (deletion.error) {
    // Postgres FK violations leak schema names ("violates foreign key
    // constraint trackedEntity_itemId_fkey on table trackedEntity").
    // Map the known item FKs (tracked entities, and inventory/cost ledger
    // history) to clear, actionable messages and pass the rest through.
    const message = friendlyDeleteItemError(deletion.error);
    throw redirect(
      requestReferrer(request) ?? path.to.items,
      await flash(request, error(deletion.error, message))
    );
  }

  throw redirect(
    requestReferrer(request) ?? path.to.items,
    await flash(request, success("Successfully deleted item"))
  );
}

function friendlyDeleteItemError(err: { code?: string; message?: string }) {
  if (isForeignKeyViolation(err)) {
    if (err.message?.includes("trackedEntity_itemId_fkey")) {
      return "Item has tracked entities linked to it and cannot be deleted. Deactivate the item instead.";
    }
    if (
      err.message?.includes("itemLedger_itemId_fkey") ||
      err.message?.includes("costLedger_itemId_fkey")
    ) {
      return "Item has inventory transactions or stock on hand and cannot be deleted. Deactivate the item instead.";
    }
    return "Item is still referenced by other records and cannot be deleted. Remove those references or deactivate the item instead.";
  }
  return err.message ?? "Failed to delete item";
}