Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/console/core/src/referral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Billing } from "./billing"
import { LiteData } from "./lite"
import { Subscription } from "./subscription"
import { ulid } from "ulid"
import { AWS } from "./aws"

export namespace Referral {
export const REWARD_AMOUNT = centsToMicroCents(500)
Expand Down Expand Up @@ -289,7 +290,7 @@ export namespace Referral {
const referralCode = normalizeCode(input.referralCode)
if (!referralCode) return

return Database.transaction(async (tx) => {
const inviterEmail = await Database.transaction(async (tx) => {
const code = await tx
.select({ workspaceID: ReferralCodeTable.workspaceID })
.from(ReferralCodeTable)
Expand Down Expand Up @@ -332,7 +333,25 @@ export namespace Referral {
.then((rows) => rows[0])
if (!referral) throw new Error("Referral not created")
if (referral.id !== referralID) throw new Error("Referral already redeemed")

return 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) return

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)
}

export async function completeFromLiteSubscription(input: { workspaceID: string; userID: string }) {
Expand Down
Loading