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.7 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 { carbon, respond } from "@carbon/api/carbon.server";
import { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { validator } from "@carbon/form";
import { redirect } from "@carbon/utils";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { useLoaderData, useParams } from "react-router";
import { employeeJobValidator } from "~/modules/people/people.models";
import { getEmployeeJob } from "~/modules/people/people.service";
import { PersonJob } from "~/modules/people/ui/Person";
import { getCustomFields, setCustomFields } from "~/utils/form";
import { path } from "~/utils/path";

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

  const { personId } = params;
  if (!personId) throw new Error("Could not find personId");

  const job = await getEmployeeJob(client, personId, companyId);
  if (job.error) {
    // PGRST116 = no rows: the person has no job record in this company.
    const message =
      job.error.code === "PGRST116"
        ? "This person has no job record in this company"
        : `Failed to load job: ${job.error.message}`;
    throw redirect(
      path.to.people,
      await flash(request, error(job.error, message))
    );
  }

  return {
    job: job.data
  };
}

export async function action(args: ActionFunctionArgs) {
  const input = await validator(employeeJobValidator).request(args, {
    params: ["personId"]
  });
  if ("invalid" in input) return input.invalid;
  const { personId } = input.params;
  const api = await carbon(args.request);
  const result = await api.people.updatePersonJob({
    ...input.data,
    employeeId: personId,
    customFields: input.formData && setCustomFields(input.formData)
  });
  return respond(args.request, result).redirect(path.to.personJob(personId), {
    message: "Successfully updated job"
  });
}

export default function PersonJobRoute() {
  const { job } = useLoaderData<typeof loader>();
  const { personId } = useParams();

  const initialValues = {
    title: job.title ?? "",
    startDate: job.startDate ?? "",
    locationId: job.locationId ?? "",
    shiftId: job.shiftId ?? "",
    departmentId: job.departmentId ?? "",
    managerId: job.managerId ?? "",
    ...getCustomFields(job.customFields)
  };

  return <PersonJob key={personId} initialValues={initialValues} />;
}