fix: close competition cutover integrity gaps
This commit is contained in:
1 parent
7ea2002975
commit
683d602d63
57 files changed
+4805
-1582
No files matched your search
@@ -2,6 +2,7 @@ import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Injectable,
|
||||
InternalServerErrorException,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
@@ -381,60 +382,194 @@ export class LegacyImportService {
|
||||
.filter((id) => mongoose.isValidObjectId(id))
|
||||
.map((id) => new mongoose.Types.ObjectId(id));
|
||||
if (ids.length === 0) return { deleted: 0 };
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const targets = await MatchModel.find({
|
||||
_id: { $in: ids },
|
||||
communityId: params.communityId,
|
||||
legacyImported: true,
|
||||
})
|
||||
.select({ _id: 1 })
|
||||
.exec();
|
||||
const targetIds = targets.map((match) => match._id);
|
||||
if (targetIds.length === 0) return { deleted: 0 };
|
||||
|
||||
const [
|
||||
MatchModel,
|
||||
CompetitionModel,
|
||||
CompetitionFactModel,
|
||||
CompetitionOutboxModel,
|
||||
AssignmentModel,
|
||||
TournamentCodeModel,
|
||||
MatchStatsModel,
|
||||
connection,
|
||||
] = await Promise.all([
|
||||
this.connectionService.getModel('match'),
|
||||
this.connectionService.getModel('competition'),
|
||||
this.connectionService.getModel('competitionFact'),
|
||||
this.connectionService.getModel('competitionOutbox'),
|
||||
this.connectionService.getModel('tournamentCodeAssignment'),
|
||||
this.connectionService.getModel('tournamentCode'),
|
||||
this.connectionService.getModel('matchStats'),
|
||||
this.connectionService.getConnection(),
|
||||
]);
|
||||
const competitions = await CompetitionModel.find({
|
||||
matchId: { $in: targetIds },
|
||||
})
|
||||
.select({ _id: 1 })
|
||||
.lean()
|
||||
.exec();
|
||||
const competitionIds = competitions.map(({ _id }) => _id);
|
||||
if (competitionIds.length > 0) {
|
||||
await Promise.all([
|
||||
CompetitionFactModel.deleteMany({
|
||||
competitionId: { $in: competitionIds },
|
||||
}).exec(),
|
||||
CompetitionOutboxModel.deleteMany({
|
||||
competitionId: { $in: competitionIds },
|
||||
}).exec(),
|
||||
AssignmentModel.deleteMany({
|
||||
competitionId: { $in: competitionIds },
|
||||
}).exec(),
|
||||
]);
|
||||
await CompetitionModel.deleteMany({
|
||||
_id: { $in: competitionIds },
|
||||
}).exec();
|
||||
const session = await this.connectionService.createSession();
|
||||
if (!session) {
|
||||
throw new InternalServerErrorException(
|
||||
'A MongoDB transaction is required to delete legacy Matches.',
|
||||
);
|
||||
}
|
||||
|
||||
let deleted = 0;
|
||||
try {
|
||||
session.startTransaction();
|
||||
const targets = await MatchModel.find({
|
||||
_id: { $in: ids },
|
||||
communityId: params.communityId,
|
||||
legacyImported: true,
|
||||
})
|
||||
.select({ _id: 1 })
|
||||
.session(session)
|
||||
.exec();
|
||||
const targetIds = targets.map((match) => match._id);
|
||||
if (targetIds.length === 0) {
|
||||
await session.commitTransaction();
|
||||
} else {
|
||||
const competitions = await CompetitionModel.find({
|
||||
matchId: { $in: targetIds },
|
||||
})
|
||||
.select({ _id: 1 })
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
const competitionIds = competitions.map(({ _id }) => _id);
|
||||
if (competitionIds.length > 0) {
|
||||
const assignments = await AssignmentModel.find({
|
||||
competitionId: { $in: competitionIds },
|
||||
state: { $in: ['reserved', 'used'] },
|
||||
})
|
||||
.select({ tournamentCodeId: 1, state: 1 })
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
const assignmentCodeIds = assignments.map(
|
||||
({ tournamentCodeId }) => tournamentCodeId,
|
||||
);
|
||||
const uniqueAssignmentCodeIds = new Set(
|
||||
assignmentCodeIds.map((id) => id.toString()),
|
||||
);
|
||||
if (uniqueAssignmentCodeIds.size !== assignmentCodeIds.length) {
|
||||
throw new ConflictException(
|
||||
'Tournament Code assignments contain a duplicate active code identity.',
|
||||
);
|
||||
}
|
||||
const tournamentCodes =
|
||||
assignmentCodeIds.length === 0
|
||||
? []
|
||||
: await TournamentCodeModel.find({
|
||||
_id: { $in: assignmentCodeIds },
|
||||
})
|
||||
.select({ _id: 1, state: 1, matchId: 1 })
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
const tournamentCodeById = new Map(
|
||||
tournamentCodes.map((code) => [code._id.toString(), code]),
|
||||
);
|
||||
const targetIdSet = new Set(targetIds.map((id) => id.toString()));
|
||||
for (const assignment of assignments) {
|
||||
const code = tournamentCodeById.get(
|
||||
assignment.tournamentCodeId.toString(),
|
||||
);
|
||||
if (
|
||||
!code ||
|
||||
code.state !== assignment.state ||
|
||||
!code.matchId ||
|
||||
!targetIdSet.has(code.matchId.toString())
|
||||
) {
|
||||
throw new ConflictException(
|
||||
'Tournament Code assignment ownership or state does not match the target legacy Match.',
|
||||
);
|
||||
}
|
||||
}
|
||||
const releasedCodeIds = assignments
|
||||
.filter(({ state }) => state === 'reserved')
|
||||
.map(({ tournamentCodeId }) => tournamentCodeId);
|
||||
const voidedCodeIds = assignments
|
||||
.filter(({ state }) => state === 'used')
|
||||
.map(({ tournamentCodeId }) => tournamentCodeId);
|
||||
const terminalAt = new Date();
|
||||
if (releasedCodeIds.length > 0) {
|
||||
const released = await TournamentCodeModel.updateMany(
|
||||
{
|
||||
_id: { $in: releasedCodeIds },
|
||||
matchId: { $in: targetIds },
|
||||
state: 'reserved',
|
||||
},
|
||||
{
|
||||
$set: { state: 'released', releasedAt: terminalAt },
|
||||
$unset: legacyTournamentCodeBindingUnset(),
|
||||
},
|
||||
)
|
||||
.session(session)
|
||||
.exec();
|
||||
assertExactTournamentCodeTransition(
|
||||
released,
|
||||
releasedCodeIds.length,
|
||||
);
|
||||
}
|
||||
if (voidedCodeIds.length > 0) {
|
||||
const voided = await TournamentCodeModel.updateMany(
|
||||
{
|
||||
_id: { $in: voidedCodeIds },
|
||||
matchId: { $in: targetIds },
|
||||
state: 'used',
|
||||
},
|
||||
{
|
||||
$set: { state: 'voided', voidedAt: terminalAt },
|
||||
$unset: legacyTournamentCodeBindingUnset(),
|
||||
},
|
||||
)
|
||||
.session(session)
|
||||
.exec();
|
||||
assertExactTournamentCodeTransition(voided, voidedCodeIds.length);
|
||||
}
|
||||
|
||||
await CompetitionFactModel.deleteMany({
|
||||
competitionId: { $in: competitionIds },
|
||||
})
|
||||
.session(session)
|
||||
.exec();
|
||||
await CompetitionOutboxModel.deleteMany({
|
||||
competitionId: { $in: competitionIds },
|
||||
})
|
||||
.session(session)
|
||||
.exec();
|
||||
await AssignmentModel.deleteMany({
|
||||
competitionId: { $in: competitionIds },
|
||||
})
|
||||
.session(session)
|
||||
.exec();
|
||||
await CompetitionModel.deleteMany({
|
||||
_id: { $in: competitionIds },
|
||||
})
|
||||
.session(session)
|
||||
.exec();
|
||||
}
|
||||
|
||||
await connection
|
||||
.collection('competition_migration_quarantine')
|
||||
.deleteMany({ _id: { $in: targetIds } }, { session });
|
||||
const result = await MatchModel.deleteMany({
|
||||
_id: { $in: targetIds },
|
||||
communityId: params.communityId,
|
||||
legacyImported: true,
|
||||
})
|
||||
.session(session)
|
||||
.exec();
|
||||
await MatchStatsModel.deleteMany({ matchId: { $in: targetIds } })
|
||||
.session(session)
|
||||
.exec();
|
||||
deleted = result.deletedCount ?? 0;
|
||||
await session.commitTransaction();
|
||||
}
|
||||
} catch (error) {
|
||||
await session.abortTransaction().catch(() => undefined);
|
||||
throw error;
|
||||
} finally {
|
||||
await session.endSession().catch(() => undefined);
|
||||
}
|
||||
|
||||
const result = await MatchModel.deleteMany({
|
||||
_id: { $in: targetIds },
|
||||
communityId: params.communityId,
|
||||
legacyImported: true,
|
||||
}).exec();
|
||||
await this.matchStatsService.removeByMatches(targetIds);
|
||||
await this.communityStatsService.invalidateCommunity(params.communityId);
|
||||
return { deleted: result.deletedCount ?? 0 };
|
||||
return { deleted };
|
||||
}
|
||||
|
||||
public async rebuildLegacyMatches(params: {
|
||||
@@ -1322,6 +1457,29 @@ function optionalNumber(value: unknown): number | undefined {
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function legacyTournamentCodeBindingUnset(): Record<string, 1> {
|
||||
return {
|
||||
matchId: 1,
|
||||
gameNumber: 1,
|
||||
bracketMatchId: 1,
|
||||
reservedAt: 1,
|
||||
};
|
||||
}
|
||||
|
||||
function assertExactTournamentCodeTransition(
|
||||
result: { matchedCount?: number; modifiedCount?: number },
|
||||
expectedCount: number,
|
||||
): void {
|
||||
if (
|
||||
result.matchedCount !== expectedCount ||
|
||||
result.modifiedCount !== expectedCount
|
||||
) {
|
||||
throw new ConflictException(
|
||||
'Tournament Code transition did not update every validated assignment.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function deterministicObjectId(key: string): mongoose.Types.ObjectId {
|
||||
return new mongoose.Types.ObjectId(
|
||||
createHash('sha256').update(key).digest('hex').slice(0, 24),
|
||||
|
||||
Reference in new issue
Block a user