emailing user on sign up

This commit is contained in:
John Yeo
2025-01-30 15:06:59 +00:00
parent 8fc0dc079c
commit 2135bd5648
7 changed files with 2262 additions and 260 deletions

View File

@@ -47,6 +47,7 @@ function CustomersView({ env }: { env: AppEnv }) {
const totalPages = Math.ceil((data?.totalCount || 0) / pageSize);
// return <LoadingScreen />;
if (isLoading) {
return <LoadingScreen />;
}

2444
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -46,6 +46,7 @@
"pino-pretty": "^13.0.0",
"postgres": "^3.4.5",
"recaseai": "^0.0.37",
"resend": "^4.1.1",
"stripe": "^17.5.0",
"svix": "^1.45.1",
"ts-node": "^10.9.2",

View File

@@ -12,6 +12,7 @@ export const createClerkCli = () => {
export const createClerkOrg = async (name: string, slug: string) => {
const clerkCli = createClerkCli();
try {
let org = await clerkCli.organizations.createOrganization({
name,

View File

@@ -0,0 +1,23 @@
import { Resend } from "resend";
export const createCli = () => {
return new Resend(process.env.RESEND_API_KEY);
};
export const sendTextEmail = async ({
to,
subject,
body,
}: {
to: string;
subject: string;
body: string;
}) => {
const resend = createCli();
await resend.emails.send({
from: `John <john@${process.env.RESEND_DOMAIN}>`,
to: to,
subject: subject,
text: body,
});
};

View File

@@ -5,6 +5,8 @@ import { Webhook } from "svix";
import { OrgService } from "@/internal/orgs/OrgService.js";
import { handleRequestError } from "@/utils/errorUtils.js";
import { SupabaseClient } from "@supabase/supabase-js";
import { createClerkCli } from "../clerkUtils.js";
import { sendOnboardingEmail } from "./sendOnboardingEmail.js";
const verifyClerkWebhook = async (req: Request, res: Response) => {
const wh = new Webhook(process.env.CLERK_SIGNING_SECRET!);
@@ -92,4 +94,9 @@ const handleOrgCreated = async (sb: SupabaseClient, eventData: any) => {
stripe_config: null,
},
});
await sendOnboardingEmail({
orgId: eventData.id,
clerkCli: createClerkCli(),
});
};

View File

@@ -0,0 +1,45 @@
import { ClerkClient } from "@clerk/express";
import { sendTextEmail } from "../resend/resendUtils.js";
const getWelcomeEmailBody = (userFirstName: string) => {
return `Hey ${userFirstName}!
Noticed you signed up for Autumn :)
Out of curiosity, are you just looking around or do you have a specific use case in mind?
John
Co-founder, Autumn
`;
};
export const sendOnboardingEmail = async ({
orgId,
clerkCli,
}: {
orgId: string;
clerkCli: ClerkClient;
}) => {
const memberships =
await clerkCli.organizations.getOrganizationMembershipList({
organizationId: orgId,
});
for (let membership of memberships.data) {
if (!membership.publicUserData) break;
const user = await clerkCli.users.getUser(membership.publicUserData.userId);
const email = user.primaryEmailAddress?.emailAddress;
if (!email) break;
console.log("Sending onboarding email to", email);
await sendTextEmail({
to: email,
subject: "Anything I can help with?",
body: getWelcomeEmailBody(user.firstName ?? "there"),
});
break;
}
};