|
1 | 1 | import { NextResponse } from 'next/server' |
2 | 2 | import { getServerSession } from 'next-auth' |
3 | | -import { eq, sql, or } from 'drizzle-orm' |
| 3 | +import { eq } from 'drizzle-orm' |
4 | 4 | import { z } from 'zod' |
5 | 5 | import { authOptions } from '../auth/[...nextauth]/auth-options' |
6 | 6 | import db from 'common/db' |
7 | 7 | 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' |
10 | 10 |
|
11 | 11 | type Referral = Pick<typeof schema.user.$inferSelect, 'id' | 'name' | 'email'> & |
12 | 12 | Pick<typeof schema.referral.$inferSelect, 'credits'> |
@@ -124,96 +124,5 @@ export async function POST(request: Request) { |
124 | 124 | } |
125 | 125 |
|
126 | 126 | 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) |
219 | 128 | } |
0 commit comments