diff --git a/backend/services/matchService.js b/backend/services/matchService.js index 29107ee9..ebe29ad3 100755 --- a/backend/services/matchService.js +++ b/backend/services/matchService.js @@ -79,6 +79,26 @@ class MatchService { .trim(); } + isTeamNameForClub(teamName, clubName) { + const normalize = (name) => String(name || '') + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .toLowerCase() + // Vereinsnamen im click-TT-Export enthalten teils eine Jahreszahl, + // die im hinterlegten Vereinsnamen fehlt (z. B. TSV Bonames / TSV 1875 Bonames). + .replace(/\b\d+\b/g, ' ') + .replace(/[^\p{L}]+/gu, ' ') + .replace(/\s+/g, ' ') + .trim(); + + const normalizedClubName = normalize(clubName); + const normalizedTeamName = normalize(teamName); + return Boolean(normalizedClubName) && ( + normalizedTeamName === normalizedClubName || + normalizedTeamName.startsWith(`${normalizedClubName} `) + ); + } + getAgeGroupCode(ageClass) { const match = String(ageClass || '').match(/jugend\s+(\d+)/i); return match ? `J${match[1]}` : null; @@ -333,14 +353,13 @@ class MatchService { const leagueIdList = leaguesInSeason.map((l) => l.id); let ownTeamIdSet = new Set(); if (club?.name && leagueIdList.length) { - const escaped = String(club.name).replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_'); - const ownTeams = await Team.findAll({ - where: { - leagueId: { [Op.in]: leagueIdList }, - name: { [Op.like]: `${escaped}%` } - }, - attributes: ['id'] + const teamsInSeason = await Team.findAll({ + where: { leagueId: { [Op.in]: leagueIdList } }, + attributes: ['id', 'name'] }); + const ownTeams = teamsInSeason.filter((team) => + this.isTeamNameForClub(team.name, club.name) + ); ownTeamIdSet = new Set(ownTeams.map((t) => t.id)); } @@ -437,17 +456,13 @@ class MatchService { } }); } else { - const clubName = club.name; - - const ownTeams = await Team.findAll({ - where: { - name: { - [Op.like]: `${clubName}%` - }, - leagueId: leagueId - }, + const teamsInLeague = await Team.findAll({ + where: { leagueId: leagueId }, attributes: ['id', 'name'] }); + const ownTeams = teamsInLeague.filter((team) => + this.isTeamNameForClub(team.name, club.name) + ); const ownTeamIds = ownTeams.map(t => t.id);