@@ -397,7 +397,8 @@ def grant_trial_credits(
397397 )
398398 raise
399399
400- # Grant credits to company via add_tokens_to_company (resolves root parent)
400+ # Grant credits to company - all updates done on the same session
401+ # to avoid SQLite "database is locked" errors from competing sessions
401402 company .trial_credits_granted = credits_usd
402403 company .trial_credits_granted_at = datetime .now (timezone .utc )
403404 company .trial_domain = domain .lower ()
@@ -418,20 +419,34 @@ def grant_trial_credits(
418419 if pricing_model == "tiered_plan" and not has_paid_subscription :
419420 trial_config = pricing_config .get ("trial" , {})
420421 trial_plan_id = trial_config .get ("plan_id" , "starter" )
421- from MagicalAuth import MagicalAuth
422-
423- auth = MagicalAuth ()
424- auth .set_company_plan (
425- company_id = company_id ,
426- plan_id = trial_plan_id ,
427- )
422+ # Set plan directly on the company object in the same session
423+ # instead of calling set_company_plan which opens a new session
424+ tier = None
425+ for t in pricing_config .get ("tiers" , []):
426+ if t .get ("id" ) == trial_plan_id :
427+ tier = t
428+ break
429+ if tier :
430+ limits = tier .get ("limits" , {})
431+ company .plan_id = trial_plan_id
432+ company .device_limit = limits .get ("devices" )
433+ company .monthly_token_limit = limits .get ("tokens" )
434+ company .storage_limit_bytes = (
435+ (limits .get ("storage_gb" ) or 0 ) * 1024 * 1024 * 1024
436+ )
437+ company .user_limit = limits .get ("users" )
438+ company .tokens_used_this_period = 0
439+ company .current_period_start = datetime .now (timezone .utc )
440+ else :
441+ # Fallback: just set the plan_id
442+ company .plan_id = trial_plan_id
428443 elif pricing_model == "per_bed" and not has_paid_subscription :
429444 trial_config = pricing_config .get ("trial" , {})
430445 trial_beds = trial_config .get ("units" , 5 )
431446 company .bed_limit = trial_beds
432447 company .plan_id = f"per_bed_{ trial_beds } "
433448
434- # Calculate token amount and add via add_tokens_to_company (root parent aware)
449+ # Calculate token amount
435450 tokens_granted = 0
436451 try :
437452 from payments .stripe_service import PriceService
@@ -443,31 +458,28 @@ def grant_trial_credits(
443458 except Exception as e :
444459 logging .warning (f"Could not calculate token amount for trial: { e } " )
445460
446- session .commit ()
447-
448- # Add tokens via the root-parent-aware method
461+ # Add tokens directly on the same session to avoid DB locking
449462 if tokens_granted > 0 :
450- try :
451- from MagicalAuth import MagicalAuth
463+ company .token_balance = (company .token_balance or 0 ) + tokens_granted
464+ company .token_balance_usd = (
465+ company .token_balance_usd or 0
466+ ) + credits_usd
467+
468+ # Reactivate any inactive users in this company
469+ from DB import User , UserCompany
470+
471+ company_user_ids = (
472+ session .query (UserCompany .user_id )
473+ .filter (UserCompany .company_id == company_id )
474+ .all ()
475+ )
476+ if company_user_ids :
477+ user_ids = [uid for (uid ,) in company_user_ids ]
478+ session .query (User ).filter (
479+ User .id .in_ (user_ids ), User .is_active == False
480+ ).update ({"is_active" : True }, synchronize_session = "fetch" )
452481
453- auth = MagicalAuth ()
454- auth .add_tokens_to_company (
455- company_id = company_id ,
456- token_amount = tokens_granted ,
457- amount_usd = credits_usd ,
458- )
459- except Exception as e :
460- logging .warning (
461- f"Could not add tokens via add_tokens_to_company: { e } "
462- )
463- # Fallback: add directly
464- company .token_balance = (
465- company .token_balance or 0
466- ) + tokens_granted
467- company .token_balance_usd = (
468- company .token_balance_usd or 0
469- ) + credits_usd
470- session .commit ()
482+ session .commit ()
471483
472484 logging .info (
473485 f"Granted ${ credits_usd :.2f} trial credits to company { company_id } "
0 commit comments