From f83e3986b7b9c7e6e70cecb3b5bf9ee3ea35ab6f Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 17 Jul 2026 14:10:52 +0200 Subject: [PATCH] feat: add age group code extraction and link youth club teams to imported leagues --- backend/services/matchService.js | 48 ++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/backend/services/matchService.js b/backend/services/matchService.js index 3995c272..4427a131 100755 --- a/backend/services/matchService.js +++ b/backend/services/matchService.js @@ -79,6 +79,28 @@ class MatchService { .trim(); } + getAgeGroupCode(ageClass) { + const match = String(ageClass || '').match(/jugend\s+(\d+)/i); + return match ? `J${match[1]}` : null; + } + + async linkYouthClubTeamsToImportedLeagues(clubId, seasonId, leaguesByAgeGroup) { + if (!leaguesByAgeGroup.size) { + return; + } + + const clubTeams = await ClubTeam.findAll({ + where: { clubId, seasonId, teamAgeGroup: { [Op.ne]: 'adult' } } + }); + + for (const clubTeam of clubTeams) { + const leagueId = leaguesByAgeGroup.get(clubTeam.teamAgeGroup); + if (leagueId && clubTeam.leagueId !== leagueId) { + await clubTeam.update({ leagueId }); + } + } + } + async findExistingMatchForCSV(clubId, date, homeTeamName, guestTeamName) { if (Number.isNaN(date?.getTime?.())) { return null; @@ -117,6 +139,7 @@ class MatchService { await checkAccess(userToken, clubId); let seasonString = ''; const matches = []; + const importedYouthLeagues = new Map(); try { const fileStream = fs.createReadStream(filePath) // click-TT exports this file as Windows-1252 / ISO-8859-1. @@ -136,6 +159,12 @@ class MatchService { seasonId: season.id, }, }); + if (row['Runde'] !== 'Pokal') { + const ageGroup = this.getAgeGroupCode(row['Altersklasse']); + if (ageGroup) { + importedYouthLeagues.set(ageGroup, league.id); + } + } const homeTeamName = this.formatTeamNameWithAgeClass( row['HeimMannschaft'], row['HeimMannschaftAltersklasse'] @@ -166,7 +195,7 @@ class MatchService { homeTeamName, guestTeamName ); - if (existingMatch && existingMatch.leagueId !== league.id) { + if (existingMatch) { if (location && existingMatch.locationId !== location.id) { await existingMatch.update({ locationId: location.id }); } @@ -200,18 +229,15 @@ class MatchService { let season = null; if (seasonString) { season = await Season.findOne({ where: { season: seasonString } }); - if (season) { - // Lösche alle Matches für Ligen dieser Saison - const leagues = await League.findAll({ - where: { seasonId: season.id, clubId } - }); - const leagueIds = leagues.map(league => league.id); - if (leagueIds.length > 0) { - await Match.destroy({ where: { clubId, leagueId: leagueIds } }); - } - } } const result = await Match.bulkCreate(matches); + if (season) { + await this.linkYouthClubTeamsToImportedLeagues( + clubId, + season.id, + importedYouthLeagues + ); + } return result; } catch (error) {