diff --git a/app/models/TournamentMatch.ts b/app/models/TournamentMatch.ts index e00028eb8..9bf99ceb0 100644 --- a/app/models/TournamentMatch.ts +++ b/app/models/TournamentMatch.ts @@ -63,9 +63,11 @@ export function deleteResult(id: string) { return db.tournamentMatchGameResult.delete({ where: { id } }); } -export function createParticipants( - data: ({ matchId: string; order: TeamOrder; teamId: string } | undefined)[] -) { +export type CreateParticipantsData = ( + | { matchId: string; order: TeamOrder; teamId: string } + | undefined +)[]; +export function createParticipants(data: CreateParticipantsData) { return db.tournamentMatchParticipant.createMany({ data: data.flatMap((result) => result ?? []), }); diff --git a/app/services/tournament.ts b/app/services/tournament.ts index 2ed202ae0..d2855ecc7 100644 --- a/app/services/tournament.ts +++ b/app/services/tournament.ts @@ -650,30 +650,9 @@ export async function reportScore({ winner: winnerTeam.order, }), // todo: bracket reset - TournamentMatch.createParticipants([ - match.winnerDestinationMatchId - ? { - matchId: match.winnerDestinationMatchId, - order: resolveNewOrder({ - bracket, - oldMatch: match, - newMatchId: match.winnerDestinationMatchId, - }), - teamId: winnerTeam.teamId, - } - : undefined, - match.loserDestinationMatchId - ? { - matchId: match.loserDestinationMatchId, - order: resolveNewOrder({ - bracket, - oldMatch: match, - newMatchId: match.loserDestinationMatchId, - }), - teamId: loserTeam?.teamId, - } - : undefined, - ]), + TournamentMatch.createParticipants( + newParticipantsForMatches({ bracket, match, loserTeam, winnerTeam }) + ), ]); // otherwise if set is not over simply create result and return } else { @@ -727,6 +706,43 @@ function resolveNewOrder({ return "LOWER"; } +function newParticipantsForMatches({ + bracket, + match, + winnerTeam, + loserTeam, +}: { + bracket: NonNullable; + match: NonNullable; + winnerTeam: Unpacked["participants"]>; + loserTeam: Unpacked["participants"]>; +}): TournamentMatch.CreateParticipantsData { + return [ + match.winnerDestinationMatchId + ? { + matchId: match.winnerDestinationMatchId, + order: resolveNewOrder({ + bracket, + oldMatch: match, + newMatchId: match.winnerDestinationMatchId, + }), + teamId: winnerTeam.teamId, + } + : undefined, + match.loserDestinationMatchId + ? { + matchId: match.loserDestinationMatchId, + order: resolveNewOrder({ + bracket, + oldMatch: match, + newMatchId: match.loserDestinationMatchId, + }), + teamId: loserTeam.teamId, + } + : undefined, + ]; +} + export async function undoLastScore({ matchId, position,