From c7fb4a70681044cbeb45396dca323aded6a0b99d Mon Sep 17 00:00:00 2001 From: Samuel Enocsson Date: Tue, 9 Jun 2026 11:04:53 +0200 Subject: [PATCH] fix: use re-fetched timestamp after recompute + rename helper var (#29) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewer 1 flagged: staleness-check read predicted_calculated_at from the original cachedPlayer snapshot even after recompute, so newly calculated ratings (predicted_calculated_at = NULL in snapshot) were immediately nulled by the staleness branch. Fix: read predicted_calculated_at from updatedPlayer too. Reviewer 2 nit: rename thisMonths → secondTuesday for consistency with the original variable name in getNextPDGAUpdateDate. --- src/services/player-service.js | 6 ++++-- src/services/rating-calculator.js | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/services/player-service.js b/src/services/player-service.js index 567c308..8465754 100644 --- a/src/services/player-service.js +++ b/src/services/player-service.js @@ -42,19 +42,21 @@ async function getPlayerDataFromDB(pdgaNumber, { includeMonthlyHistory = true } let stdDev = cachedPlayer.std_dev; let excludedRoundsCount = cachedPlayer.excluded_rounds_count; let cutoffRating = cachedPlayer.cutoff_rating; + let predictedCalculatedAtRaw = cachedPlayer.predicted_calculated_at; if (!predictedRating || predictedRating === 0) { predictedRating = await getPredictedRatingFromDB(pdgaNumber); const updatedPlayer = await getPlayerFromDB(pdgaNumber); stdDev = updatedPlayer?.std_dev; excludedRoundsCount = updatedPlayer?.excluded_rounds_count; cutoffRating = updatedPlayer?.cutoff_rating; + predictedCalculatedAtRaw = updatedPlayer?.predicted_calculated_at; } // Staleness-check: invalidate cached predicted_rating if the PDGA cycle has // rolled over since it was calculated. Don't recompute — round_history may be // equally stale. UI will show "—" until the next manual refresh. - const predictedCalculatedAt = cachedPlayer.predicted_calculated_at - ? new Date(cachedPlayer.predicted_calculated_at) + const predictedCalculatedAt = predictedCalculatedAtRaw + ? new Date(predictedCalculatedAtRaw) : null; const previousUpdate = getPreviousPDGAUpdateDate(); const hasPredicted = predictedRating !== null && predictedRating !== 0; diff --git a/src/services/rating-calculator.js b/src/services/rating-calculator.js index 4712b9e..77c6e13 100644 --- a/src/services/rating-calculator.js +++ b/src/services/rating-calculator.js @@ -51,10 +51,10 @@ function getNextPDGAUpdateDate() { const currentMonth = today.getMonth(); const currentYear = today.getFullYear(); - const thisMonths = secondTuesdayOf(currentYear, currentMonth); + const secondTuesday = secondTuesdayOf(currentYear, currentMonth); - if (today <= thisMonths) { - return thisMonths; + if (today <= secondTuesday) { + return secondTuesday; } else { const nextMonth = currentMonth === 11 ? 0 : currentMonth + 1; const nextYear = currentMonth === 11 ? currentYear + 1 : currentYear; @@ -66,8 +66,8 @@ function getPreviousPDGAUpdateDate() { const today = new Date(); const year = today.getFullYear(); const month = today.getMonth(); - const thisMonths = secondTuesdayOf(year, month); - if (today > thisMonths) return thisMonths; + const secondTuesday = secondTuesdayOf(year, month); + if (today > secondTuesday) return secondTuesday; // Otherwise: last month's second Tuesday const prevMonth = month === 0 ? 11 : month - 1; const prevYear = month === 0 ? year - 1 : year;