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/fix: Personalize invitation emails and fix inviter information (#424)

Ayush Soniba19a3f4f754
diff --git a/apps/erp/app/routes/x+/users+/customers.new.tsx b/apps/erp/app/routes/x+/users+/customers.new.tsx
index 90da10b828..4f994172c4 100644
--- a/apps/erp/app/routes/x+/users+/customers.new.tsx
+++ b/apps/erp/app/routes/x+/users+/customers.new.tsx
@@ -1,4 +1,10 @@
-import { assertIsPost, getAppUrl, RESEND_DOMAIN, success } from "@carbon/auth";
+import {
+ assertIsPost,
+ error,
+ getAppUrl,
+ RESEND_DOMAIN,
+ success
+} from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { InviteEmail } from "@carbon/documents/email";
@@ -43,21 +49,28 @@ export async function action({ request }: ActionFunctionArgs) {
if (!result.success) {
console.error(result);
- throw redirect(path.to.employeeAccounts, await flash(request, result));
+ throw redirect(
+ path.to.customerAccounts,
+ await flash(
+ request,
+ error(result, result.message ?? "Failed to create customer account")
+ )
+ );
}
const location = request.headers.get("x-vercel-ip-city") ?? "Unknown";
const ip = request.headers.get("x-forwarded-for") ?? "127.0.0.1";
- const [company, user] = await Promise.all([
+ const [company, user, invitee] = await Promise.all([
client.from("company").select("name").eq("id", companyId).single(),
- client.from("user").select("email, fullName").eq("id", userId).single()
+ client.from("user").select("email, fullName").eq("id", userId).single(),
+ client.from("user").select("fullName").eq("id", result.userId).single()
]);
if (!company.data || !user.data) {
throw new Error("Failed to load company or user");
}
- const invitationEmail = await sendEmail({
+ await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: result.email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
@@ -69,6 +82,7 @@ export async function action({ request }: ActionFunctionArgs) {
invitedByEmail: user.data.email,
invitedByName: user.data.fullName ?? "",
email: result.email,
+ name: invitee.data?.fullName ?? "",
companyName: company.data.name,
inviteLink: `${getAppUrl()}/invite/${result.code}`,
ip,
diff --git a/apps/erp/app/routes/x+/users+/employees.new.tsx b/apps/erp/app/routes/x+/users+/employees.new.tsx
index 6e12a12b43..c65b7cb4d4 100644
--- a/apps/erp/app/routes/x+/users+/employees.new.tsx
+++ b/apps/erp/app/routes/x+/users+/employees.new.tsx
@@ -72,7 +72,13 @@ export async function action({ request }: ActionFunctionArgs) {
if (!result.success) {
console.error(result);
- throw redirect(path.to.employeeAccounts, await flash(request, result));
+ throw redirect(
+ path.to.employeeAccounts,
+ await flash(
+ request,
+ error(result, result.message ?? "Failed to create employee account")
+ )
+ );
}
const location = request.headers.get("x-vercel-ip-city") ?? "Unknown";
@@ -86,7 +92,7 @@ export async function action({ request }: ActionFunctionArgs) {
throw new Error("Failed to load company or user");
}
- const invitationEmail = await sendEmail({
+ await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
@@ -98,6 +104,7 @@ export async function action({ request }: ActionFunctionArgs) {
invitedByEmail: user.data.email,
invitedByName: user.data.fullName ?? "",
email,
+ name: `${firstName} ${lastName}`.trim(),
companyName: company.data.name,
inviteLink: `${getAppUrl()}/invite/${result.code}`,
ip,
diff --git a/apps/erp/app/routes/x+/users+/resend-invite.tsx b/apps/erp/app/routes/x+/users+/resend-invite.tsx
index a9d6fd0e57..80a2a5684e 100644
--- a/apps/erp/app/routes/x+/users+/resend-invite.tsx
+++ b/apps/erp/app/routes/x+/users+/resend-invite.tsx
@@ -52,7 +52,7 @@ export async function action({ request }: ActionFunctionArgs) {
const invite = await serviceRole
.from("invite")
- .select("code")
+ .select("code, createdBy")
.eq("email", user.data.email)
.eq("companyId", companyId)
.is("acceptedAt", null)
@@ -68,7 +68,13 @@ export async function action({ request }: ActionFunctionArgs) {
);
}
- const invitationEmail = await sendEmail({
+ const inviter = await serviceRole
+ .from("user")
+ .select("email, fullName")
+ .eq("id", invite.data.createdBy)
+ .single();
+
+ await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: user.data.email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
@@ -77,9 +83,10 @@ export async function action({ request }: ActionFunctionArgs) {
},
html: await render(
InviteEmail({
- invitedByEmail: user.data.email,
- invitedByName: user.data.fullName ?? "",
+ invitedByEmail: inviter.data?.email ?? user.data.email,
+ invitedByName: inviter.data?.fullName ?? "",
email: user.data.email,
+ name: user.data.fullName ?? "",
companyName: company.data.name,
inviteLink: `${getAppUrl()}/invite/${invite.data.code}`,
ip,
diff --git a/apps/erp/app/routes/x+/users+/suppliers.new.tsx b/apps/erp/app/routes/x+/users+/suppliers.new.tsx
index bc9618ceda..4d259dee74 100644
--- a/apps/erp/app/routes/x+/users+/suppliers.new.tsx
+++ b/apps/erp/app/routes/x+/users+/suppliers.new.tsx
@@ -1,4 +1,10 @@
-import { assertIsPost, getAppUrl, RESEND_DOMAIN, success } from "@carbon/auth";
+import {
+ assertIsPost,
+ error,
+ getAppUrl,
+ RESEND_DOMAIN,
+ success
+} from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { InviteEmail } from "@carbon/documents/email";
@@ -43,21 +49,28 @@ export async function action({ request }: ActionFunctionArgs) {
if (!result.success) {
console.error(result);
- throw redirect(path.to.employeeAccounts, await flash(request, result));
+ throw redirect(
+ path.to.supplierAccounts,
+ await flash(
+ request,
+ error(result, result.message ?? "Failed to create supplier account")
+ )
+ );
}
const location = request.headers.get("x-vercel-ip-city") ?? "Unknown";
const ip = request.headers.get("x-forwarded-for") ?? "127.0.0.1";
- const [company, user] = await Promise.all([
+ const [company, user, invitee] = await Promise.all([
client.from("company").select("name").eq("id", companyId).single(),
- client.from("user").select("email, fullName").eq("id", userId).single()
+ client.from("user").select("email, fullName").eq("id", userId).single(),
+ client.from("user").select("fullName").eq("id", result.userId).single()
]);
if (!company.data || !user.data) {
throw new Error("Failed to load company or user");
}
- const invitationEmail = await sendEmail({
+ await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: result.email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
@@ -69,6 +82,7 @@ export async function action({ request }: ActionFunctionArgs) {
invitedByEmail: user.data.email,
invitedByName: user.data.fullName ?? "",
email: result.email,
+ name: invitee.data?.fullName ?? "",
companyName: company.data.name,
inviteLink: `${getAppUrl()}/invite/${result.code}`,
ip,
diff --git a/packages/documents/src/email/InviteEmail.tsx b/packages/documents/src/email/InviteEmail.tsx
index e98b4fbeb0..1328b67565 100644
--- a/packages/documents/src/email/InviteEmail.tsx
+++ b/packages/documents/src/email/InviteEmail.tsx
@@ -17,6 +17,7 @@ import {
interface Props {
email?: string;
+ name?: string;
invitedByEmail?: string;
invitedByName?: string;
companyName?: string;
@@ -29,6 +30,7 @@ export const InviteEmail = ({
invitedByEmail = "[email protected]",
invitedByName = "Tom Sawyer",
email = "[email protected]",
+ name,
companyName = "Tombstone",
inviteLink = "https://carbon.ms/invite/1234567890",
ip = "38.38.38.38",
@@ -60,6 +62,13 @@ export const InviteEmail = ({
Join <strong>{companyName}</strong> on <strong>Carbon</strong>
</Heading>
+ <Text
+ className={`text-[14px] leading-[24px] ${themeClasses.text}`}
+ style={{ color: lightStyles.text.color }}
+ >
+ Hi {name ?? ""},
+ </Text>
+
<Text
className={`text-[14px] leading-[24px] ${themeClasses.text}`}
style={{ color: lightStyles.text.color }}
diff --git a/packages/jobs/trigger/user-admin.ts b/packages/jobs/trigger/user-admin.ts
index f138680bd7..2db2d58be2 100644
--- a/packages/jobs/trigger/user-admin.ts
+++ b/packages/jobs/trigger/user-admin.ts
@@ -73,7 +73,7 @@ export const userAdminTask = task({
const invite = await serviceRole
.from("invite")
- .select("code")
+ .select("code, createdBy")
.eq("email", user.data.email)
.eq("companyId", companyId)
.is("acceptedAt", null)
@@ -86,7 +86,13 @@ export const userAdminTask = task({
};
}
- const invitationEmail = await sendEmail({
+ const inviter = await serviceRole
+ .from("user")
+ .select("email, fullName")
+ .eq("id", invite.data.createdBy)
+ .single();
+
+ await sendEmail({
from: `Carbon <no-reply@${RESEND_DOMAIN}>`,
to: user.data.email,
subject: `You have been invited to join ${company.data?.name} on Carbon`,
@@ -95,9 +101,10 @@ export const userAdminTask = task({
},
html: await render(
InviteEmail({
- invitedByEmail: user.data.email,
- invitedByName: user.data.fullName ?? "",
+ invitedByEmail: inviter.data?.email ?? user.data.email,
+ invitedByName: inviter.data?.fullName ?? "",
email: user.data.email,
+ name: user.data.fullName ?? "",
companyName: company.data.name,
inviteLink: `${getAppUrl()}/invite/${invite.data.code}`,
ip,
@@ -106,8 +113,6 @@ export const userAdminTask = task({
),
});
- console.log(invitationEmail);
-
result = {
success: true,
message: `Successfully resent invite for ${payload.id}`,