feat: add age group code extraction and link youth club teams to imported leagues
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 50s
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 50s
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user