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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// 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 } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import {
integrations as availableIntegrations,
getIntegrationIdsByRole,
quickInstallConnectors
} from "@carbon/ee";
import { hasIntegrationEarlyAccess } from "@carbon/ee/plan.server";
import { RecordOutlet, toast } from "@carbon/react";
import { redirect } from "@carbon/utils";
import { useLingui } from "@lingui/react/macro";
import { useEffect } from "react";
import type { LoaderFunctionArgs } from "react-router";
import { data, useLoaderData, useSearchParams } from "react-router";
import { getIntegrationError } from "~/modules/settings/integration-errors";
import { isOAuthPopupResult } from "~/modules/settings/oauth-popup";
import { getIntegrationsWithHealth } from "~/modules/settings/settings.service";
import { IntegrationsList } from "~/modules/settings/ui";
import { path } from "~/utils/path";
export const config = {
runtime: "nodejs"
};
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId, email, userId } = await requirePermissions(
request,
{
view: "settings"
}
);
const integrations = await getIntegrationsWithHealth(client, companyId);
if (integrations.error) {
throw redirect(
path.to.settings,
await flash(
request,
error(integrations.error, "Failed to load integrations")
)
);
}
const items = integrations.data.map((i) => {
// Resolve the install mode HERE, server-side, and send only its id. The
// card needs it to show copy that matches what this install actually does;
// shipping the whole `metadata` to the browser to achieve that would hand
// out credential-adjacent state for every integration.
const config = availableIntegrations.find((c) => c.id === i.id) as
| { resolveInstallMode?: (m: unknown) => { id: string } | undefined }
| undefined;
return {
id: i.id!,
active: i.active!,
health: i.health,
healthReason: i.healthReason,
installMode: i.active
? config?.resolveInstallMode?.(i.metadata)?.id
: undefined
};
});
// Which role slots are taken. The database refuses a second active
// integration of a role (migration 20260924133915); this is what stops a
// customer reaching that refusal through the UI.
const activeRoles: Record<string, string | null> = {
accounting: null,
spend: null
};
for (const role of ["accounting", "spend"] as const) {
const ids = new Set<string>(getIntegrationIdsByRole(role));
activeRoles[role] =
items.find((i) => i.active && ids.has(i.id))?.id ?? null;
}
return data({
integrations: items,
activeRoles,
// Decided here so the flag's rules never reach the browser.
hasEarlyAccess: await hasIntegrationEarlyAccess({
companyId,
userId,
email
})
// No OAuth state here. Every OAuth install now starts at
// `api+/integrations.$id.connect`, which issues a SIGNED, browser-bound,
// single-use state — strictly better than the unsigned correlation value this
// used to hand out, and the only place that can know the chosen mode.
});
}
export default function IntegrationsRoute() {
const { integrations, hasEarlyAccess } = useLoaderData<typeof loader>();
const { i18n } = useLingui();
const [searchParams, setSearchParams] = useSearchParams();
const integration = searchParams.get("integration");
const integrationError = searchParams.get("error");
// An integration's OAuth callback can only redirect the browser, so it reports a
// failed connect as `?integration=<id>&error=<code>` and the copy is resolved
// here (see ~/modules/settings/integration-errors).
useEffect(() => {
if (!integration) return;
const failure = getIntegrationError(integration, integrationError);
if (failure) {
toast.error(i18n._(failure.title), {
// Same id for the same failure, so a re-render can't stack duplicates.
id: `${integration}:${integrationError}`,
description: i18n._(failure.description)
});
}
// Consume the params either way — a reload shouldn't replay the toast, and an
// unrecognized code shouldn't linger in the URL.
setSearchParams(
(params) => {
params.delete("integration");
params.delete("error");
return params;
},
{ replace: true, preventScrollReset: true }
);
}, [integration, integrationError, i18n, setSearchParams]);
// A connect popup whose state could not be read cannot name its card, so the
// callback answers to `connection` and no card listener accepts it. Word it here.
useEffect(() => {
const onMessage = (event: MessageEvent) => {
if (event.origin !== window.location.origin) return;
if (!isOAuthPopupResult(event.data)) return;
if (event.data.integration !== "connection" || event.data.ok) return;
const failure = getIntegrationError("connection", event.data.error);
if (failure) {
toast.error(i18n._(failure.title), {
id: `connection:${event.data.error}`,
description: i18n._(failure.description)
});
}
};
window.addEventListener("message", onMessage);
return () => window.removeEventListener("message", onMessage);
}, [i18n]);
return (
<>
<IntegrationsList
integrations={integrations}
// @ts-expect-error TS2322 - TODO: fix type
availableIntegrations={availableIntegrations}
quickInstallConnectors={quickInstallConnectors}
hasEarlyAccess={hasEarlyAccess}
/>
<RecordOutlet />
</>
);
}