Skip to content

Commit 177d2ff

Browse files
Brandon ChenBrandon Chen
authored andcommitted
fix: build errors
1 parent bcb4331 commit 177d2ff

File tree

6 files changed

+119
-105
lines changed

6 files changed

+119
-105
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { eq, sql, or } from 'drizzle-orm'
2+
import { NextResponse } from 'next/server'
3+
import { CREDITS_REFERRAL_BONUS } from 'common/constants'
4+
import db from 'common/db'
5+
import * as schema from 'common/db/schema'
6+
import { hasMaxedReferrals } from 'common/util/server/referral'
7+
8+
export async function redeemReferralCode(referralCode: string, userId: string) {
9+
try {
10+
// Check if the user has already used a referral code
11+
const existingReferral = await db
12+
.select()
13+
.from(schema.referral)
14+
.where(eq(schema.referral.referred_id, userId))
15+
.limit(1)
16+
17+
if (existingReferral.length > 0) {
18+
return NextResponse.json(
19+
{ error: 'You have already used a referral code' },
20+
{ status: 429 }
21+
)
22+
}
23+
24+
// Check if the user is trying to use their own referral code
25+
const currentUser = await db
26+
.select({ referral_code: schema.user.referral_code })
27+
.from(schema.user)
28+
.where(eq(schema.user.id, userId))
29+
.limit(1)
30+
31+
if (currentUser[0]?.referral_code === referralCode) {
32+
return NextResponse.json(
33+
{
34+
error: "Nice try bud, you can't use your own referral code",
35+
},
36+
{
37+
status: 400,
38+
}
39+
)
40+
}
41+
42+
// Find the referrer user
43+
const referrers = await db
44+
.select()
45+
.from(schema.user)
46+
.where(eq(schema.user.referral_code, referralCode))
47+
.limit(1)
48+
49+
if (referrers.length !== 1) {
50+
return NextResponse.json(
51+
{ error: 'Invalid referral code' },
52+
{ status: 400 }
53+
)
54+
}
55+
const referrer = referrers[0]
56+
57+
// Check if the referrer has maxed out their referrals
58+
const referralStatus = await hasMaxedReferrals(referrer.id)
59+
if (referralStatus.reason) {
60+
return NextResponse.json(
61+
{ error: referralStatus.reason },
62+
{ status: 400 }
63+
)
64+
}
65+
66+
await db.transaction(async (tx) => {
67+
// Create the referral
68+
await tx.insert(schema.referral).values({
69+
referrer_id: referrer.id,
70+
referred_id: userId,
71+
status: 'completed',
72+
credits: CREDITS_REFERRAL_BONUS,
73+
created_at: new Date(),
74+
completed_at: new Date(),
75+
})
76+
77+
// Update both users' quota
78+
await tx
79+
.update(schema.user)
80+
.set({
81+
quota: sql<number>`${schema.user.quota} + ${CREDITS_REFERRAL_BONUS}`,
82+
})
83+
.where(or(eq(schema.user.id, referrer.id), eq(schema.user.id, userId)))
84+
})
85+
86+
return NextResponse.json(
87+
{
88+
credits_redeemed: CREDITS_REFERRAL_BONUS,
89+
},
90+
{
91+
status: 200,
92+
}
93+
)
94+
} catch (error) {
95+
console.error('Error applying referral code:', error)
96+
return NextResponse.json(
97+
{ error: 'Internal Server Error' },
98+
{ status: 500 }
99+
)
100+
}
101+
}

web/src/app/api/referrals/route.ts

Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { NextResponse } from 'next/server'
22
import { getServerSession } from 'next-auth'
3-
import { eq, sql, or } from 'drizzle-orm'
3+
import { eq } from 'drizzle-orm'
44
import { z } from 'zod'
55
import { authOptions } from '../auth/[...nextauth]/auth-options'
66
import db from 'common/db'
77
import * as schema from 'common/db/schema'
8-
import { CREDITS_REFERRAL_BONUS, MAX_REFERRALS } from 'common/constants'
9-
import { hasMaxedReferrals } from 'common/util/server/referral'
8+
import { MAX_REFERRALS } from 'common/constants'
9+
import { redeemReferralCode } from './helpers'
1010

1111
type Referral = Pick<typeof schema.user.$inferSelect, 'id' | 'name' | 'email'> &
1212
Pick<typeof schema.referral.$inferSelect, 'credits'>
@@ -124,96 +124,5 @@ export async function POST(request: Request) {
124124
}
125125

