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.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 { error, notFound, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { useAction } from "@carbon/query";
import { Button, Heading } from "@carbon/react";
import { redirect } from "@carbon/utils";
import { LuTriangleAlert } from "react-icons/lu";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { data, Link, useLoaderData } from "react-router";
import { getKanban } from "~/modules/inventory/inventory.service";
import {
  getJob,
  updateKanbanJob
} from "~/modules/production/production.service";
import { path } from "~/utils/path";

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {
    role: "employee"
  });

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

  const kanban = await getKanban(client, id, companyId);
  if (
    kanban.data?.replenishmentSystem !== "Make" ||
    !kanban.data?.jobReadableId
  ) {
    // false alarm, this is not a collision
    throw redirect(path.to.api.kanban(id));
  }

  const job = await getJob(client, kanban.data.jobId!);
  if (job.error) {
    return {
      existingJob: null,
      id
    };
  }

  return {
    existingJob: job.data,
    id
  };
}

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

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

  const kanbanUpdate = await updateKanbanJob(client, {
    id,
    jobId: null,
    userId,
    companyId
  });

  if (kanbanUpdate.error) {
    return data(
      { success: false },
      await flash(
        request,
        error(kanbanUpdate.error, "Failed to remove the job from the kanban")
      )
    );
  }

  return redirect(
    path.to.api.kanban(id),
    await flash(request, success("Job removed from the kanban"))
  );
}

export default function KanbanCollisionRoute() {
  const { id, existingJob } = useLoaderData<typeof loader>();
  const fetcher = useAction<typeof action>();

  if (!existingJob) return null;

  return (
    <div className="flex flex-col gap-2 h-screen w-screen items-center justify-center">
      <LuTriangleAlert className="size-12 text-muted-foreground" />
      <Heading size="display">{existingJob?.jobId}</Heading>
      {/* <JobStatus status={existingJob?.status} /> */}
      <p className="text-lg text-muted-foreground max-w-md text-center mx-auto">
        There's already a job for this kanban.
      </p>
      <div className="flex gap-2 py-4">
        <Button size="lg" variant="secondary" asChild>
          <Link to={path.to.api.kanbanJobLink(id)}>View Job</Link>
        </Button>
        <fetcher.Form method="post">
          <Button
            isLoading={fetcher.state !== "idle"}
            isDisabled={fetcher.state !== "idle"}
            variant="destructive"
            size="lg"
            type="submit"
          >
            Remove Job
          </Button>
        </fetcher.Form>
      </div>
    </div>
  );
}