diff --git a/backend/services/matchService.js b/backend/services/matchService.js index 4427a131..29107ee9 100755 --- a/backend/services/matchService.js +++ b/backend/services/matchService.js @@ -185,25 +185,12 @@ class MatchService { }); } - // Club teams already configured through MyTischtennis can be linked to - // leagues with a different display name than the click-TT CSV "Staffel". - // Update the existing match in that league so the schedule view receives - // the venue instead of creating a disconnected duplicate in a CSV league. - const existingMatch = await this.findExistingMatchForCSV( - clubId, - parsedDate, - homeTeamName, - guestTeamName - ); - if (existingMatch) { - if (location && existingMatch.locationId !== location.id) { - await existingMatch.update({ locationId: location.id }); - } - continue; - } - + // Resolve the CSV teams before looking for a previous import. A match may + // already exist in a legacy/generic league (for example "1. Kreisklasse"), + // while this CSV supplies the actual youth league. In that case it must be + // moved to the CSV league as well, otherwise it is classified as adult. const homeTeamId = await this.getOrCreateTeamId( - row['HeimMannschaft'], + row['HeimMannschaft'], row['HeimMannschaftAltersklasse'], clubId, league.id, @@ -216,6 +203,28 @@ class MatchService { league.id, season.id ); + + const existingMatch = await this.findExistingMatchForCSV( + clubId, + parsedDate, + homeTeamName, + guestTeamName + ); + if (existingMatch) { + const updates = {}; + if (location && existingMatch.locationId !== location.id) { + updates.locationId = location.id; + } + if (existingMatch.leagueId !== league.id) { + updates.leagueId = league.id; + updates.homeTeamId = homeTeamId; + updates.guestTeamId = guestTeamId; + } + if (Object.keys(updates).length > 0) { + await existingMatch.update(updates); + } + continue; + } matches.push({ date: parsedDate, time: row['Termin'].split(' ')[1],