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.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 { assertIsPost, error, notFound, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { validationError, validator } from "@carbon/form";
import { redirect } from "@carbon/utils";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { data, useLoaderData } from "react-router";
import { CustomerRentalRateForm } from "~/modules/items/ui/Item";
import { customerItemRentalRateValidator } from "~/modules/sales/sales.models";
import {
  getCustomerItemRentalRate,
  upsertCustomerItemRentalRate
} from "~/modules/sales/sales.service";
import { path } from "~/utils/path";

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

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

  const rate = await getCustomerItemRentalRate(client, rateId, companyId);
  if (rate.error || rate.data.itemId !== itemId) {
    throw redirect(
      path.to.partSales(itemId),
      await flash(
        request,
        error(rate.error, "Failed to load customer rental rates")
      )
    );
  }

  return { rate: rate.data };
}

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

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

  // The update writes the URL's item, so a rate of another item must not be
  // reachable through this one.
  const existing = await getCustomerItemRentalRate(client, rateId, companyId);
  if (existing.error || existing.data.itemId !== itemId) {
    throw redirect(
      path.to.partSales(itemId),
      await flash(
        request,
        error(existing.error, "Failed to load customer rental rates")
      )
    );
  }

  const validation = await validator(customerItemRentalRateValidator).validate(
    await request.formData()
  );
  if (validation.error) {
    return validationError(validation.error);
  }

  const update = await upsertCustomerItemRentalRate(client, {
    ...validation.data,
    id: rateId,
    itemId,
    companyId,
    userId
  });
  if (update.error) {
    return data(
      {},
      await flash(
        request,
        error(
          update.error,
          update.error.code === "23505"
            ? "This customer already has rental rates for this item"
            : "Failed to update customer rental rates"
        )
      )
    );
  }

  throw redirect(
    path.to.partSales(itemId),
    await flash(request, success("Updated customer rental rates"))
  );
}

export default function EditCustomerRentalRateRoute() {
  const { rate } = useLoaderData<typeof loader>();

  return (
    <CustomerRentalRateForm
      key={rate.id}
      initialValues={{
        id: rate.id,
        itemId: rate.itemId,
        currencyCode: rate.currencyCode,
        customerId: rate.customerId ?? "",
        customerTypeId: rate.customerTypeId ?? "",
        dayRate: rate.dayRate ?? undefined,
        weekRate: rate.weekRate ?? undefined,
        monthRate: rate.monthRate ?? undefined,
        validFrom: rate.validFrom ?? undefined,
        validTo: rate.validTo ?? undefined,
        notes: rate.notes ?? undefined
      }}
    />
  );
}