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.5 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 { redirect } from "@carbon/utils";
import type { ActionFunctionArgs } from "react-router";
import { deletePayment } from "~/modules/invoicing/invoicing.service";
import { path } from "~/utils/path";

// Action-only route — the delete confirmation modal (ConfirmDelete) posts here.
// The payment table's RLS DELETE policy restricts deletes to Draft payments, so
// a non-draft delete fails at the database; the UI also hides the action.
export async function action({ request, params }: ActionFunctionArgs) {
  assertIsPost(request);
  const { client, companyId } = await requirePermissions(request, {
    delete: "invoicing"
  });

  const { paymentId } = params;
  if (!paymentId) {
    throw redirect(
      path.to.payments,
      await flash(request, error(params, "Failed to get a payment id"))
    );
  }

  const remove = await deletePayment(client, paymentId, companyId);
  if (remove.error) {
    throw redirect(
      path.to.payment(paymentId),
      await flash(request, error(remove.error, "Failed to delete payment"))
    );
  }

  throw redirect(
    path.to.payments,
    await flash(request, success("Successfully deleted payment"))
  );
}