fix: prevent empty-team auction completion state
This commit is contained in:
1 parent
2984bd1604
commit
ab721873f3
6 files changed
+748
-452
No files matched your search
@@ -226,8 +226,10 @@ describe('TournamentCodeService round-robin scheduling', () => {
|
||||
(game) => game.roundNumber === roundNumber,
|
||||
);
|
||||
expect(round).toHaveLength(4);
|
||||
expect(new Set(round.flatMap((game) => [game.teamAIndex, game.teamBIndex])).size)
|
||||
.toBe(8);
|
||||
expect(
|
||||
new Set(round.flatMap((game) => [game.teamAIndex, game.teamBIndex]))
|
||||
.size,
|
||||
).toBe(8);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -405,6 +407,69 @@ describe('TournamentCodeService pool stats', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('TournamentCodeService pending match attachment', () => {
|
||||
it('does not attach tournament codes before match teams are built', async () => {
|
||||
const invalidMatch = {
|
||||
...createMatch({
|
||||
status: 'team-built',
|
||||
teams: [],
|
||||
tournamentCodeIds: [],
|
||||
} as unknown as MatchOverrides),
|
||||
isModified: jest.fn().mockReturnValue(false),
|
||||
};
|
||||
const validMatch = {
|
||||
...createMatch({
|
||||
status: 'team-built',
|
||||
teams: [
|
||||
{
|
||||
teamIndex: 1,
|
||||
members: [{ userId: new mongoose.Types.ObjectId() }],
|
||||
},
|
||||
{
|
||||
teamIndex: 2,
|
||||
members: [{ userId: new mongoose.Types.ObjectId() }],
|
||||
},
|
||||
],
|
||||
tournamentCodeIds: [],
|
||||
} as unknown as MatchOverrides),
|
||||
isModified: jest.fn().mockReturnValue(false),
|
||||
};
|
||||
const codeId = new mongoose.Types.ObjectId();
|
||||
const MatchModel = {
|
||||
find: jest.fn().mockReturnValue({
|
||||
exec: jest.fn().mockResolvedValue([invalidMatch, validMatch]),
|
||||
}),
|
||||
findById: jest.fn().mockResolvedValue(validMatch),
|
||||
};
|
||||
const TournamentCodeModel = {
|
||||
findOneAndUpdate: jest.fn().mockReturnValue({
|
||||
exec: jest.fn().mockResolvedValue({ _id: codeId }),
|
||||
}),
|
||||
};
|
||||
const connectionService = {
|
||||
getModel: jest.fn().mockImplementation((modelName: string) => {
|
||||
if (modelName === 'match') return Promise.resolve(MatchModel);
|
||||
if (modelName === 'tournamentCode') {
|
||||
return Promise.resolve(TournamentCodeModel);
|
||||
}
|
||||
throw new Error(`Unexpected model: ${modelName}`);
|
||||
}),
|
||||
};
|
||||
const service = new TournamentCodeService(connectionService as never);
|
||||
|
||||
const matches = await service.attachPendingMatchDocuments();
|
||||
|
||||
expect(matches).toEqual([validMatch]);
|
||||
expect(MatchModel.findById).toHaveBeenCalledTimes(1);
|
||||
expect(MatchModel.findById).toHaveBeenCalledWith(validMatch._id);
|
||||
expect(TournamentCodeModel.findOneAndUpdate).toHaveBeenCalledTimes(1);
|
||||
expect(validMatch.tournamentCodeIds.map((id) => id.toString())).toEqual([
|
||||
codeId.toString(),
|
||||
]);
|
||||
expect(invalidMatch.tournamentCodeIds).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('TournamentCodeService code replacement', () => {
|
||||
function queryResult<T>(value: T) {
|
||||
return { exec: jest.fn().mockResolvedValue(value) };
|
||||
|
||||
@@ -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 &&
|
||||
|
||||
Reference in new issue
Block a user