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 { assertIsPost, error, notFound, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { upsertCustomerPortal } from "@carbon/ee/customer-portals.server";
import { requireFeature } from "@carbon/ee/plan.server";
import { validationError, validator } from "@carbon/form";
import { useCloseRoute } from "@carbon/react";
import { redirect } from "@carbon/utils";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { data, useLoaderData } from "react-router";
import { customerPortalValidator } from "~/modules/sales/sales.models";
import CustomerPortalForm from "~/modules/sales/ui/CustomerPortals/CustomerPortalForm.ee";
import { getCustomerPortal } from "~/modules/shared/shared.service";
import { path } from "~/utils/path";

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

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

  const customerPortal = await getCustomerPortal(client, id);

  if (customerPortal.error) {
    throw redirect(
      path.to.customerPortals,
      await flash(
        request,
        error(customerPortal.error, "Failed to get customer portal")
      )
    );
  }

  return {
    customerPortal: customerPortal.data
  };
}

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

  await requireFeature({
    request,
    client,
    companyId,
    feature: "CUSTOMER_PORTALS",
    redirectTo: path.to.customerPortals
  });

  const formData = await request.formData();
  const validation = await validator(customerPortalValidator).validate(
    formData
  );

  if (validation.error) {
    return validationError(validation.error);
  }

  const { id, customerId } = validation.data;
  if (!id) throw new Error("id not found");

  const updateCustomerPortal = await upsertCustomerPortal(client, companyId, {
    id,
    documentType: "Customer",
    documentId: customerId,
    customerId
  });

  if (updateCustomerPortal.error) {
    return data(
      {},
      await flash(
        request,
        error(updateCustomerPortal.error, "Failed to update customer portal")
      )
    );
  }

  throw redirect(
    path.to.customerPortals,
    await flash(request, success("Updated customer portal"))
  );
}

export default function EditCustomerPortalRoute() {
  const { customerPortal } = useLoaderData<typeof loader>();
  const closeRoute = useCloseRoute();

  const initialValues = {
    id: customerPortal.id ?? undefined,
    customerId: customerPortal.customerId ?? ""
  };

  return (
    <CustomerPortalForm
      key={initialValues.id}
      initialValues={initialValues}
      onClose={() => closeRoute()}
    />
  );
}