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.

feat(billing): grant Business-tier access to companies owned by @carbon.ms emails

Sidwebworks9b8251bcfcea
diff --git a/packages/auth/src/services/auth.server.ts b/packages/auth/src/services/auth.server.ts
index b19f326a37..ed056ba395 100644
--- a/packages/auth/src/services/auth.server.ts
+++ b/packages/auth/src/services/auth.server.ts
@@ -19,6 +19,7 @@ import { getCarbonServiceRole } from "../lib/supabase/client.server";
import type { AuthSession } from "../types";
import { path } from "../utils/path";
import { error } from "../utils/result";
+import { isCarbonOwnedCompany } from "./company.server";
import {
destroyAuthSession,
flash,
@@ -274,7 +275,10 @@ export async function requirePermissions(
.eq("id", companyId)
.single();
- if (planData?.planId === Plan.Starter) {
+ if (
+ planData?.planId === Plan.Starter &&
+ !(await isCarbonOwnedCompany(companyId))
+ ) {
throw new Response(
"API access requires the Business plan and above. Please upgrade your plan to use API keys.",
{ status: 403 }
diff --git a/packages/auth/src/services/company.server.ts b/packages/auth/src/services/company.server.ts
index a04b045d56..82eaea8893 100644
--- a/packages/auth/src/services/company.server.ts
+++ b/packages/auth/src/services/company.server.ts
@@ -1,6 +1,7 @@
import { CarbonEdition, DOMAIN } from "@carbon/auth";
-import { Edition } from "@carbon/utils";
+import { Edition, isInternalEmail } from "@carbon/utils";
import * as cookie from "cookie";
+import { getCarbonServiceRole } from "../lib/supabase/client.server";
import { getCookieDomain } from "../utils/cookie";
const cookieName = "companyId";
@@ -28,3 +29,33 @@ export function setCompanyId(companyId: string | null) {
domain: cookieDomain
});
}
+
+/**
+ * True when the company's group owner has a Carbon-internal email. These
+ * companies get top-tier plan access without billing — the runtime analogue of
+ * the `STRIPE_BYPASS_COMPANY_IDS` list, keyed on owner identity instead of a
+ * hardcoded id. Use only as a fallback after the normal plan check, since it
+ * hits the database.
+ */
+export async function isCarbonOwnedCompany(
+ companyId: string
+): Promise<boolean> {
+ const client = getCarbonServiceRole();
+
+ const { data: company } = await client
+ .from("company")
+ .select("companyGroup(ownerId)")
+ .eq("id", companyId)
+ .single();
+
+ const ownerId = company?.companyGroup?.ownerId;
+ if (!ownerId) return false;
+
+ const { data: owner } = await client
+ .from("user")
+ .select("email")
+ .eq("id", ownerId)
+ .single();
+
+ return isInternalEmail(owner?.email);
+}
diff --git a/packages/ee/src/plan.server.ts b/packages/ee/src/plan.server.ts
index d101d7124f..35f8527e01 100644
--- a/packages/ee/src/plan.server.ts
+++ b/packages/ee/src/plan.server.ts
@@ -1,4 +1,5 @@
import { CarbonEdition, error, STRIPE_BYPASS_COMPANY_IDS } from "@carbon/auth";
+import { isCarbonOwnedCompany } from "@carbon/auth/company.server";
import { flash } from "@carbon/auth/session.server";
import type { Database } from "@carbon/database";
import { Edition, normalizePlanId, type Plan } from "@carbon/utils";
@@ -41,7 +42,8 @@ export async function companyHasPlan(
if (isBypassCompany(companyId)) return true;
const current = await getCompanyPlan(client, companyId);
- return planMeetsRequirement(current, resolveRequirement(spec));
+ if (planMeetsRequirement(current, resolveRequirement(spec))) return true;
+ return isCarbonOwnedCompany(companyId);
}
type RequirePlanArgs = {
@@ -67,7 +69,10 @@ export async function requirePlan({
const requirement = resolveRequirement(spec as GateSpec);
const current = await getCompanyPlan(client, companyId);
- if (!planMeetsRequirement(current, requirement)) {
+ if (
+ !planMeetsRequirement(current, requirement) &&
+ !(await isCarbonOwnedCompany(companyId))
+ ) {
throw redirect(
redirectTo,
await flash(
diff --git a/packages/stripe/src/stripe.server.ts b/packages/stripe/src/stripe.server.ts
index 207c2dd5d3..552316ac4b 100644
--- a/packages/stripe/src/stripe.server.ts
+++ b/packages/stripe/src/stripe.server.ts
@@ -1,4 +1,5 @@
import { getCarbonServiceRole } from "@carbon/auth/client.server";
+import { isCarbonOwnedCompany } from "@carbon/auth/company.server";
import type { Database } from "@carbon/database";
import {
CarbonEdition,
@@ -153,6 +154,19 @@ export async function getStripeCustomerByCompanyId(
const customerId = await getStripeCustomerId(companyId);
if (!customerId) {
+ // Carbon-owned companies get Business-tier access without subscribing.
+ if (await isCarbonOwnedCompany(companyId)) {
+ return {
+ subscriptionId: "carbon-owned-subscription",
+ status: "active" as const,
+ priceId: "carbon-owned-price",
+ planId: Plan.Business,
+ currentPeriodStart: Math.floor(Date.now() / 1000),
+ currentPeriodEnd: Math.floor(Date.now() / 1000) + 365 * 24 * 60 * 60,
+ cancelAtPeriodEnd: false,
+ paymentMethod: null
+ };
+ }
return null;
}
const customer = await getStripeCustomer(customerId, companyId);