fix: prevent empty-team auction completion state

This commit is contained in:
dahyeon committed 2026-06-25 05:26:09 -07:00
1 parent 2984bd1604
commit ab721873f3
6 files changed
+748 -452

No files matched your search

@@ -164,10 +164,9 @@ export class TournamentCodeService {
_id: TournamentCodeState;
count: number;
}>([{ $group: { _id: '$state', count: { $sum: 1 } } }]).exec();
const byState = Object.fromEntries(states.map((state) => [state, 0])) as Record<
TournamentCodeState,
number
>;
const byState = Object.fromEntries(
states.map((state) => [state, 0]),
) as Record<TournamentCodeState, number>;
for (const entry of counts) byState[entry._id] = entry.count;
const expiringSoon = await TournamentCodeModel.countDocuments({
@@ -189,7 +188,9 @@ export class TournamentCodeService {
state: 'available',
}).exec();
if (result.deletedCount === 0) {
throw new BadRequestException('Only available tournament codes can be deleted.');
throw new BadRequestException(
'Only available tournament codes can be deleted.',
);
}
}
@@ -249,7 +250,9 @@ export class TournamentCodeService {
const reservedIds: mongoose.Types.ObjectId[] = [];
const startGameNumber = match.tournamentCodeIds.length + 1;
const expiresAfter = new Date(Date.now() + TOURNAMENT_CODE_EXPIRY_MARGIN_MS);
const expiresAfter = new Date(
Date.now() + TOURNAMENT_CODE_EXPIRY_MARGIN_MS,
);
for (let index = 0; index < need; index += 1) {
const code = await TournamentCodeModel.findOneAndUpdate(
@@ -334,10 +337,14 @@ export class TournamentCodeService {
throw new NotFoundException('Assigned tournament code not found.');
}
if (currentCode.state === 'used' || currentCode.riotMatchId) {
throw new BadRequestException('Used tournament codes cannot be replaced.');
throw new BadRequestException(
'Used tournament codes cannot be replaced.',
);
}
const expiresAfter = new Date(Date.now() + TOURNAMENT_CODE_EXPIRY_MARGIN_MS);
const expiresAfter = new Date(
Date.now() + TOURNAMENT_CODE_EXPIRY_MARGIN_MS,
);
const replacement = await TournamentCodeModel.findOneAndUpdate(
{
state: 'available',
@@ -358,7 +365,9 @@ export class TournamentCodeService {
{ sort: { expiresAt: 1 }, returnDocument: 'after' },
).exec();
if (!replacement) {
throw new BadRequestException('No available tournament code to replace with.');
throw new BadRequestException(
'No available tournament code to replace with.',
);
}
currentCode.state = 'released';
@@ -449,6 +458,7 @@ export class TournamentCodeService {
const matches = await MatchModel.find(filter).exec();
const touched: MatchDocument[] = [];
for (const match of matches) {
if (!this.hasBuiltTeams(match)) continue;
if (
(match.tournamentCodeIds?.length ?? 0) >=
this.resolveRequiredSlots(match)
@@ -463,7 +473,16 @@ export class TournamentCodeService {
return touched;
}
public async buildSlots(match: MatchDocument): Promise<TournamentCodeSlotView[]> {
private hasBuiltTeams(match: MatchDocument): boolean {
return Boolean(
match.teams.length >= 2 &&
match.teams.every((team) => team.members.length > 0),
);
}
public async buildSlots(
match: MatchDocument,
): Promise<TournamentCodeSlotView[]> {
if (
(match.gameMode ?? 'pair') === 'round-robin' &&
(match.gameSchedule?.length ?? 0) === 0 &&