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.

3.1 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 { error, notFound, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { getDatabaseErrorMessage, redirect } from "@carbon/utils";
import { useLingui } from "@lingui/react/macro";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { useLoaderData, useNavigate, useParams } from "react-router";
import { ConfirmDelete } from "~/components/Modals";
import {
  deleteMaintenanceSchedule,
  getMaintenanceSchedule
} from "~/modules/resources/resources.service";
import { getParams, path } from "~/utils/path";

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client } = await requirePermissions(request, {
    view: "resources",
    role: "employee"
  });
  const { scheduleId } = params;
  if (!scheduleId) throw notFound("scheduleId not found");

  const schedule = await getMaintenanceSchedule(client, scheduleId);
  if (schedule.error) {
    throw redirect(
      `${path.to.maintenanceSchedules}?${getParams(request)}`,
      await flash(
        request,
        error(schedule.error, "Failed to get maintenance schedule")
      )
    );
  }

  return { schedule: schedule.data };
}

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

  const { scheduleId } = params;
  if (!scheduleId) {
    throw redirect(
      `${path.to.maintenanceSchedules}?${getParams(request)}`,
      await flash(request, error(params, "Failed to get a schedule id"))
    );
  }

  const { error: deleteError } = await deleteMaintenanceSchedule(
    client,
    scheduleId
  );
  if (deleteError) {
    const errorMessage = getDatabaseErrorMessage(
      deleteError,
      "Failed to delete maintenance schedule",
      { referenced: "Schedule has related dispatches, cannot delete" }
    );

    throw redirect(
      `${path.to.maintenanceSchedules}?${getParams(request)}`,
      await flash(request, error(deleteError, errorMessage))
    );
  }

  throw redirect(
    `${path.to.maintenanceSchedules}?${getParams(request)}`,
    await flash(request, success("Successfully deleted maintenance schedule"))
  );
}

export default function DeleteMaintenanceScheduleRoute() {
  const { scheduleId } = useParams();
  const { schedule } = useLoaderData<typeof loader>();
  const navigate = useNavigate();
  const { t } = useLingui();

  if (!schedule) return null;
  if (!scheduleId) throw notFound("scheduleId not found");

  const onCancel = () => navigate(path.to.maintenanceSchedules);
  const name = schedule.name;
  return (
    <ConfirmDelete
      action={path.to.deleteMaintenanceSchedule(scheduleId)}
      name={name}
      text={t`Are you sure you want to delete the maintenance schedule: ${name}? This cannot be undone.`}
      onCancel={onCancel}
    />
  );
}