Skip to content

Open source CapCut alternative.

TypeScript

feat(site): a deleted address signs up again without signup credits

David Le8f8d1cc62eb6
diff --git a/site/prisma/Onboarding.prisma b/site/prisma/Onboarding.prisma
index 5811e63b..c78706c7 100644
--- a/site/prisma/Onboarding.prisma
+++ b/site/prisma/Onboarding.prisma
@@ -20,3 +20,17 @@ model UserOnboarding {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
+
+// What stays behind when an account is deleted: a hash of its address, and
+// the credits it was granted and spent. Signup reads it so an address that
+// already received its signup credits starts a second account with none.
+// Rows carry no foreign key; they outlive the user they came from.
+model DeletedAccount {
+ // sha256 of the lowercased address, so the table holds no email.
+ emailHash String @id
+ userId String
+ signedUpAt DateTime
+ deletedAt DateTime @default(now())
+ grantedMicros BigInt @default(0)
+ chargedMicros BigInt @default(0)
+}
diff --git a/site/prisma/migrations/20260906105043/migration.sql b/site/prisma/migrations/20260906105043/migration.sql
new file mode 100644
index 00000000..99ec3871
--- /dev/null
+++ b/site/prisma/migrations/20260906105043/migration.sql
@@ -0,0 +1,11 @@
+-- CreateTable
+CREATE TABLE "DeletedAccount" (
+ "emailHash" TEXT NOT NULL,
+ "userId" TEXT NOT NULL,
+ "signedUpAt" TIMESTAMP(3) NOT NULL,
+ "deletedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "grantedMicros" BIGINT NOT NULL DEFAULT 0,
+ "chargedMicros" BIGINT NOT NULL DEFAULT 0,
+
+ CONSTRAINT "DeletedAccount_pkey" PRIMARY KEY ("emailHash")
+);
diff --git a/site/src/emails/welcome.tsx b/site/src/emails/welcome.tsx
index bc8030b8..2e95f575 100644
--- a/site/src/emails/welcome.tsx
+++ b/site/src/emails/welcome.tsx
@@ -23,7 +23,8 @@ import { DonkeyMark } from "./_components/DonkeyMark";
type WelcomeEmailProps = {
name: string;
- credits: string;
+ // Null when the account received no signup credits.
+ credits: string | null;
unsubscribeUrl: string;
};
@@ -60,10 +61,12 @@ export default function WelcomeEmail({
The editor will always have a free version, and the entire
project is open source.
</Text>
- <Text className="text-[15px] leading-relaxed">
- I also added ${credits} in AI credits to your account so you can
- try AI Chat or generate videos, images, audio, and more.
- </Text>
+ {credits !== null && (
+ <Text className="text-[15px] leading-relaxed">
+ I also added ${credits} in AI credits to your account so you
+ can try AI Chat or generate videos, images, audio, and more.
+ </Text>
+ )}
<Text className="text-[15px] leading-relaxed">
If you enjoy using Donkey Cut, I&apos;d really appreciate a ⭐ on
GitHub. If you have questions, feedback, or just want to say hi,
diff --git a/site/src/lib/email/send-welcome.ts b/site/src/lib/email/send-welcome.ts
index f88c77bd..0e1b5be0 100644
--- a/site/src/lib/email/send-welcome.ts
+++ b/site/src/lib/email/send-welcome.ts
@@ -14,14 +14,19 @@ import {
unsubscribeActionUrl,
unsubscribePageUrl,
} from "@/lib/email/unsubscribe";
-import { signupAppCredits } from "@/lib/onboarding/sequence";
import { prisma } from "@/lib/prisma";
// Sends the one-time welcome email. Two layers keep it single-send: the row
// claim below is the permanent record (only the caller whose conditional
// update flips the null actually sends), and Resend's idempotency key covers a
// crash between claim release and retry for 24 hours.
-export async function sendWelcomeEmail(user: EmailUser): Promise<void> {
+// `credits` is the amount the signup grant landed, or null when the address
+// had already received it on an earlier account; the email names it only when
+// it is there.
+export async function sendWelcomeEmail(
+ user: EmailUser,
+ credits: string | null,
+): Promise<void> {
if (!isResendConfigured()) {
console.log("[email] RESEND_API_KEY not set; skipping welcome email", {
userId: user.id,
@@ -59,7 +64,7 @@ export async function sendWelcomeEmail(user: EmailUser): Promise<void> {
replyTo: emailFrom() || from,
subject: "Thanks for signing up ❤️",
react: WelcomeEmail({
- credits: signupAppCredits,
+ credits,
name: firstName,
unsubscribeUrl: unsubscribePageUrl(user.id),
}),
diff --git a/site/src/lib/jobs/delete-user.ts b/site/src/lib/jobs/delete-user.ts
index 9b3335df..65b59595 100644
--- a/site/src/lib/jobs/delete-user.ts
+++ b/site/src/lib/jobs/delete-user.ts
@@ -7,6 +7,7 @@ import { deletePrefix, INFERENCE_PREFIX, R2NotConfiguredError } from "@/cut/serv
import { getStripe, StripeNotConfiguredError } from "@/lib/billing/stripe";
import { getResend, isResendConfigured } from "@/lib/email/resend";
import { defineJob, JobFailure } from "@/lib/jobs/registry";
+import { deletedAccountKey } from "@/lib/onboarding/deleted-account";
import { prisma } from "@/lib/prisma";
// Deletes an account and everything it owns. Order matters: the Stripe
@@ -15,9 +16,11 @@ import { prisma } from "@/lib/prisma";
// contact, then the Cut cloud content — per-project and per-asset cascades,
// which also clear R2 objects and HLS ladder records — then the user-keyed
// rows that carry no foreign key, an R2 sweep of anything left under the
-// user's prefixes, and finally the user row, whose FK cascades take sessions,
-// accounts, credits, billing, onboarding, and email settings. Every step is
-// idempotent, so a queue redelivery resumes where the last attempt stopped.
+// user's prefixes, then the record the address leaves behind so a second
+// signup under it gets no signup credits, and finally the user row, whose FK
+// cascades take sessions, accounts, credits, billing, onboarding, and email
+// settings. Every step is idempotent, so a queue redelivery resumes where the
+// last attempt stopped.
export const deleteUserJob = defineJob(
z.object({ email: z.string().trim().email() }),
async ({ email }) => {
@@ -87,6 +90,23 @@ export const deleteUserJob = defineJob(
if (!(e instanceof R2NotConfiguredError)) throw e;
}
+ const credits = await prisma.userCreditAccount.findUnique({
+ where: { userId: user.id },
+ select: { lifetimeGrantedMicros: true, lifetimeChargedMicros: true },
+ });
+ const emailHash = deletedAccountKey(user.email);
+ const record = {
+ userId: user.id,
+ signedUpAt: user.createdAt,
+ grantedMicros: credits?.lifetimeGrantedMicros ?? BigInt(0),
+ chargedMicros: credits?.lifetimeChargedMicros ?? BigInt(0),
+ };
+ await prisma.deletedAccount.upsert({
+ where: { emailHash },
+ create: { emailHash, ...record },
+ update: record,
+ });
+
await prisma.user.delete({ where: { id: user.id } });
return {
diff --git a/site/src/lib/onboarding/deleted-account.ts b/site/src/lib/onboarding/deleted-account.ts
new file mode 100644
index 00000000..5d187aa1
--- /dev/null
+++ b/site/src/lib/onboarding/deleted-account.ts
@@ -0,0 +1,19 @@
+import { createHash } from "node:crypto";
+
+import { prisma } from "@/lib/prisma";
+
+// The key a deleted account leaves behind. better-auth lowercases every
+// address it stores, so the same Google account hashes the same on its way
+// back in.
+export function deletedAccountKey(email: string): string {
+ return createHash("sha256").update(email.trim().toLowerCase()).digest("hex");
+}
+
+/** Whether an account under this address was deleted before. */
+export async function isDeletedAddress(email: string): Promise<boolean> {
+ const row = await prisma.deletedAccount.findUnique({
+ where: { emailHash: deletedAccountKey(email) },
+ select: { emailHash: true },
+ });
+ return row !== null;
+}
diff --git a/site/src/lib/onboarding/signup-grants.ts b/site/src/lib/onboarding/signup-grants.ts
index ea6222c1..5b53c45e 100644
--- a/site/src/lib/onboarding/signup-grants.ts
+++ b/site/src/lib/onboarding/signup-grants.ts
@@ -5,6 +5,7 @@ import { grantCredits } from "@/lib/credits/inference";
import { type EmailUser } from "@/lib/email/resend";
import { sendWelcomeEmail } from "@/lib/email/send-welcome";
import { syncResendContact } from "@/lib/email/sync-contact";
+import { isDeletedAddress } from "@/lib/onboarding/deleted-account";
import { signupAppCredits } from "@/lib/onboarding/sequence";
// Single source of truth for what a new account starts with. All steps are
@@ -13,13 +14,23 @@ import { signupAppCredits } from "@/lib/onboarding/sequence";
// double-sending. The grant amounts are in sequence.ts, which the welcome
// slides and the welcome email import too.
export async function provisionSignupGrants(user: EmailUser): Promise<void> {
+ // An address that was deleted already had its signup credits once. The
+ // starter project, fonts and welcome still land; the credits do not, and
+ // the welcome email leaves them out.
+ const returning = await isDeletedAddress(user.email);
+ if (returning) {
+ console.info("[signup-grants] address returned after deletion; signup credits withheld", {
+ userId: user.id,
+ });
+ }
+ const credits = returning ? null : signupAppCredits;
// Settle the steps independently: one failing must not block the others,
// and signup itself must never fail because a bonus grant hiccupped.
const results = await Promise.allSettled([
- grantSignupAppCredits(user.id),
+ credits === null ? Promise.resolve() : grantSignupAppCredits(user.id, credits),
seedStarterProject(user.id),
seedFontsFolder(user.id),
- sendWelcomeEmail(user),
+ sendWelcomeEmail(user, credits),
syncResendContact(user),
]);
@@ -33,11 +44,11 @@ export async function provisionSignupGrants(user: EmailUser): Promise<void> {
}
}
-async function grantSignupAppCredits(userId: string): Promise<void> {
+async function grantSignupAppCredits(userId: string, credits: string): Promise<void> {
// grantCredits dedupes on (source, sourceId, userId), so this is a no-op on
// re-run.
await grantCredits({
- amountMicros: creditStringToMicros(signupAppCredits),
+ amountMicros: creditStringToMicros(credits),
description: "Signup bonus credits",
source: "signup",
sourceId: `signup-app-credit:${userId}`,