Fix: Auto-detect client_id from ClientActivity when not provided in /api/reports/last-sync

- When client doesn't send client_id parameter, look up rustdesk_id from ClientActivity table
- This ensures server uses same client_id that client uses when syncing
- Fixes issue where server derived 'token_66' but client was syncing with '8d95b8a115c19857'
- Server now correctly finds existing sync records and returns requires_full_sync=False
parent dce7b7b7
...@@ -852,7 +852,7 @@ def track_client_activity(api_token, rustdesk_id=None): ...@@ -852,7 +852,7 @@ def track_client_activity(api_token, rustdesk_id=None):
db.session.add(client) db.session.add(client)
logger.info(f"track_client_activity: Created new client with rustdesk_id {rustdesk_id}") logger.info(f"track_client_activity: Created new client with rustdesk_id {rustdesk_id}")
else: else:
# If no rustdesk_id provided, update the most recent client for this API token # If no rustdesk_id provided, update most recent client for this API token
client = ClientActivity.query.filter_by( client = ClientActivity.query.filter_by(
api_token_id=api_token.id api_token_id=api_token.id
).order_by(ClientActivity.last_seen.desc()).first() ).order_by(ClientActivity.last_seen.desc()).first()
...@@ -999,7 +999,7 @@ def api_reports_sync(): ...@@ -999,7 +999,7 @@ def api_reports_sync():
# Get request data - use force=True to parse JSON even if Content-Type header is missing # Get request data - use force=True to parse JSON even if Content-Type header is missing
data = request.get_json(force=True, silent=True) data = request.get_json(force=True, silent=True)
# Log the full request content for debugging # Log full request content for debugging
if data: if data:
logger.info(f"Reports sync request content: {json.dumps(data, indent=2, default=str)}") logger.info(f"Reports sync request content: {json.dumps(data, indent=2, default=str)}")
else: else:
...@@ -1094,7 +1094,7 @@ def api_reports_sync(): ...@@ -1094,7 +1094,7 @@ def api_reports_sync():
}), 400 }), 400
# Validate summary data # Validate summary data
summary = data['summary'] summary = data.get('summary')
if not all(key in summary for key in ['total_payin', 'total_payout', 'net_profit', 'total_bets', 'total_matches']): if not all(key in summary for key in ['total_payin', 'total_payout', 'net_profit', 'total_bets', 'total_matches']):
return jsonify({ return jsonify({
'success': False, 'success': False,
...@@ -1462,14 +1462,30 @@ def api_get_last_sync(): ...@@ -1462,14 +1462,30 @@ 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'}") 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, derive a unique client_id from the API token # If client_id is not provided, derive it from ClientActivity using API token
if not client_id: if not client_id:
# Use the API token to derive a unique client_id # Get most recent client activity for this API token to find rustdesk_id
if api_token: if api_token:
# Derive a unique client_id from the API token most_recent_client = ClientActivity.query.filter_by(api_token_id=api_token.id)\
# This ensures each API token has its own unique client identifier for sync purposes .order_by(desc(ClientActivity.last_seen))\
client_id = f"token_{api_token.id}" .first()
logger.info(f"API get last sync: Derived client_id={client_id} from API token {api_token.name} (ID: {api_token.id})")
if most_recent_client:
client_id = most_recent_client.rustdesk_id
logger.info(f"API get last sync: Auto-detected client_id={client_id} from API token {api_token.name} (ID: {api_token.id}) via ClientActivity")
else:
logger.warning(f"API get last sync: No client activity found for API token {api_token.name}")
return jsonify({
'success': True,
'message': 'No client activity found for this API token',
'client_id': None,
'last_sync_id': None,
'last_sync_timestamp': None,
'last_sync_type': None,
'total_syncs': 0,
'requires_full_sync': True,
'server_timestamp': datetime.utcnow().isoformat()
}), 200
else: else:
logger.warning(f"API get last sync: No API token provided") logger.warning(f"API get last sync: No API token provided")
return jsonify({ return jsonify({
......
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