refactor: rebuild stats from competition revision

This commit is contained in:
dahyeon committed 2026-07-12 19:28:04 +00:00
1 parent 28e9ac1529
commit e6e434d13f
4 files changed
+412 -7

No files matched your search

@@ -21,9 +21,44 @@ export class MatchStatsRepository {
fragment: Omit<MatchStats, 'createdAt' | 'updatedAt'>,
): Promise<void> {
const MatchStatsModel = await this.connectionService.getModel('matchStats');
if (
!fragment.sourceCompetitionId ||
fragment.sourceRevision === undefined
) {
await MatchStatsModel.updateOne(
{ matchId: fragment.matchId },
{ $set: fragment },
{ upsert: true },
).exec();
return;
}
const updated = await MatchStatsModel.updateOne(
{
matchId: fragment.matchId,
$or: [
{ sourceCompetitionId: { $exists: false } },
{ sourceCompetitionId: fragment.sourceCompetitionId },
],
$and: [
{
$or: [
{ sourceRevision: { $exists: false } },
{ sourceRevision: { $lte: fragment.sourceRevision } },
],
},
],
},
{ $set: fragment },
{ upsert: false },
).exec();
if (updated.matchedCount > 0) return;
// If no read model exists, insert it. If a newer revision won the race,
// this filter matches it and $setOnInsert intentionally changes nothing.
await MatchStatsModel.updateOne(
{ matchId: fragment.matchId },
{ $set: fragment },
{ $setOnInsert: fragment },
{ upsert: true },
).exec();
}