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.

17.3 KB
Raw Blame
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.

import { ONSHAPE_CLIENT_ID } from "@carbon/auth";
import { type SVGProps, useEffect, useState } from "react";
import { z } from "zod";
import { defineIntegration } from "../fns";
import { beginOAuthPopup } from "../oauth-popup";
import {
  normalizeOnshapeUrl,
  ONSHAPE_GOVERNMENT_INTEGRATION_ID,
  ONSHAPE_GOVERNMENT_OAUTH_CALLBACK_PATH,
  ONSHAPE_INTEGRATION_ID
} from "./lib/connection";

// Both Onshape integrations (see ./lib/connection) run the same asset sync, so
// they share its toggle, its validation and its backfill action.
const assetSyncSetting = {
  name: "assetSyncEnabled",
  label: "Sync released assets",
  description:
    "Automatically pull released Onshape drawings and CAD models onto matching Carbon items (matched by part number). Off by default.",
  type: "switch" as const,
  required: false,
  value: false
};

// SwitchField posts a literal "true"/"false" string; preprocess explicitly so
// unchecking sticks (z.coerce.boolean would treat "false" as truthy).
const assetSyncEnabledSchema = z
  .preprocess((value) => {
    if (typeof value === "boolean") return value;
    if (value === "true") return true;
    if (value === "false") return false;
    return value;
  }, z.boolean())
  .default(false);

const backfillAction = {
  id: "backfill",
  label: "Backfill released assets",
  description:
    "Pull released Onshape assets onto all matching Carbon items now (link-only). Requires asset sync enabled.",
  endpoint: "/api/integrations/onshape/backfill",
  // Only shown once asset sync is enabled (the backfill route is gated on it
  // too, so hide the button rather than show one that errors).
  enabledWhenSetting: "assetSyncEnabled"
};

// A revoked grant or dead refresh token can only be re-authorized.
export function onshapeReconnectAction(installEndpoint: string) {
  return {
    id: "reconnect",
    label: "Reconnect Onshape",
    description:
      "Sign in to Onshape again to renew Carbon's access. Use this when the connection shows Unhealthy; settings and linked items are kept.",
    endpoint: installEndpoint
  };
}

export const Onshape = defineIntegration({
  name: "Onshape",
  id: ONSHAPE_INTEGRATION_ID,
  active: !!ONSHAPE_CLIENT_ID,
  category: "CAD",
  logo: Logo,
  description:
    "Onshape is a browser-based CAD/PLM software for modern engineering teams. This integration will sync data from Onshape to Carbon.",
  shortDescription: "Sync data from Onshape to Carbon.",
  images: [],
  settings: [assetSyncSetting],
  schema: z.object({
    assetSyncEnabled: assetSyncEnabledSchema
  }),
  actions: [
    onshapeReconnectAction("/api/integrations/onshape/install"),
    backfillAction
  ],
  onClientInstall: async () => {
    const popup = beginOAuthPopup();
    let error = "unexpected";
    try {
      const response = await fetch("/api/integrations/onshape/install");
      const body = (await response.json()) as { url?: string; error?: string };
      if (body?.url) {
        popup.navigate(body.url);
        return;
      }
      error = body?.error ?? error;
    } catch {
      // Falls through to the error redirect.
    }
    popup.close();
    window.location.href = `/x/settings/integrations?integration=${ONSHAPE_INTEGRATION_ID}&error=${error}`;
  }
});

const onshapeUrlSchema = z
  .string()
  .trim()
  .transform((value, ctx) => {
    const url = normalizeOnshapeUrl(value);
    if (!url) {
      ctx.addIssue({ code: "custom", message: "Must be an https:// URL" });
      return z.NEVER;
    }
    return url;
  });

/**
 * Onshape Government (ITAR / FedRAMP) has no public App Store, so Carbon's
 * public app cannot be installed there. The customer's Enterprise admin creates
 * a PRIVATE OAuth app inside their own Enterprise and enters its client here;
 * saving the settings then sends them through that app's consent screen.
 *
 * Always offered: it needs nothing from this instance's environment, since the
 * OAuth client is the customer's own.
 */
