fix(riot): persist refreshed tier state

This commit is contained in:
dahyeon committed 2026-06-07 07:59:11 -07:00
1 parent ef693fcf4a
commit be5383a2e9
2 files changed
+292 -2

No files matched your search

@@ -3,6 +3,7 @@ import mongoose from 'mongoose';
import { ConnectionService } from '../global/database/connection.service';
import {
ChampionMasteryEntry,
MatchStatus,
RiotAccount,
TierInfo,
} from '../global/database/schema';
@@ -33,6 +34,11 @@ const DEFAULT_SUMMONER_TTL_MS = 24 * 60 * 60 * 1000; // 24h
const DEFAULT_LEAGUE_TTL_MS = 60 * 60 * 1000; // 1h
const DEFAULT_MASTERY_TTL_MS = 6 * 60 * 60 * 1000; // 6h
const DEFAULT_RECENT_MATCHES_TTL_MS = 6 * 60 * 60 * 1000; // 6h
const ACTIVE_MATCH_STATUSES: MatchStatus[] = [
'recruiting',
'team-built',
'in-progress',
];
export interface EnrichOptions {
platform?: RiotPlatformRoute;
@@ -117,6 +123,11 @@ export class RiotAccountEnrichmentService {
this.applyLeagueEntries(account, outcome.dto);
await user.save();
await this.syncActiveMatchParticipantTier(
user._id,
params.puuid,
account.tiers,
);
return 'updated';
}
@@ -243,6 +254,13 @@ export class RiotAccountEnrichmentService {
) {
await user.save();
}
if (leagueOutcome.status === 'updated') {
await this.syncActiveMatchParticipantTier(
user._id,
params.puuid,
account.tiers,
);
}
return {
summoner: summonerOutcome.status,
@@ -425,7 +443,10 @@ export class RiotAccountEnrichmentService {
const changed =
!last ||
last.tier !== entry.tier ||
(last.rank ?? '') !== (entry.rank ?? '');
(last.rank ?? '') !== (entry.rank ?? '') ||
last.leaguePoints !== entry.leaguePoints ||
last.wins !== entry.wins ||
last.losses !== entry.losses;
if (!changed) continue;
account.tierHistory.push({
@@ -463,6 +484,62 @@ export class RiotAccountEnrichmentService {
account.matchIdsFetchedAt = fetchedAt;
}
private async syncActiveMatchParticipantTier(
userId: mongoose.Types.ObjectId,
puuid: string,
tiers: TierInfo[],
): Promise<void> {
const MatchModel = await this.connectionService.getModel('match');
const tier = this.selectParticipantTier(tiers);
const update = tier
? { $set: { 'participants.$[participant].tier': tier } }
: { $unset: { 'participants.$[participant].tier': '' } };
await MatchModel.updateMany(
{
status: { $in: ACTIVE_MATCH_STATUSES },
participants: {
$elemMatch: {
userId,
puuid,
},
},
},
update,
{
arrayFilters: [
{
'participant.userId': userId,
'participant.puuid': puuid,
},
],
},
).exec();
}
private selectParticipantTier(tiers: TierInfo[]): TierInfo | undefined {
const tier =
tiers.find((entry) => entry.queueType === 'RANKED_SOLO_5x5') ?? tiers[0];
if (!tier) return undefined;
return {
leagueId: tier.leagueId,
queueType: tier.queueType,
tier: tier.tier,
rank: tier.rank,
puuid: tier.puuid,
leaguePoints: tier.leaguePoints,
wins: tier.wins,
losses: tier.losses,
veteran: tier.veteran,
inactive: tier.inactive,
freshBlood: tier.freshBlood,
hotStreak: tier.hotStreak,
miniSeries: tier.miniSeries,
fetchedAt: tier.fetchedAt,
};
}
private tiersOldestAt(tiers: TierInfo[]): number {
if (tiers.length === 0) return 0;
let oldest = Infinity;