From e0d56ddadd51bd0386a6e659a7c480ca41a7a27a Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Thu, 16 Oct 2025 18:53:28 +0200 Subject: [PATCH] Enhance MyTischtennis fetch logging in AutoFetchMatchResultsService and AutoUpdateRatingsService. Integrate logging for match results and league table fetch attempts, including success status and execution details. Update updateRatings method to utilize memberService for fetching ratings, improving error handling and logging consistency. Update .gitignore to exclude backend log files. --- .gitignore | 2 + .../services/autoFetchMatchResultsService.js | 61 +++++++++++++++++-- backend/services/autoUpdateRatingsService.js | 43 ++++++++----- 3 files changed, 88 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 8a8d91a..a4d801f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ frontend/.env backend/.env backend/images/* +backend/backend-debug.log +backend/*.log \ No newline at end of file diff --git a/backend/services/autoFetchMatchResultsService.js b/backend/services/autoFetchMatchResultsService.js index 4a2271a..212e3ab 100644 --- a/backend/services/autoFetchMatchResultsService.js +++ b/backend/services/autoFetchMatchResultsService.js @@ -1,4 +1,5 @@ import myTischtennisService from './myTischtennisService.js'; +import myTischtennisFetchLogService from './myTischtennisFetchLogService.js'; import myTischtennisClient from '../clients/myTischtennisClient.js'; import MyTischtennis from '../models/MyTischtennis.js'; import ClubTeam from '../models/ClubTeam.js'; @@ -100,7 +101,20 @@ class AutoFetchMatchResultsService { const executionTime = Date.now() - startTime; - // TODO: Log the attempt to a dedicated match results history table + // Log the attempt to MyTischtennisFetchLog + await myTischtennisFetchLogService.logFetch( + account.userId, + 'match_results', + success, + message, + { + errorDetails, + recordsProcessed: fetchedCount, + executionTime, + isAutomatic: true + } + ); + devLog(`Match results fetch for ${account.email}: ${success ? 'SUCCESS' : 'FAILED'} (${executionTime}ms)`); } @@ -221,11 +235,40 @@ class AutoFetchMatchResultsService { // Also fetch and update league table data for this team if (account.userId) { try { - await this.fetchAndUpdateLeagueTable(account.userId, team.leagueId); + const tableStartTime = Date.now(); + const tableResult = await this.fetchAndUpdateLeagueTable(account.userId, team.leagueId); + const tableExecutionTime = Date.now() - tableStartTime; + devLog(`✓ League table updated for league ${team.leagueId}`); + + // Log league table fetch + await myTischtennisFetchLogService.logFetch( + account.userId, + 'league_table', + true, + `League table updated successfully`, + { + recordsProcessed: tableResult?.teamsUpdated || 0, + executionTime: tableExecutionTime, + isAutomatic: true, + leagueId: team.leagueId + } + ); } catch (error) { console.error(`Error updating league table for league ${team.leagueId}:`, error); - // Don't fail the entire process if table update fails + + // Log failed table fetch + await myTischtennisFetchLogService.logFetch( + account.userId, + 'league_table', + false, + 'League table update failed', + { + errorDetails: error.message, + isAutomatic: true, + leagueId: team.leagueId + } + ); } } else { devLog(`Skipping league table update - no userId available`); @@ -727,10 +770,15 @@ class AutoFetchMatchResultsService { const tableData = await this.parseTableData(JSON.stringify(response.data), leagueId); // Update teams with table data - await this.updateTeamsWithTableData(tableData, leagueId); + const teamsUpdated = await this.updateTeamsWithTableData(tableData, leagueId); devLog(`✓ Updated league table for league ${leagueId} with ${tableData.length} teams`); + return { + teamsUpdated, + totalTeams: tableData.length + }; + } catch (error) { console.error(`Error fetching league table for league ${leagueId}:`, error); throw error; @@ -789,6 +837,8 @@ class AutoFetchMatchResultsService { * @param {number} leagueId - League ID */ async updateTeamsWithTableData(tableData, leagueId) { + let updatedCount = 0; + for (const teamData of tableData) { try { // Find team by name in this league @@ -814,6 +864,7 @@ class AutoFetchMatchResultsService { tablePointsLost: teamData.tablePointsLost || 0 }); + updatedCount++; devLog(` ✓ Updated team ${team.name} with table data`); } else { devLog(` ⚠ Team not found: ${teamData.teamName}`); @@ -822,6 +873,8 @@ class AutoFetchMatchResultsService { console.error(`Error updating team ${teamData.teamName}:`, error); } } + + return updatedCount; } } diff --git a/backend/services/autoUpdateRatingsService.js b/backend/services/autoUpdateRatingsService.js index daeff9d..0900cbb 100644 --- a/backend/services/autoUpdateRatingsService.js +++ b/backend/services/autoUpdateRatingsService.js @@ -1,4 +1,6 @@ import myTischtennisService from './myTischtennisService.js'; +import myTischtennisFetchLogService from './myTischtennisFetchLogService.js'; +import memberService from './memberService.js'; import myTischtennisClient from '../clients/myTischtennisClient.js'; import MyTischtennis from '../models/MyTischtennis.js'; import { devLog } from '../utils/logger.js'; @@ -93,7 +95,21 @@ class AutoUpdateRatingsService { const executionTime = Date.now() - startTime; - // Log the attempt + // Log the attempt to MyTischtennisFetchLog + await myTischtennisFetchLogService.logFetch( + account.userId, + 'ratings', + success, + message, + { + errorDetails, + recordsProcessed: updatedCount, + executionTime, + isAutomatic: true + } + ); + + // Also log to update history (for backwards compatibility) await myTischtennisService.logUpdateAttempt( account.userId, success, @@ -108,21 +124,20 @@ class AutoUpdateRatingsService { * Update ratings for a specific account */ async updateRatings(account) { - // TODO: Implement actual rating update logic - // This would typically involve: - // 1. Fetching current ratings from myTischtennis - // 2. Comparing with local data - // 3. Updating local member ratings - devLog(`Updating ratings for ${account.email}`); - // For now, simulate an update - await new Promise(resolve => setTimeout(resolve, 1000)); - - return { - success: true, - updatedCount: Math.floor(Math.random() * 10) // Simulate some updates - }; + try { + // Use the memberService to update ratings from MyTischtennis + const result = await memberService.updateRatingsFromMyTischtennis(account.userId); + + return { + success: true, + updatedCount: result.updatedCount || 0 + }; + } catch (error) { + devLog(`Error updating ratings: ${error.message}`); + throw error; + } } /**