Fix: Derive client_id from API token when not provided in /api/reports/last-sync

- When client_id is not provided, derive a unique client_id from the API token (token_{api_token.id})
- This ensures each API token has its own unique client identifier for sync purposes
- Fixes issue where client was receiving status for all clients instead of just itself
- Allows the server to correctly signal requires_full_sync=True when the database is empty for that specific client
parent c5541e46
......@@ -1462,88 +1462,21 @@ def api_get_last_sync():
logger.info(f"API get last sync: user={user.username}, client_id={client_id}, api_token={api_token.name if api_token else 'None'}")
# If client_id is not provided, get all clients for this user
# If client_id is not provided, derive a unique client_id from the API token
if not client_id:
# Get all clients associated with this user's API tokens
user_token_ids = [t.id for t in APIToken.query.filter_by(user_id=user.id).all()]
logger.info(f"API get last sync: Found {len(user_token_ids)} API tokens for user {user.username}")
if user_token_ids:
client_ids = [c.rustdesk_id for c in ClientActivity.query.filter(
ClientActivity.api_token_id.in_(user_token_ids)
).all()]
logger.info(f"API get last sync: Found {len(client_ids)} clients for user {user.username}: {client_ids}")
if not client_ids:
logger.warning(f"API get last sync: No clients found for user {user.username}")
return jsonify({
'success': True,
'message': 'No clients found for this user',
'clients': [],
'server_timestamp': datetime.utcnow().isoformat()
}), 200
# Return last sync info for all clients
clients_data = []
for cid in client_ids:
logger.info(f"API get last sync: Processing client {cid}")
last_sync = ReportSync.query.filter_by(client_id=cid)\
.order_by(desc(ReportSync.sync_timestamp))\
.first()
if last_sync:
total_syncs = ReportSync.query.filter_by(client_id=cid).count()
logger.info(f"API get last sync: Client {cid} has {total_syncs} sync(s), last_sync_id={last_sync.sync_id}")
clients_data.append({
'client_id': cid,
'last_sync_id': last_sync.sync_id,
'last_sync_timestamp': last_sync.sync_timestamp.isoformat() if last_sync.sync_timestamp else None,
'last_sync_type': last_sync.sync_type if hasattr(last_sync, 'sync_type') else 'unknown',
'total_syncs': total_syncs,
'requires_full_sync': False,
'last_sync_summary': {
'total_payin': float(last_sync.total_payin) if last_sync.total_payin else 0.0,
'total_payout': float(last_sync.total_payout) if last_sync.total_payout else 0.0,
'net_profit': float(last_sync.net_profit) if last_sync.net_profit else 0.0,
'total_bets': last_sync.total_bets,
'total_matches': last_sync.total_matches,
'cap_compensation_balance': float(last_sync.cap_compensation_balance) if last_sync.cap_compensation_balance else 0.0
}
})
else:
# Check if any records exist for this client
total_bets_for_client = Bet.query.filter_by(client_id=cid).count()
total_stats_for_client = ExtractionStats.query.filter_by(client_id=cid).count()
total_match_reports_for_client = MatchReport.query.filter_by(client_id=cid).count()
requires_full_sync = (total_bets_for_client == 0 and total_stats_for_client == 0 and total_match_reports_for_client == 0)
logger.info(f"API get last sync: Client {cid} - bets={total_bets_for_client}, stats={total_stats_for_client}, match_reports={total_match_reports_for_client}, requires_full_sync={requires_full_sync}")
clients_data.append({
'client_id': cid,
'last_sync_id': None,
'last_sync_timestamp': None,
'last_sync_type': None,
'total_syncs': 0,
'requires_full_sync': requires_full_sync,
'last_sync_summary': None
})
logger.info(f"API get last sync: Returning {len(clients_data)} client(s) for user {user.username}")
return jsonify({
'success': True,
'message': f'Found {len(clients_data)} client(s) for this user',
'clients': clients_data,
'server_timestamp': datetime.utcnow().isoformat()
}), 200
# Use the API token to derive a unique client_id
if api_token:
# Derive a unique client_id from the API token
# This ensures each API token has its own unique client identifier for sync purposes
client_id = f"token_{api_token.id}"
logger.info(f"API get last sync: Derived client_id={client_id} from API token {api_token.name} (ID: {api_token.id})")
else:
logger.warning(f"API get last sync: No API token provided")
return jsonify({
'success': True,
'message': 'No API tokens found for this user',
'clients': [],
'server_timestamp': datetime.utcnow().isoformat()
}), 200
'success': False,
'error': 'No API token provided',
'details': 'Cannot identify client without API token'
}), 401
# Verify user has access to this specific client
if not user.is_admin:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment