@@ -119,7 +119,7 @@ export async function redeemReferralCode(referralCode: string, userId: string) {
119119 }
120120
121121 await db . transaction ( async ( tx ) => {
122- // 1. Create the referral record locally
122+ // 1. Create the referral record locally (one-time referral, is_legacy: false)
123123 const now = new Date ( )
124124 const referralRecord = await tx
125125 . insert ( schema . referral )
@@ -128,6 +128,7 @@ export async function redeemReferralCode(referralCode: string, userId: string) {
128128 referred_id : userId ,
129129 status : 'completed' ,
130130 credits : CREDITS_REFERRAL_BONUS ,
131+ is_legacy : false ,
131132 created_at : now ,
132133 completed_at : now ,
133134 } )
@@ -137,30 +138,17 @@ export async function redeemReferralCode(referralCode: string, userId: string) {
137138
138139 const operationId = referralRecord [ 0 ] . operation_id
139140
140- // Get the user's next quota reset date
141- const user = await tx . query . user . findFirst ( {
142- where : eq ( schema . user . id , userId ) ,
143- columns : {
144- next_quota_reset : true ,
145- } ,
146- } )
147-
148- if ( ! user ?. next_quota_reset ) {
149- throw new Error ( 'User next_quota_reset not found' )
150- }
151-
152- // 2. Process and grant credits for both users
141+ // 2. Process and grant credits for both users (one-time, never expires)
153142 const grantPromises = [ ]
154143
155- // Process Referrer
156- grantPromises . push (
144+ const grantForUser = ( user : { id : string ; role : 'referrer' | 'referred' } ) =>
157145 grantCreditOperation ( {
158- userId : referrer . id ,
146+ userId : user . id ,
159147 amount : CREDITS_REFERRAL_BONUS ,
160148 type : 'referral' ,
161- description : ' Referral bonus (referrer)' ,
162- expiresAt : user . next_quota_reset ,
163- operationId : `${ operationId } -referrer ` ,
149+ description : ` Referral bonus (${ user . role } )` ,
150+ expiresAt : null , // One-time referrals never expire
151+ operationId : `${ operationId } -${ user . role } ` ,
164152 tx,
165153 logger,
166154 } )
@@ -169,42 +157,17 @@ export async function redeemReferralCode(referralCode: string, userId: string) {
169157 logger . error (
170158 {
171159 error,
172- userId : referrer . id ,
173- role : 'referrer' ,
160+ userId : user . id ,
161+ role : user . role ,
174162 creditsToGrant : CREDITS_REFERRAL_BONUS ,
175163 } ,
176164 'Failed to process referral credit grant' ,
177165 )
178166 return false
179- } ) ,
180- )
167+ } )
181168
182- // Process Referred User
183- grantPromises . push (
184- grantCreditOperation ( {
185- userId : referred . id ,
186- amount : CREDITS_REFERRAL_BONUS ,
187- type : 'referral' ,
188- description : 'Referral bonus (referred)' ,
189- expiresAt : user . next_quota_reset ,
190- operationId : `${ operationId } -referred` ,
191- tx,
192- logger,
193- } )
194- . then ( ( ) => true )
195- . catch ( ( error : Error ) => {
196- logger . error (
197- {
198- error,
199- userId : referred . id ,
200- role : 'referred' ,
201- creditsToGrant : CREDITS_REFERRAL_BONUS ,
202- } ,
203- 'Failed to process referral credit grant' ,
204- )
205- return false
206- } ) ,
207- )
169+ grantPromises . push ( grantForUser ( { id : referrer . id , role : 'referrer' } ) )
170+ grantPromises . push ( grantForUser ( { id : referred . id , role : 'referred' } ) )
208171
209172 const results = await Promise . all ( grantPromises )
210173
0 commit comments