Files
pdga-rating/src/services/topbar-service.js
T
Samuel Enocsson d336156bbb fix: header "Next update" uses second Tuesday (closes #12)
The topbar showed the first Tuesday of the *next* month instead of
PDGA's actual cycle (second Tuesday of the month). Replace the
duplicated computeNextUpdate() with the central
getNextPDGAUpdateDate() from rating-calculator, keeping only the
formatter ("Tue 9 Jun") here.
2026-05-22 09:54:57 +02:00

41 lines
1.5 KiB
JavaScript

const { getLastRefresh } = require('../models/player');
const { getNextPDGAUpdateDate } = require('./rating-calculator');
const logger = require('../logger');
const DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const MONTH_NAMES = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function formatRelative(isoString) {
if (!isoString) return 'Never';
const then = new Date(isoString.replace(' ', 'T') + (isoString.endsWith('Z') ? '' : 'Z'));
const diffMs = Date.now() - then.getTime();
if (Number.isNaN(diffMs) || diffMs < 0) return 'Just now';
const sec = Math.floor(diffMs / 1000);
if (sec < 60) return 'Just now';
const min = Math.floor(sec / 60);
if (min < 60) return `${min} min ago`;
const hr = Math.floor(min / 60);
if (hr < 24) return `${hr} h ago`;
const day = Math.floor(hr / 24);
if (day === 1) return 'Yesterday';
if (day < 7) return `${day} days ago`;
return then.toISOString().slice(0, 10);
}
function formatNextUpdate(date) {
return `${DAY_NAMES[date.getDay()]} ${date.getDate()} ${MONTH_NAMES[date.getMonth()]}`;
}
async function getTopbarLocals() {
const nextUpdate = formatNextUpdate(getNextPDGAUpdateDate());
try {
const lastIso = await getLastRefresh();
return { lastRefresh: formatRelative(lastIso), nextUpdate };
} catch (err) {
logger.warn({ err }, 'topbar locals fallback');
return { lastRefresh: 'Unknown', nextUpdate };
}
}
module.exports = { getTopbarLocals };