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.

Reducing dependencies when starting the app

Kartik Bajaj263765c3c925
diff --git a/.env.example b/.env.example
index 495655a89d..49157e92cd 100644
--- a/.env.example
+++ b/.env.example
@@ -43,3 +43,5 @@ ANTHROPIC_API_KEY="sk-ant-***************"
OPENAI_API_KEY="sk-proj-*****************"
GOOGLE_PLACES_API_KEY="*******************"
+
+RATE_LIMIT="5"
\ No newline at end of file
diff --git a/apps/academy/app/routes/_auth+/login.tsx b/apps/academy/app/routes/_auth+/login.tsx
index 86eca15eef..e116978779 100644
--- a/apps/academy/app/routes/_auth+/login.tsx
+++ b/apps/academy/app/routes/_auth+/login.tsx
@@ -3,6 +3,7 @@ import {
carbonClient,
error,
magicLinkValidator,
+ RATE_LIMIT,
} from "@carbon/auth";
import { sendMagicLink, verifyAuthSession } from "@carbon/auth/auth.server";
import { flash, getAuthSession } from "@carbon/auth/session.server";
@@ -50,7 +51,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(5, "1 h"), // 5 submissions per hour
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
analytics: true,
});
diff --git a/apps/erp/app/routes/_public+/login.tsx b/apps/erp/app/routes/_public+/login.tsx
index 03a9f1ffba..db17aa9c3f 100644
--- a/apps/erp/app/routes/_public+/login.tsx
+++ b/apps/erp/app/routes/_public+/login.tsx
@@ -7,6 +7,7 @@ import {
CONTROLLED_ENVIRONMENT,
error,
magicLinkValidator,
+ RATE_LIMIT,
} from "@carbon/auth";
import { sendMagicLink, verifyAuthSession } from "@carbon/auth/auth.server";
import { flash, getAuthSession } from "@carbon/auth/session.server";
@@ -60,7 +61,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(5, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
analytics: true,
});
diff --git a/apps/erp/app/routes/_public+/verify.tsx b/apps/erp/app/routes/_public+/verify.tsx
index acc392715e..fd40d45bc3 100644
--- a/apps/erp/app/routes/_public+/verify.tsx
+++ b/apps/erp/app/routes/_public+/verify.tsx
@@ -1,4 +1,4 @@
-import { assertIsPost, error } from "@carbon/auth";
+import { assertIsPost, error, RATE_LIMIT } from "@carbon/auth";
import {
createEmailAuthAccount,
signInWithEmail,
@@ -59,7 +59,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(5, "1 h"), // 5 submissions per hour
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
analytics: true,
});
diff --git a/apps/erp/app/routes/x+/users+/customers.new.tsx b/apps/erp/app/routes/x+/users+/customers.new.tsx
index 2964e48857..b4072937ae 100644
--- a/apps/erp/app/routes/x+/users+/customers.new.tsx
+++ b/apps/erp/app/routes/x+/users+/customers.new.tsx
@@ -3,7 +3,7 @@ import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { InviteEmail } from "@carbon/documents/email";
import { validationError, validator } from "@carbon/form";
-import { resend } from "@carbon/lib/resend.server";
+import { sendEmail } from "@carbon/lib/resend.server";
import { render } from "@react-email/components";
import type { ActionFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
@@ -61,7 +61,7 @@ export async function action({ request }: ActionFunctionArgs) {
throw new Error("Failed to load company or user");
}
- const invitationEmail = await resend.emails.send({
+ const invitationEmail = await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: result.email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
diff --git a/apps/erp/app/routes/x+/users+/employees.new.tsx b/apps/erp/app/routes/x+/users+/employees.new.tsx
index f11fbc2b24..bfaa9e196c 100644
--- a/apps/erp/app/routes/x+/users+/employees.new.tsx
+++ b/apps/erp/app/routes/x+/users+/employees.new.tsx
@@ -9,7 +9,7 @@ import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { InviteEmail } from "@carbon/documents/email";
import { validationError, validator } from "@carbon/form";
-import { resend } from "@carbon/lib/resend.server";
+import { sendEmail } from "@carbon/lib/resend.server";
import { render } from "@react-email/components";
import { useLoaderData } from "@remix-run/react";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@vercel/remix";
@@ -91,7 +91,7 @@ export async function action({ request }: ActionFunctionArgs) {
throw new Error("Failed to load company or user");
}
- const invitationEmail = await resend.emails.send({
+ const invitationEmail = await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
diff --git a/apps/erp/app/routes/x+/users+/resend-invite.tsx b/apps/erp/app/routes/x+/users+/resend-invite.tsx
index c89a4443ae..3a2197624a 100644
--- a/apps/erp/app/routes/x+/users+/resend-invite.tsx
+++ b/apps/erp/app/routes/x+/users+/resend-invite.tsx
@@ -9,7 +9,7 @@ import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { InviteEmail } from "@carbon/documents/email";
import { validationError, validator } from "@carbon/form";
-import { resend } from "@carbon/lib/resend.server";
+import { sendEmail } from "@carbon/lib/resend.server";
import { render } from "@react-email/components";
import { tasks } from "@trigger.dev/sdk/v3";
import type { ActionFunctionArgs } from "@vercel/remix";
@@ -70,7 +70,7 @@ export async function action({ request }: ActionFunctionArgs) {
);
}
- const invitationEmail = await resend.emails.send({
+ const invitationEmail = await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: user.data.email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
diff --git a/apps/erp/app/routes/x+/users+/suppliers.new.tsx b/apps/erp/app/routes/x+/users+/suppliers.new.tsx
index 81bfbf7f83..6f56cc8498 100644
--- a/apps/erp/app/routes/x+/users+/suppliers.new.tsx
+++ b/apps/erp/app/routes/x+/users+/suppliers.new.tsx
@@ -3,7 +3,7 @@ import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { InviteEmail } from "@carbon/documents/email";
import { validationError, validator } from "@carbon/form";
-import { resend } from "@carbon/lib/resend.server";
+import { sendEmail } from "@carbon/lib/resend.server";
import { render } from "@react-email/components";
import type { ActionFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
@@ -61,7 +61,7 @@ export async function action({ request }: ActionFunctionArgs) {
throw new Error("Failed to load company or user");
}
- const invitationEmail = await resend.emails.send({
+ const invitationEmail = await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: result.email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
diff --git a/apps/mes/app/routes/_public+/login.tsx b/apps/mes/app/routes/_public+/login.tsx
index 932562abd1..8fec5ef0c3 100644
--- a/apps/mes/app/routes/_public+/login.tsx
+++ b/apps/mes/app/routes/_public+/login.tsx
@@ -5,6 +5,7 @@ import {
CONTROLLED_ENVIRONMENT,
error,
magicLinkValidator,
+ RATE_LIMIT,
} from "@carbon/auth";
import { sendMagicLink, verifyAuthSession } from "@carbon/auth/auth.server";
import { flash, getAuthSession } from "@carbon/auth/session.server";
@@ -54,7 +55,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(5, "1 h"), // 5 submissions per hour
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
analytics: true,
});
diff --git a/apps/starter/app/routes/_public+/login.tsx b/apps/starter/app/routes/_public+/login.tsx
index 1c497fc2b2..5272130c3b 100644
--- a/apps/starter/app/routes/_public+/login.tsx
+++ b/apps/starter/app/routes/_public+/login.tsx
@@ -3,6 +3,7 @@ import {
carbonClient,
error,
magicLinkValidator,
+ RATE_LIMIT,
} from "@carbon/auth";
import { sendMagicLink, verifyAuthSession } from "@carbon/auth/auth.server";
import { flash, getAuthSession } from "@carbon/auth/session.server";
@@ -50,7 +51,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(5, "1 h"), // 5 submissions per hour
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
analytics: true,
});
diff --git a/packages/auth/src/config/env.ts b/packages/auth/src/config/env.ts
index d99c4ecb8d..d68ef8e534 100644
--- a/packages/auth/src/config/env.ts
+++ b/packages/auth/src/config/env.ts
@@ -192,6 +192,10 @@ export const CLOUDFLARE_TURNSTILE_SECRET_KEY = getEnv(
"CLOUDFLARE_TURNSTILE_SECRET_KEY",
{ isRequired: false }
);
+export const RATE_LIMIT = parseInt(
+ getEnv("RATE_LIMIT", { isRequired: false, isSecret: false }) || "5",
+ 10
+);
export function getAppUrl() {
if (VERCEL_ENV === "production" || NODE_ENV === "production") {
diff --git a/packages/auth/src/services/verification.server.ts b/packages/auth/src/services/verification.server.ts
index e98fa8ad30..5c4aba032b 100644
--- a/packages/auth/src/services/verification.server.ts
+++ b/packages/auth/src/services/verification.server.ts
@@ -1,6 +1,6 @@
import { VerificationEmail } from "@carbon/documents/email";
import { redis } from "@carbon/kv";
-import { resend } from "@carbon/lib/resend.server";
+import { sendEmail } from "@carbon/lib/resend.server";
import { render } from "@react-email/components";
import { RESEND_DOMAIN } from "../config/env";
@@ -24,12 +24,13 @@ export async function sendVerificationCode(email: string) {
})
);
- const result = await resend.emails.send({
+ const result = await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: email,
subject: "Verify your email address",
html,
});
+ console.log(result);
return !result.error;
} catch (error) {
diff --git a/packages/jobs/trigger/onboard.ts b/packages/jobs/trigger/onboard.ts
index 1aeeef80db..292ff2d8f0 100644
--- a/packages/jobs/trigger/onboard.ts
+++ b/packages/jobs/trigger/onboard.ts
@@ -2,7 +2,7 @@ import { openai } from "@ai-sdk/openai";
import { getCarbonServiceRole, RESEND_DOMAIN } from "@carbon/auth";
import type { Database } from "@carbon/database";
import { GetStartedEmail, WelcomeEmail } from "@carbon/documents/email";
-import { resend } from "@carbon/lib/resend.server";
+import { resend, sendEmail } from "@carbon/lib/resend.server";
import { getSlackClient } from "@carbon/lib/slack.server";
import { getTwentyClient } from "@carbon/lib/twenty.server";
import { render } from "@react-email/components";
@@ -253,7 +253,7 @@ export const onboardTask = task({
);
if (sendOnboardingEmail) {
- await resend.emails.send({
+ await sendEmail({
from,
to: user.data.email,
subject: `Welcome to Carbon`,
@@ -268,7 +268,7 @@ export const onboardTask = task({
await wait.for({ days: 3 });
if (sendOnboardingEmail) {
- await resend.emails.send({
+ await sendEmail({
from,
to: user.data.email,
subject: `Get the most out of Carbon`,
diff --git a/packages/jobs/trigger/user-admin.ts b/packages/jobs/trigger/user-admin.ts
index bead6422c6..cf212dbbe2 100644
--- a/packages/jobs/trigger/user-admin.ts
+++ b/packages/jobs/trigger/user-admin.ts
@@ -10,7 +10,7 @@ import {
} from "@carbon/auth";
import { deactivateUser } from "@carbon/auth/users.server";
import { InviteEmail } from "@carbon/documents/email";
-import { resend } from "@carbon/lib/resend.server";
+import { sendEmail } from "@carbon/lib/resend.server";
import { updateSubscriptionQuantityForCompany } from "@carbon/stripe/stripe.server";
import { Edition } from "@carbon/utils";
import { render } from "@react-email/components";
@@ -86,7 +86,7 @@ export const userAdminTask = task({
};
}
- const invitationEmail = await resend.emails.send({
+ const invitationEmail = await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: user.data.email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
diff --git a/packages/lib/src/resend.server.ts b/packages/lib/src/resend.server.ts
index 295c033026..476e6486e9 100644
--- a/packages/lib/src/resend.server.ts
+++ b/packages/lib/src/resend.server.ts
@@ -1,3 +1,22 @@
+import type {
+ CreateEmailOptions,
+ CreateEmailRequestOptions,
+ CreateEmailResponse,
+} from "resend";
import { Resend } from "resend";
export const resend = new Resend(process.env.RESEND_API_KEY!);
+
+export const sendEmail = async (
+ payload: CreateEmailOptions,
+ options?: CreateEmailRequestOptions
+): Promise<CreateEmailResponse> => {
+ if (process.env.DISABLE_RESEND) {
+ console.log(payload, options);
+ return {
+ error: null,
+ data: null,
+ };
+ }
+ return resend.emails.send(payload, options);
+};