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.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 { assertIsPost, error, notFound } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { getLogger } from "@carbon/logger";
import type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";
import { deleteAssemblySubAssembly } from "~/modules/production/production.service";
import { getDatabaseClient } from "~/services/database.server";

const logger = getLogger("erp", "assembly-sub-assembly-delete");

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

  const { id, stepId } = params;
  if (!id) throw notFound("id is not found");
  if (!stepId) throw notFound("step id is not found");

  try {
    await deleteAssemblySubAssembly(getDatabaseClient(), {
      assemblyInstructionId: id,
      headerId: stepId,
      companyId,
      userId
    });
    return { success: true };
  } catch (err) {
    logger.error("Failed to delete sub-assembly", {
      companyId,
      assemblyInstructionId: id,
      headerId: stepId,
      error: err
    });
    return data(
      { success: false },
      await flash(
        request,
        error(
          err,
          err instanceof Error ? err.message : "Failed to delete sub-assembly"
        )
      )
    );
  }
}