Skip to content

feat(referral): notify referrer by email when code is redeemed#28721

Open
PanAchy wants to merge 1 commit into
anomalyco:devfrom
PanAchy:fix/referral-email
Open

feat(referral): notify referrer by email when code is redeemed#28721
PanAchy wants to merge 1 commit into
anomalyco:devfrom
PanAchy:fix/referral-email

Conversation

@PanAchy
Copy link
Copy Markdown
Contributor

@PanAchy PanAchy commented May 21, 2026

Issue for this PR

Closes #28719

Type of change

  • Bug fix

What does this PR do?

When a new user signs up using a referral link, Referral.createFromAccount successfully creates the referral row but never notifies the referrer. This PR adds a non-blocking email notification to the referrer after successful redemption.

After the DB transaction completes, the inviter workspace admin email is resolved from UserTable + AuthTable. If an email address is found, AWS.sendEmail is called with:

  • Subject: "Someone used your OpenCode referral code"
  • Body: "A friend signed up with your OpenCode referral code. You'll earn $5 in credits when they subscribe."

The send is non-blocking: .catch(console.error) ensures a failed email does not block or fail the referral creation.

// After successful referral transaction:
const inviterEmail = await tx
  .select({ email: AuthTable.subject })
  .from(UserTable)
  .innerJoin(
    AuthTable,
    and(
      eq(AuthTable.accountID, UserTable.accountID),
      eq(AuthTable.provider, "email"),
    ),
  )
  .where(
    and(
      eq(UserTable.workspaceID, code.workspaceID),
      eq(UserTable.role, "admin"),
      isNull(UserTable.timeDeleted),
    ),
  )
  .orderBy(asc(UserTable.timeCreated))
  .then((rows) => rows[0]?.email);

if (inviterEmail)
  await AWS.sendEmail({
    to: inviterEmail,
    subject: "Someone used your OpenCode referral code",
    body: "A friend signed up with your OpenCode referral code. You'll earn $5 in credits when they subscribe.",
  }).catch(console.error);

Only packages/console/core/src/referral.ts is changed. No schema changes, no migrations needed.

How did you verify your code works?

  • Traced AWS.sendEmail usage pattern from user.ts — call signature matches.
  • Confirmed AuthTable.subject holds the email address for provider = "email" rows.
  • Confirmed UserTable.role = "admin" identifies workspace admins.
  • Email send failure is non-blocking and does not affect referral creation.
  • No automated test added for the email send path: requires live DB + SST context. The logic is verifiable by code review.

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No notification email when referral code is used

1 participant