attempt to fix oauth trials

This commit is contained in:
Saumya 2025-09-10 16:46:11 +05:30
parent 6dd69c00c2
commit 0db4fc4d9c
2 changed files with 82 additions and 14 deletions

View File

@ -198,25 +198,42 @@ class TrialService:
logger.error(f"[TRIAL SECURITY] Error checking existing subscription: {e}")
ledger_check = await client.from_('credit_ledger')\
.select('id')\
.select('id, description')\
.eq('account_id', account_id)\
.like('description', '%trial%')\
.limit(1)\
.or_(
'description.ilike.%trial credits%,'
'description.ilike.%free trial%,'
'description.ilike.%day trial%,'
'type.eq.trial_grant'
)\
.execute()
if ledger_check.data:
logger.warning(f"[TRIAL SECURITY] Trial attempt rejected - account {account_id} has trial-related ledger entries")
await client.from_('trial_history').upsert({
'account_id': account_id,
'started_at': datetime.now(timezone.utc).isoformat(),
'ended_at': datetime.now(timezone.utc).isoformat(),
'note': 'Created from credit_ledger detection during blocked trial attempt'
}, on_conflict='account_id').execute()
has_actual_trial = False
for entry in ledger_check.data:
desc = entry.get('description', '').lower()
if 'trial credits' in desc or 'free trial' in desc or 'day trial' in desc:
has_actual_trial = True
break
elif 'start a trial' in desc or 'please start a trial' in desc:
continue
else:
has_actual_trial = True
break
raise HTTPException(
status_code=403,
detail="Trial history detected. Each account is limited to one free trial."
)
if has_actual_trial:
logger.warning(f"[TRIAL SECURITY] Trial attempt rejected - account {account_id} has trial-related ledger entries")
await client.from_('trial_history').upsert({
'account_id': account_id,
'started_at': datetime.now(timezone.utc).isoformat(),
'ended_at': datetime.now(timezone.utc).isoformat(),
'note': 'Created from credit_ledger detection during blocked trial attempt'
}, on_conflict='account_id').execute()
raise HTTPException(
status_code=403,
detail="Trial history detected. Each account is limited to one free trial."
)
try:
from .subscription_service import subscription_service

View File

@ -0,0 +1,51 @@
-- OPTIONAL: Clear trial data for testing
-- Run this manually for specific test accounts only
-- DO NOT run this in production without careful consideration
-- To clear trial data for a specific account, run:
-- Replace 'YOUR_ACCOUNT_ID' with the actual account ID
/*
BEGIN;
-- Clear trial history
DELETE FROM trial_history
WHERE account_id = 'YOUR_ACCOUNT_ID';
-- Clear trial-related credit ledger entries
DELETE FROM credit_ledger
WHERE account_id = 'YOUR_ACCOUNT_ID'
AND description LIKE '%trial%';
-- Reset trial status in credit_accounts
UPDATE credit_accounts
SET
trial_status = 'none',
trial_started_at = NULL,
trial_ends_at = NULL
WHERE account_id = 'YOUR_ACCOUNT_ID';
COMMIT;
*/
-- For the specific user having issues:
-- Uncomment and run if needed for testing
/*
BEGIN;
DELETE FROM trial_history
WHERE account_id = '210578c9-d8a0-4197-8cce-b866395a2080';
DELETE FROM credit_ledger
WHERE account_id = '210578c9-d8a0-4197-8cce-b866395a2080'
AND description LIKE '%trial%';
UPDATE credit_accounts
SET
trial_status = 'none',
trial_started_at = NULL,
trial_ends_at = NULL
WHERE account_id = '210578c9-d8a0-4197-8cce-b866395a2080';
COMMIT;
*/