feat(tournament): add cleanup logic for orphaned matches

- Implemented a new method to clean up orphaned matches where at least one player no longer exists, enhancing data integrity in tournament management.
- Added a corresponding route and frontend functionality to trigger the cleanup process, allowing users to easily remove invalid match records.
- Updated localization strings to support the new feature, ensuring clarity in the user interface.
This commit is contained in:
Torsten Schulz (local)
2026-01-31 00:16:23 +01:00
parent 3fc1760b2c
commit 10e6d74d93
6 changed files with 76 additions and 0 deletions

View File

@@ -3201,6 +3201,38 @@ Ve // 2. Neues Turnier anlegen
await TournamentMatch.destroy({ where });
}
/**
* Entfernt Matches, bei denen mindestens ein Spieler nicht mehr existiert
* (z.B. gelöscht bevor die Aufräum-Logik beim Teilnehmerlöschen eingeführt wurde).
*/
async cleanupOrphanedMatches(userToken, clubId, tournamentId) {
await checkAccess(userToken, clubId);
const tournament = await Tournament.findByPk(tournamentId);
if (!tournament || tournament.clubId != clubId) {
throw new Error('Turnier nicht gefunden');
}
const members = await TournamentMember.findAll({ where: { tournamentId }, attributes: ['id'] });
const externals = await ExternalTournamentParticipant.findAll({ where: { tournamentId }, attributes: ['id'] });
const validIds = new Set([
...members.map(m => m.id),
...externals.map(e => e.id)
]);
const matches = await TournamentMatch.findAll({ where: { tournamentId } });
let deletedCount = 0;
for (const m of matches) {
const p1Exists = !m.player1Id || validIds.has(m.player1Id);
const p2Exists = !m.player2Id || validIds.has(m.player2Id);
if (!p1Exists || !p2Exists) {
await TournamentResult.destroy({ where: { matchId: m.id } });
await m.destroy();
deletedCount++;
}
}
return { deletedCount };
}
async removeParticipant(userToken, clubId, tournamentId, participantId) {
await checkAccess(userToken, clubId);