diff --git a/apps/api/src/modules/community-stats/match-stats.service.spec.ts b/apps/api/src/modules/community-stats/match-stats.service.spec.ts index eee97b81..4af50388 100644 --- a/apps/api/src/modules/community-stats/match-stats.service.spec.ts +++ b/apps/api/src/modules/community-stats/match-stats.service.spec.ts @@ -206,7 +206,7 @@ describe('MatchStatsService', () => { { getMatchTimeline: jest.fn() } as never, ); - await service.buildAndUpsert(matchId); + await service.buildAndUpsert(matchId, { sourceRevision: 12 }); expect(riotFind).toHaveBeenCalledWith({ matchId: { $in: ['KR_COMPETITION'] }, @@ -236,6 +236,54 @@ describe('MatchStatsService', () => { }), ); }); + + it('does not rebuild before the requested Competition revision is visible', async () => { + const matchId = new mongoose.Types.ObjectId(); + const riotFind = jest.fn(); + const connectionService = { + getModel: jest.fn().mockImplementation(async (name: string) => { + if (name === 'match') { + return { + findById: () => + queryResult( + { + _id: matchId, + status: 'in-progress', + }, + false, + ), + }; + } + if (name === 'competition') { + return { + findOne: () => queryResult({ revision: 11 }), + }; + } + if (name === 'riotMatch') return { find: riotFind }; + throw new Error(`Unexpected model: ${name}`); + }), + } as unknown as ConnectionService; + const repository = { + upsert: jest.fn(), + deleteByMatch: jest.fn(), + } as unknown as jest.Mocked; + const builder = { + buildFragment: jest.fn(), + } as unknown as jest.Mocked; + const service = new MatchStatsService( + connectionService, + repository, + builder, + { getMatchTimeline: jest.fn() } as never, + ); + + await service.buildAndUpsert(matchId, { sourceRevision: 12 }); + + expect(riotFind).not.toHaveBeenCalled(); + expect(builder.buildFragment).not.toHaveBeenCalled(); + expect(repository.upsert).not.toHaveBeenCalled(); + expect(repository.deleteByMatch).not.toHaveBeenCalled(); + }); }); function participant( diff --git a/apps/api/src/modules/community-stats/match-stats.service.ts b/apps/api/src/modules/community-stats/match-stats.service.ts index 1e363df9..865397f8 100644 --- a/apps/api/src/modules/community-stats/match-stats.service.ts +++ b/apps/api/src/modules/community-stats/match-stats.service.ts @@ -26,7 +26,7 @@ export class MatchStatsService { public async buildAndUpsert( matchId: mongoose.Types.ObjectId, - options: { includeTimeline?: boolean } = {}, + options: { includeTimeline?: boolean; sourceRevision?: number } = {}, ): Promise { const MatchModel = await this.connectionService.getModel('match'); const CompetitionModel = @@ -37,6 +37,12 @@ export class MatchStatsService { const competition = await CompetitionModel.findOne({ matchId }) .lean() .exec(); + if ( + options.sourceRevision !== undefined && + (!competition || competition.revision < options.sourceRevision) + ) { + return; + } const competitionSource = competition ? competitionStatsSource(match, competition) : undefined; @@ -148,7 +154,10 @@ export class MatchStatsService { const matches = await MatchModel.find({ communityId, status: { $in: ['finished', 'in-progress'] }, - 'result.games.0': { $exists: true }, + $or: [ + { competitionId: { $exists: true } }, + { 'result.games.0': { $exists: true } }, + ], }) .sort({ scheduledAt: -1 }) .select({ _id: 1 })