126126
const { referralCode } = await request.json()
127-
try {
128-
// Check if the user has already used a referral code
129-
const existingReferral = await db
130-
.select()
131-
.from(schema.referral)
132-
.where(eq(schema.referral.referred_id, userId))
133-
.limit(1)
134-
135-
if (existingReferral.length > 0) {
136-
return NextResponse.json(
137-
{ error: 'You have already used a referral code' },
138-
{ status: 429 }
139-
)
140-
}
141-
142-
// Check if the user is trying to use their own referral code
143-
const currentUser = await db
144-
.select({ referral_code: schema.user.referral_code })
145-
.from(schema.user)
146-
.where(eq(schema.user.id, userId))
147-
.limit(1)
148-
149-
if (currentUser[0]?.referral_code === referralCode) {
150-
return NextResponse.json(
151-
{
152-
error: "Nice try bud, you can't use your own referral code",
153-
},
154-
{
155-
status: 400,
156-
}
157-
)
158-
}
159-
160-
// Find the referrer user
161-
const referrers = await db
162-
.select()
163-
.from(schema.user)
164-
.where(eq(schema.user.referral_code, referralCode))
165-
.limit(1)
166-
167-
if (referrers.length !== 1) {
168-
return NextResponse.json(
169-
{ error: 'Invalid referral code' },
170-
{ status: 400 }
171-
)
172-
}
173-
const referrer = referrers[0]
174-
175-
// Check if the referrer has maxed out their referrals
176-
const referralStatus = await hasMaxedReferrals(referrer.id)
177-
if (referralStatus.reason) {
178-
return NextResponse.json(
179-
{ error: referralStatus.reason },
180-
{ status: 400 }
181-
)
182-
}
183-
184-
await db.transaction(async (tx) => {
185-
// Create the referral
186-
await tx.insert(schema.referral).values({
187-
referrer_id: referrer.id,
188-
referred_id: userId,
189-
status: 'completed',
190-
credits: CREDITS_REFERRAL_BONUS,
191-
created_at: new Date(),
192-
completed_at: new Date(),
193-
})
194-
195-
// Update both users' quota
196-
await tx
197-
.update(schema.user)
198-
.set({
199-
quota: sql<number>`${schema.user.quota} + ${CREDITS_REFERRAL_BONUS}`,
200-
})
201-
.where(or(eq(schema.user.id, referrer.id), eq(schema.user.id, userId)))
202-
})
203-
204-
return NextResponse.json(
205-
{
206-
credits_redeemed: CREDITS_REFERRAL_BONUS,
207-
},
208-
{
209-
status: 200,
210-
}
211-
)
212-
} catch (error) {
213-
console.error('Error applying referral code:', error)
214-
return NextResponse.json(
215-
{ error: 'Internal Server Error' },
216-
{ status: 500 }
217-
)
218-
}
127+
return redeemReferralCode(referralCode, userId)
219128
}

web/src/app/onboard/page.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { authOptions } from '../api/auth/[...nextauth]/auth-options'
1212
import { genAuthCode } from 'common/util/credentials'
1313
import { env } from '@/env.mjs'
1414
import CardWithBeams from '@/components/card-with-beams'
15-
import { redeemReferralCode } from '../api/referrals/route'
15+
import { redeemReferralCode } from '../api/referrals/helpers'
1616

1717
interface PageProps {
1818
searchParams: {
@@ -145,16 +145,18 @@ const Onboard = async ({ searchParams }: PageProps) => {
145145
}
146146
redeemReferralMessage = (
147147
<p>
148-
`You've earned an extra ${respJson.credits_redeemed} credits from your
149-
referral code!`
148+
`You&apos;ve earned an extra ${respJson.credits_redeemed} credits from
149+
your referral code!`
150150
</p>
151151
)
152152
} catch (e) {
153153
console.error(e)
154154
const error = e as Error
155155
redeemReferralMessage = (
156156
<div className="flex flex-col space-y-2">
157-
<p>Uh-oh, we couldn't apply your referral code. {error.message}.</p>
157+
<p>
158+
Uh-oh, we couldn&apos;t apply your referral code. {error.message}.
159+
</p>
158160
<p>
159161
Please try again and reach out to {env.NEXT_PUBLIC_SUPPORT_EMAIL} if
160162
the problem persists.

web/src/app/referrals/[code]/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default function RedeemPage({ params }: { params: { code: string } }) {
7878
<CardHeader>
7979
<CardTitle className="flex">
8080
<GiftIcon className="mr-2" />
81-
You've got credits!
81+
You&apos;ve got credits!
8282
</CardTitle>
8383
</CardHeader>
8484

@@ -109,8 +109,8 @@ export default function RedeemPage({ params }: { params: { code: string } }) {
109109
</ol>
110110
{data?.isSameUser && (
111111
<p className="font-bold text-red-600 mt-4">
112-
Just FYI, this is your own referral code. It won't be valid for
113-
you to use.
112+
Just FYI, this is your own referral code. It won&apos;t be valid
113+
for you to use.
114114
</p>
115115
)}
116116
</CardContent>

web/src/app/referrals/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const ReferralsPage = () => {
6363
return (
6464
<Card className="mb-6">
6565
<CardHeader>
66-
<CardTitle>You're not logged in.</CardTitle>
66+
<CardTitle>You&apos;re not logged in.</CardTitle>
6767
</CardHeader>
6868
<CardContent>No referral code for you!</CardContent>
6969
</Card>
@@ -154,7 +154,7 @@ const ReferralsPage = () => {
154154
<Separator />
155155

156156
{data.referrals.length === 0 ? (
157-
<p>You haven't referred anyone yet.</p>
157+
<p>You haven&apos;t referred anyone yet.</p>
158158
) : (
159159
<ul className="space-y-2">
160160
{data.referrals.map((r) => (

web/src/components/navbar/sign-in-button.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Button } from '@/components/ui/button'
88
import { OAuthProviderType } from 'next-auth/providers/oauth-types'
99
import { sleep } from 'common/util/helpers'
1010
import { toast } from '../ui/use-toast'
11+
import Image from 'next/image'
1112

1213
export const SignInButton = ({
1314
providerName,
@@ -37,9 +38,10 @@ export const SignInButton = ({
3738
className="flex items-center gap-2"
3839
>
3940
{isPending && <Icons.loader className="mr-2 size-4 animate-spin" />}
40-
<img
41+
<Image
4142
src={`https://s2.googleusercontent.com/s2/favicons?domain=${providerDomain}`}
4243
className="rounded-full"
44+
alt={providerName + ' logo'}
4345
/>
4446
Continue with{' '}
4547
{providerName.charAt(0).toUpperCase() + providerName.slice(1)}

0 commit comments

Comments
 (0)