Refactor backend configuration and enhance logging in services
Updated config.js to ensure .env is loaded correctly from the backend directory. Enhanced MyTischtennisUrlController by removing unnecessary console logs and improving error handling. Updated autoFetchMatchResultsService and autoUpdateRatingsService to return detailed summaries, including counts of fetched or updated items. Improved logging in schedulerService to capture execution details, enhancing monitoring capabilities across scheduled tasks.
This commit is contained in:
@@ -54,7 +54,6 @@ class MyTischtennisUrlController {
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching additional team data:', error);
|
||||
// Continue with parsed data only
|
||||
}
|
||||
}
|
||||
@@ -107,7 +106,6 @@ class MyTischtennisUrlController {
|
||||
account.accessToken
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error fetching team data:', error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,67 +243,26 @@ class MyTischtennisUrlController {
|
||||
}
|
||||
|
||||
// Get myTischtennis session (similar to memberService.updateRatingsFromMyTischtennis)
|
||||
console.log('Fetching session for userId:', userId, '(from header:', userIdOrEmail, ')');
|
||||
let session;
|
||||
|
||||
try {
|
||||
session = await myTischtennisService.getSession(userId);
|
||||
console.log('Session found:', !!session);
|
||||
if (session) {
|
||||
console.log('Session details:', {
|
||||
hasCookie: !!session.cookie,
|
||||
hasAccessToken: !!session.accessToken,
|
||||
expiresAt: session.expiresAt ? new Date(session.expiresAt * 1000).toISOString() : null
|
||||
});
|
||||
}
|
||||
} catch (sessionError) {
|
||||
console.log('Session invalid, attempting login...', sessionError.message);
|
||||
console.error('Session error details:', {
|
||||
message: sessionError.message,
|
||||
stack: sessionError.stack,
|
||||
name: sessionError.name
|
||||
});
|
||||
|
||||
// Versuche automatischen Login mit gespeicherten Credentials
|
||||
try {
|
||||
console.log('Attempting automatic login for userId:', userId);
|
||||
|
||||
// Check if account exists and has password
|
||||
const accountCheck = await myTischtennisService.getAccount(userId);
|
||||
if (!accountCheck) {
|
||||
throw new Error('MyTischtennis-Account nicht gefunden');
|
||||
}
|
||||
|
||||
console.log('Account found:', {
|
||||
email: accountCheck.email,
|
||||
hasPassword: !!accountCheck.encryptedPassword,
|
||||
hasAccessToken: !!accountCheck.accessToken,
|
||||
hasCookie: !!accountCheck.cookie
|
||||
});
|
||||
|
||||
if (!accountCheck.encryptedPassword) {
|
||||
throw new Error('Kein Passwort gespeichert. Bitte melden Sie sich in den MyTischtennis-Einstellungen an und speichern Sie Ihr Passwort.');
|
||||
}
|
||||
|
||||
console.log('Calling verifyLogin...');
|
||||
const verifyResult = await myTischtennisService.verifyLogin(userId);
|
||||
console.log('verifyLogin result:', verifyResult);
|
||||
|
||||
await myTischtennisService.verifyLogin(userId);
|
||||
session = await myTischtennisService.getSession(userId);
|
||||
console.log('Automatic login successful, session:', {
|
||||
hasCookie: !!session?.cookie,
|
||||
hasAccessToken: !!session?.accessToken
|
||||
});
|
||||
} catch (loginError) {
|
||||
console.error('Automatic login failed - DETAILED ERROR:', {
|
||||
message: loginError.message,
|
||||
stack: loginError.stack,
|
||||
name: loginError.name,
|
||||
userId: userId,
|
||||
response: loginError.response?.data,
|
||||
status: loginError.response?.status
|
||||
});
|
||||
|
||||
const errorMessage = loginError.message || 'Automatischer Login fehlgeschlagen';
|
||||
throw new HttpError(401, `MyTischtennis-Session abgelaufen und automatischer Login fehlgeschlagen: ${errorMessage}. Bitte melden Sie sich in den MyTischtennis-Einstellungen an.`);
|
||||
}
|
||||
@@ -318,13 +275,7 @@ class MyTischtennisUrlController {
|
||||
throw new HttpError(404, 'MyTischtennis-Account nicht verknüpft. Bitte verknüpfen Sie Ihren Account in den MyTischtennis-Einstellungen.');
|
||||
}
|
||||
|
||||
console.log('Using session:', {
|
||||
email: account.email,
|
||||
hasCookie: !!session.cookie,
|
||||
hasAccessToken: !!session.accessToken,
|
||||
expiresAt: new Date(session.expiresAt * 1000)
|
||||
});
|
||||
|
||||
|
||||
// Get team with league and season
|
||||
const team = await ClubTeam.findByPk(clubTeamId, {
|
||||
include: [
|
||||
@@ -345,29 +296,11 @@ class MyTischtennisUrlController {
|
||||
throw new HttpError(404, `Team mit ID ${clubTeamId} nicht gefunden`);
|
||||
}
|
||||
|
||||
console.log('Team data:', {
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
myTischtennisTeamId: team.myTischtennisTeamId,
|
||||
hasLeague: !!team.league,
|
||||
leagueData: team.league ? {
|
||||
id: team.league.id,
|
||||
name: team.league.name,
|
||||
myTischtennisGroupId: team.league.myTischtennisGroupId,
|
||||
association: team.league.association,
|
||||
groupname: team.league.groupname,
|
||||
hasSeason: !!team.league.season
|
||||
} : null
|
||||
});
|
||||
|
||||
// Verbesserte Validierung mit detaillierten Fehlermeldungen
|
||||
if (!team.myTischtennisTeamId) {
|
||||
throw new HttpError(400, `Team "${team.name}" (interne ID: ${team.id}) ist nicht für myTischtennis konfiguriert: myTischtennisTeamId fehlt. Bitte konfigurieren Sie das Team zuerst über die MyTischtennis-URL.`);
|
||||
}
|
||||
|
||||
// Stelle sicher, dass die myTischtennisTeamId auch wirklich verwendet wird
|
||||
console.log(`Verwende myTischtennisTeamId: ${team.myTischtennisTeamId} (nicht die interne clubTeamId: ${team.id})`);
|
||||
|
||||
if (!team.league) {
|
||||
throw new HttpError(400, 'Team ist keiner Liga zugeordnet. Bitte ordnen Sie das Team einer Liga zu.');
|
||||
}
|
||||
@@ -394,11 +327,6 @@ class MyTischtennisUrlController {
|
||||
// Log the request to myTischtennis BEFORE making the call
|
||||
// This ensures we always see what WILL BE sent, even if the call fails
|
||||
const requestStartTime = Date.now();
|
||||
console.log('=== ABOUT TO FETCH FROM MYTISCHTENNIS ===');
|
||||
console.log('URL:', myTischtennisUrl);
|
||||
console.log('myTischtennisTeamId:', team.myTischtennisTeamId);
|
||||
console.log('clubTeamId:', team.id);
|
||||
|
||||
try {
|
||||
await apiLogService.logRequest({
|
||||
userId: account.userId,
|
||||
@@ -423,7 +351,7 @@ class MyTischtennisUrlController {
|
||||
schedulerJobType: 'mytischtennis_fetch'
|
||||
});
|
||||
} catch (logError) {
|
||||
console.error('Error logging request (non-critical):', logError);
|
||||
// Silent fail - logging errors shouldn't break the request
|
||||
}
|
||||
|
||||
// Fetch data for this specific team
|
||||
@@ -445,9 +373,7 @@ class MyTischtennisUrlController {
|
||||
try {
|
||||
await autoFetchMatchResultsService.fetchAndUpdateLeagueTable(account.userId, team.league.id);
|
||||
tableUpdateResult = 'League table updated successfully';
|
||||
console.log('✓ League table updated for league:', team.league.id);
|
||||
} catch (error) {
|
||||
console.error('Error fetching league table data:', error);
|
||||
tableUpdateResult = 'League table update failed: ' + error.message;
|
||||
// Don't fail the entire request if table update fails
|
||||
}
|
||||
@@ -462,8 +388,6 @@ class MyTischtennisUrlController {
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error in fetchTeamData:', error);
|
||||
console.error('Error stack:', error.stack);
|
||||
|
||||
// Update log with error information if we got far enough to build the URL
|
||||
if (typeof myTischtennisUrl !== 'undefined' && account && team) {
|
||||
@@ -491,7 +415,7 @@ class MyTischtennisUrlController {
|
||||
schedulerJobType: 'mytischtennis_fetch'
|
||||
});
|
||||
} catch (logError) {
|
||||
console.error('Error logging failed request:', logError);
|
||||
// Silent fail - logging errors shouldn't break the request
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user