1111 */
1212
1313import { db } from '@sim/db'
14- import { DEFAULT_REFERRAL_BONUS_CREDITS } from '@sim/db/constants'
1514import { referralAttribution , referralCampaigns , user , userStats } from '@sim/db/schema'
1615import { createLogger } from '@sim/logger'
1716import { eq } from 'drizzle-orm'
@@ -157,9 +156,12 @@ export async function POST() {
157156 }
158157
159158 const matchedCampaign = await findMatchingCampaign ( utmData )
160- const bonusAmount = matchedCampaign
161- ? Number ( matchedCampaign . bonusCreditAmount )
162- : DEFAULT_REFERRAL_BONUS_CREDITS
159+ if ( ! matchedCampaign ) {
160+ cookieStore . delete ( COOKIE_NAME )
161+ return NextResponse . json ( { attributed : false , reason : 'no_matching_campaign' } )
162+ }
163+
164+ const bonusAmount = Number ( matchedCampaign . bonusCreditAmount )
163165
164166 let attributed = false
165167 await db . transaction ( async ( tx ) => {
@@ -168,7 +170,7 @@ export async function POST() {
168170 . values ( {
169171 id : nanoid ( ) ,
170172 userId : session . user . id ,
171- campaignId : matchedCampaign ? .id ?? null ,
173+ campaignId : matchedCampaign . id ,
172174 utmSource : utmData . utm_source || null ,
173175 utmMedium : utmData . utm_medium || null ,
174176 utmCampaign : utmData . utm_campaign || null ,
@@ -189,8 +191,8 @@ export async function POST() {
189191 if ( attributed ) {
190192 logger . info ( 'Referral attribution created and bonus credits applied' , {
191193 userId : session . user . id ,
192- campaignId : matchedCampaign ? .id ,
193- campaignName : matchedCampaign ? .name ,
194+ campaignId : matchedCampaign . id ,
195+ campaignName : matchedCampaign . name ,
194196 utmSource : utmData . utm_source ,
195197 utmCampaign : utmData . utm_campaign ,
196198 utmContent : utmData . utm_content ,
0 commit comments