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.3 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, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { getErrorMessage, redirect } from "@carbon/utils";
import type { ActionFunctionArgs } from "react-router";
import {
  deleteContractLineReleasingSalesOrderLine,
  getContractLine
} from "~/modules/sales/sales.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, userId } = await requirePermissions(request, {
    delete: "sales"
  });

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

  const line = await getContractLine(client, lineId, companyId);
  if (
    line.error ||
    line.data.companyId !== companyId ||
    line.data.customerContractId !== id
  ) {
    throw redirect(
      path.to.contractDetails(id),
      await flash(
        request,
        error(null, "This line does not belong to this contract")
      )
    );
  }

  // Refuses a contract that is not a Draft — an Active contract's lines end
  // through Amend — and hands the line's sales-order line back to its order.
  try {
    await deleteContractLineReleasingSalesOrderLine(getDatabaseClient(), {
      companyId,
      userId,
      customerContractId: id,
      customerContractLineId: lineId
    });
  } catch (err) {
    throw redirect(
      path.to.contractLine(id, lineId),
      await flash(
        request,
        error(err, getErrorMessage(err, "Failed to remove line"))
      )
    );
  }

  // Back to where the delete was asked for — unless that was the line's own
  // page, which no longer exists.
  const referrer = requestReferrer(request);
  const back =
    referrer && !referrer.includes(`/${lineId}/`)
      ? referrer
      : path.to.contractDetails(id);
  throw redirect(
    back,
    await flash(request, success("Removed line from the contract"))
  );
}