export const OnshapeGovernment = defineIntegration({
  name: "Onshape Government",
  id: ONSHAPE_GOVERNMENT_INTEGRATION_ID,
  active: true,
  category: "CAD",
  logo: Logo,
  setupInstructions: GovernmentSetupInstructions,
  description:
    "Onshape Government is the ITAR and FedRAMP edition of Onshape, hosted in AWS GovCloud. It has no public App Store, so this integration connects through a private OAuth app your Enterprise owns. It syncs the same data from Onshape to Carbon as the standard integration.",
  shortDescription:
    "Sync data from Onshape Government to Carbon through a private app.",
  images: [],
  settingGroups: [
    {
      name: "Connection",
      description: "The private OAuth app created in your Onshape Enterprise"
    },
    {
      name: "Sync",
      description:
        "Every API call counts toward your Enterprise's yearly Onshape API limit."
    }
  ],
  settings: [
    {
      name: "baseUrl",
      label: "Onshape URL",
      description:
        "The address your team signs in to Onshape Government at. Each Enterprise has its own.",
      group: "Connection",
      type: "text" as const,
      required: true,
      value: ""
    },
    {
      name: "clientId",
      label: "Client ID",
      description: "Shown on the private OAuth application in Onshape.",
      group: "Connection",
      type: "text" as const,
      required: true,
      value: ""
    },
    {
      name: "clientSecret",
      label: "Client secret",
      description:
        "Onshape shows it once, when the application is created. Stored encrypted; leave empty to keep the saved one.",
      group: "Connection",
      type: "secret" as const,
      required: true,
      value: ""
    },
    { ...assetSyncSetting, group: "Sync" }
  ],
  schema: z.object({
    baseUrl: onshapeUrlSchema,
    clientId: z.string().trim().min(1),
    // Empty keeps the vaulted secret; the settings save requires one on a
    // fresh install.
    clientSecret: z.string(),
    assetSyncEnabled: assetSyncEnabledSchema
  }),
  actions: [backfillAction]
});

function GovernmentSetupInstructions() {
  // The callback is this Carbon instance's own address. Read after mount so the
  // server render and the first client render agree.
  const [origin, setOrigin] = useState("");
  useEffect(() => setOrigin(window.location.origin), []);
  const callbackUrl = `${origin}${ONSHAPE_GOVERNMENT_OAUTH_CALLBACK_PATH}`;
  const launchUrl = `${origin}/x/settings/integrations/${ONSHAPE_GOVERNMENT_INTEGRATION_ID}`;

  return (
    <>
      <p className="text-sm text-muted-foreground">
        1. An Enterprise admin signs in to Onshape Government and opens
        Enterprise settings → Developer → OAuth applications → Create new OAuth
        application. If you would rather not do this yourselves, give Carbon
        support a seat in your Enterprise and we will build it with you.
      </p>
      <p className="mt-3 text-sm text-muted-foreground">
        2. Name it <span className="font-mono">Carbon</span>, give it any unique
        primary format such as <span className="font-mono">com.carbon.erp</span>
        , and choose <strong>Connected Cloud App</strong> as the type.
      </p>
      <p className="mt-3 text-sm text-muted-foreground">
        3. Set the redirect URL to{" "}
        <span className="font-mono break-all">{callbackUrl}</span> and the OAuth
        URL to <span className="font-mono break-all">{launchUrl}</span>.
      </p>
      <p className="mt-3 text-sm text-muted-foreground">
        4. Grant <strong>Application can read your profile information</strong>,{" "}
        <strong>Application can read your documents</strong> and{" "}
        <strong>Application can write to your documents</strong>. Write access
        is only used to export released drawings and models and to subscribe to
        release notifications.
      </p>
      <p className="mt-3 text-sm text-muted-foreground">
        5. Create the application and copy the client ID and secret straight
        away — Onshape shows the secret once. Enter them below with your Onshape
        URL, then save: Carbon sends you to Onshape to approve the connection.
      </p>
      <p className="mt-3 text-sm text-muted-foreground">
        The app is private to your Enterprise, so its API calls count toward
        your Enterprise's yearly Onshape API limit. Browsing documents and
        importing a bill of materials use a few calls each; released asset sync
        uses several per released revision.
      </p>
    </>
  );
}

export function Logo(props: SVGProps<SVGSVGElement>) {
  return (
    <svg
      width="216"
      height="49"
      viewBox="0 0 216 49"
      fill="#40AA1D"
      xmlns="http://www.w3.org/2000/svg"
      {...props}
    >
      <path
        d="M11.2302 24.3207L14.7526 26.3435V21.7777L24.3381 16.1716L20.8735 14.0332L11.2302 19.6971V24.3207Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M20.8735 0.97168L28.8421 5.59527L7.8811 18.0212V28.7132L0.258911 24.263V12.993L20.8735 0.97168Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M26.4168 17.3855L22.8945 19.4083L26.9365 21.7779L26.8788 32.5855L30.4012 30.5627V19.6973L26.4168 17.3855Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M41.6612 36.8622L33.6925 41.6014V17.8477L24.0493 12.1838L32.1335 7.50244L41.6612 13.0507V36.8622Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M24.8578 33.857V29.9269L20.8157 32.2965L11.2302 26.6904V30.6205L20.8734 36.2266L24.8578 33.857Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M0.258911 37.0355V28.2507L20.8735 40.0409L30.3435 34.377L30.4012 43.5086L21.1045 48.9991L0.258911 37.0355Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M68.4544 15.3625C71.3416 15.3625 73.6514 16.3451 75.4414 18.2523C77.116 20.1017 77.9822 22.4713 77.9822 25.3033C77.9822 28.1352 77.116 30.5048 75.4414 32.3542C73.6514 34.2614 71.3416 35.244 68.4544 35.244C65.5672 35.244 63.2575 34.2614 61.4674 32.3542C59.7929 30.5048 58.9267 28.1352 58.9267 25.3033C58.9267 22.4713 59.7929 20.1017 61.4674 18.2523C63.2575 16.3451 65.5672 15.3625 68.4544 15.3625ZM68.4544 32.5854C70.5332 32.5854 72.15 31.8919 73.3627 30.447C74.5175 29.0599 75.095 27.3839 75.095 25.3033C75.095 23.2226 74.5175 21.4888 73.3627 20.1595C72.15 18.7146 70.5332 18.0211 68.4544 18.0211C66.3757 18.0211 64.7588 18.7146 63.5462 20.1595C62.3913 21.5466 61.8139 23.2226 61.8139 25.3033C61.8139 27.3839 62.3913 29.1177 63.5462 30.447C64.7011 31.8919 66.3757 32.5854 68.4544 32.5854Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M84.6805 15.7093V18.7146C85.3734 17.7899 86.2396 17.0386 87.2212 16.3451C88.2028 15.7093 89.3577 15.3625 90.6281 15.3625C91.8984 15.3625 92.9956 15.5359 93.9195 15.8827C94.8434 16.2295 95.6518 16.8074 96.3447 17.501C97.9038 19.0614 98.7122 21.142 98.7122 23.685V34.8972H95.825V23.8006C95.825 22.009 95.3053 20.6219 94.3237 19.5816C93.342 18.5413 92.0139 18.0211 90.4549 18.0211C88.838 18.0211 87.4522 18.5413 86.355 19.6394C85.2002 20.7375 84.6805 22.1245 84.6805 23.8584V34.955H81.7933V15.7093H84.6805Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M114.823 20.853C114.592 18.8302 112.975 17.8477 110.03 17.8477C107.085 17.8477 105.584 18.7724 105.584 20.6218C105.584 21.3731 105.872 21.9511 106.508 22.3557C107.143 22.7602 107.951 23.107 108.875 23.3382C109.799 23.5693 110.838 23.8583 111.993 24.0317C113.09 24.2629 114.13 24.5519 115.111 24.9564C116.035 25.361 116.844 25.8811 117.479 26.5747C118.114 27.2682 118.403 28.1929 118.403 29.4066C118.403 31.2561 117.71 32.7009 116.266 33.6834C114.881 34.666 112.975 35.1861 110.55 35.1861C108.009 35.1861 105.93 34.5504 104.429 33.2789C103.043 32.123 102.292 30.6781 102.177 28.8287H105.18C105.353 30.6203 106.334 31.7762 108.182 32.412C108.875 32.6431 109.51 32.7009 110.146 32.7009C110.781 32.7009 111.416 32.6431 111.993 32.5853C112.571 32.5275 113.09 32.3542 113.668 32.1808C114.881 31.7184 115.516 30.8515 115.516 29.6956C115.516 28.8865 115.227 28.3085 114.592 27.8462C113.957 27.4416 113.148 27.0948 112.224 26.8059C111.3 26.5169 110.261 26.2857 109.106 26.0545C108.009 25.8233 106.97 25.5344 105.988 25.1298C105.064 24.7252 104.256 24.2051 103.62 23.5693C102.985 22.8758 102.697 22.0089 102.697 20.7952C102.697 19.0614 103.332 17.6743 104.602 16.7496C105.872 15.767 107.72 15.3047 110.203 15.3047C112.571 15.3047 114.419 15.8826 115.747 16.9807C116.902 17.9055 117.537 19.1769 117.71 20.7374H114.823V20.853Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M124.928 7.96484V18.6569C125.621 17.7322 126.487 16.9808 127.469 16.2873C128.45 15.6516 129.605 15.3048 130.876 15.3048C132.146 15.3048 133.243 15.4782 134.167 15.8249C135.091 16.1717 135.899 16.7497 136.592 17.4432C138.151 19.0037 138.96 21.0843 138.96 23.6272V34.8394H136.073V23.8006C136.073 22.009 135.553 20.6219 134.571 19.5816C133.59 18.5413 132.261 18.0211 130.702 18.0211C129.086 18.0211 127.7 18.5413 126.603 19.6394C125.448 20.7375 124.928 22.1246 124.928 23.8584V34.955H122.041V7.96484H124.928Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M158.766 31.8341C157.207 34.0881 154.955 35.244 151.894 35.244C149.123 35.244 146.871 34.2614 145.138 32.3542C143.464 30.447 142.598 28.1352 142.598 25.3033C142.598 22.5291 143.464 20.1595 145.138 18.2523C146.871 16.3451 149.18 15.3625 152.068 15.3625C154.955 15.3625 157.207 16.5184 158.766 18.7724V15.6515H161.653V34.8972H158.766V31.8341ZM152.299 32.5854C154.377 32.5854 155.994 31.8919 157.207 30.447C158.362 29.0599 158.939 27.3839 158.939 25.3033C158.939 23.2226 158.362 21.4888 157.207 20.1595C155.994 18.7146 154.377 18.0211 152.299 18.0211C150.278 18.0211 148.603 18.7146 147.333 20.1595C146.12 21.5466 145.543 23.2226 145.543 25.3033C145.543 27.3839 146.12 29.1177 147.333 30.447C148.545 31.8919 150.22 32.5854 152.299 32.5854Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M169.506 18.7724C171.065 16.5184 173.317 15.3625 176.378 15.3625C179.149 15.3625 181.401 16.3451 183.076 18.2523C184.808 20.1595 185.675 22.4713 185.675 25.3033C185.675 28.0774 184.808 30.447 183.076 32.3542C181.344 34.2614 179.034 35.244 176.147 35.244C173.26 35.244 171.008 34.0881 169.448 31.8341V42.5839H166.561V15.7093H169.448V18.7724H169.506ZM176.031 17.9633C173.952 17.9633 172.336 18.6569 171.123 20.1017C169.968 21.4888 169.391 23.1648 169.391 25.2455C169.391 27.3261 169.968 29.0599 171.123 30.3892C172.336 31.8341 173.952 32.5276 176.031 32.5276C178.052 32.5276 179.727 31.8341 180.997 30.3892C182.21 29.0021 182.787 27.3261 182.787 25.2455C182.787 23.1648 182.21 21.431 180.997 20.1017C179.727 18.7146 178.052 17.9633 176.031 17.9633Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M198.032 32.5853C200.63 32.5853 202.478 31.4872 203.575 29.3488H206.751C206.058 31.0827 205.019 32.5275 203.575 33.5679C202.132 34.666 200.284 35.1861 197.974 35.1861C195.087 35.1861 192.719 34.2036 190.929 32.2964C189.139 30.4469 188.273 28.0773 188.273 25.2454C188.273 22.4135 189.139 20.0439 190.814 18.1944C192.604 16.2872 194.914 15.3047 197.801 15.3047C200.688 15.3047 202.998 16.2872 204.788 18.1944C206.462 20.0439 207.329 22.4135 207.329 25.2454V26.4013H191.218C191.449 28.1929 192.142 29.6378 193.354 30.7937C194.567 32.0074 196.126 32.5853 198.032 32.5853ZM197.801 17.9633C196.011 17.9633 194.567 18.5412 193.354 19.6393C192.2 20.6796 191.564 22.0667 191.276 23.7427H204.326C204.095 22.0667 203.402 20.6796 202.247 19.6393C201.034 18.5412 199.591 17.9633 197.801 17.9633Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M212.756 21.4889C211.082 21.4889 209.754 20.1019 209.754 18.4836C209.754 16.8075 211.14 15.4783 212.756 15.4783C214.431 15.4783 215.759 16.8653 215.759 18.4836C215.759 20.1019 214.431 21.4889 212.756 21.4889ZM212.756 15.7094C211.255 15.7094 210.042 16.9231 210.042 18.4258C210.042 19.9285 211.255 21.1422 212.756 21.1422C214.258 21.1422 215.47 19.9285 215.47 18.4258C215.47 16.9231 214.2 15.7094 212.756 15.7094Z"
        fill="#40AA1D"
      ></path>
      <path
        d="M213.045 16.9231C213.334 16.9231 213.565 16.9809 213.738 17.1543C213.911 17.3277 213.969 17.501 213.969 17.79C213.969 17.9634 213.911 18.1368 213.854 18.3102C213.738 18.4836 213.623 18.5414 213.449 18.5991C213.565 18.5991 213.623 18.6569 213.68 18.7147C213.738 18.7725 213.796 18.8303 213.796 18.8881C213.854 18.9459 213.854 19.0037 213.854 19.0615C213.854 19.1193 213.854 19.2349 213.911 19.2927C213.911 19.3505 213.911 19.4661 213.911 19.5239C213.911 19.5817 213.911 19.6972 213.911 19.755C213.911 19.8128 213.969 19.9284 213.969 19.9862C213.969 20.044 214.027 20.1018 214.084 20.1596H213.623C213.565 20.1018 213.565 20.1018 213.565 20.044C213.565 19.9862 213.565 19.9284 213.565 19.8706C213.565 19.8128 213.565 19.6972 213.565 19.6394C213.565 19.5817 213.565 19.4661 213.565 19.4083C213.565 19.3505 213.565 19.2349 213.507 19.1771C213.507 19.1193 213.449 19.0615 213.449 19.0037C213.392 18.9459 213.334 18.8881 213.276 18.8881C213.218 18.8303 213.103 18.8303 213.045 18.8303H212.006V20.2174H211.602V17.0387H213.045V16.9231ZM213.161 18.368C213.276 18.368 213.334 18.3102 213.392 18.2524C213.449 18.1946 213.507 18.1368 213.565 18.079C213.623 18.0212 213.623 17.9056 213.623 17.79C213.623 17.6166 213.565 17.501 213.507 17.3855C213.392 17.2699 213.276 17.2121 213.045 17.2121H212.006V18.3102H212.872C212.987 18.368 213.045 18.368 213.161 18.368Z"
        fill="#40AA1D"
      ></path>
    </svg>
  );
}