All files / src/utils tournamentStatus.js

100% Statements 278/278
92.74% Branches 115/124
100% Functions 14/14
100% Lines 278/278

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 3081x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 4469x 4448x 4469x 4469x   1x 1x 1x 1x 1x 1x 1x 527x 527x 527x 527x 527x   1x 1x 10022x 10022x 10022x 10022x   1x 1x 1848x 1848x   1x 1x 1x 1x 1x 603x 603x 602x 603x 535x 527x 512x 512x   1x 1x 1x 1x 1x 1x 1x 1x 33x   30x 30x 30x   33x 1x 1x 1x 33x 32x 25x 25x 25x 9x 1x 1x   1x 1x 1x 1x 1x 1x 375x 375x 375x 375x 375x 375x 375x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 628x   627x 628x 628x   622x 622x 622x 165x 622x   622x 622x 622x   622x 627x 612x 628x 137x 137x 137x 137x 137x 137x   601x 151x 151x   541x 235x 235x 235x 217x 235x 235x   99x 99x 99x 628x 628x 628x 628x 628x 628x   1x 754x 746x 746x 746x 746x 746x 746x 746x 746x 746x   1x 1x 1x 1x 1x 1x 379x 379x 379x 379x 379x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 120x 120x 120x 120x 120x   120x 120x 120x 14x 14x 14x 120x 120x   1x 1x 1x 1x 1x 1x 1x 1x 1x 211x 211x 1118x 1118x 1118x 1118x 211x 224x 224x 211x 211x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2505x 2505x 2409x 2492x 2407x 2407x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1228x 1228x 26x 26x 26x 1228x 13x 13x 13x 1196x 1196x  
/**
 * Tournament status vocabulary (SB-886).
 *
 * The Tournaments tab used to state only what a tournament *is* (a name, a
 * crest, a match count). These helpers let it state where the tournament is in
 * its own life — "Starts tomorrow", "Live now", "Completed" — which is the
 * difference between a schedule tracker and a companion.
 *
 * Everything here is a pure function of (tournament, matches, now) so it can be
 * unit-tested without a database or a clock.
 */
 
/**
 * Statuses under which a match has a real scoreline.
 *
 * `live` is the value the database actually stores (the `match_status` enum has
 * no `in_progress` member). This vocabulary was written against `in_progress`,
 * so until SB-910 a match under way scored 0-0 in the tournament view: it hit
 * neither list, so its scoreline was treated as absent and its status label
 * rendered as nothing at all. `in_progress` stays as an alias rather than a
 * rename, so any payload still using it keeps working.
 */
export const SCORED_STATUSES = ['completed', 'live', 'in_progress', 'forfeit'];
 
/** Statuses that count as "this tournament is happening right now". */
const LIVE_STATUSES = ['live', 'in_progress'];
 
const MS_PER_DAY = 86400000;
 
/**
 * Parse a `YYYY-MM-DD` date column as local midnight.
 *
 * `new Date('2026-08-29')` parses as UTC midnight, which is the previous
 * evening in every US timezone — the classic off-by-one that makes a Saturday
 * tournament say "Starts today" on Friday night. Appending a time forces local.
 */
export function parseDateOnly(value) {
  if (!value) return null;
  const d = new Date(`${String(value).slice(0, 10)}T00:00:00`);
  return Number.isNaN(d.getTime()) ? null : d;
}
 
/**
 * `YYYY-MM-DD` for the viewer's local calendar day.
 *
 * `toISOString().slice(0, 10)` is UTC, which flips to tomorrow at 7–8pm on the
 * US east coast — the exact hours a parent checks tomorrow's Fest schedule.
 */
export function localDateKey(now = new Date()) {
  const y = now.getFullYear();
  const m = String(now.getMonth() + 1).padStart(2, '0');
  const d = String(now.getDate()).padStart(2, '0');
  return `${y}-${m}-${d}`;
}
 
/** Local midnight at the start of whatever day `date` falls on. */
export function startOfDay(date) {
  const d = new Date(date);
  d.setHours(0, 0, 0, 0);
  return d;
}
 
/** Whole days from `from` to `to`, counted in calendar days, not 24h blocks. */
export function daysBetween(from, to) {
  return Math.round((startOfDay(to) - startOfDay(from)) / MS_PER_DAY);
}
 
/**
 * "Today" / "Tomorrow" / "Yesterday" for a match date, or null when the day is
 * far enough away that the weekday alone reads better.
 */
export function relativeDayLabel(dateValue, now = new Date()) {
  const date = parseDateOnly(dateValue);
  if (!date) return null;
  const delta = daysBetween(now, date);
  if (delta === 0) return 'Today';
  if (delta === 1) return 'Tomorrow';
  if (delta === -1) return 'Yesterday';
  return null;
}
 
/**
 * Human countdown for a span in milliseconds.
 *
 * Resolution tightens as the moment approaches: weeks far out, days inside a
 * fortnight, hours and minutes inside two days. A parent three weeks out does
 * not want to see 503 hours; a parent the night before wants the minutes.
 */
export function formatCountdown(ms) {
  if (ms == null || Number.isNaN(ms) || ms <= 0) return null;
 
  const minutes = Math.floor(ms / 60000);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);
 
  if (days >= 14) {
    const weeks = Math.round(days / 7);
    return `${weeks} weeks`;
  }
  if (days >= 2) return `${days} days`;
  if (hours >= 1) {
    const m = minutes % 60;
    return m > 0 ? `${hours}h ${m}m` : `${hours}h`;
  }
  if (minutes >= 1) return `${minutes}m`;
  return 'any moment';
}
 
/**
 * The earliest kickoff still in the future, as an ISO string, or null.
 * Matches with no `scheduled_kickoff` are skipped — a date alone cannot drive a
 * countdown, and guessing a kickoff time is worse than showing none.
 */
export function nextKickoff(matches = [], now = new Date()) {
  const upcoming = matches
    .map(m => m.scheduled_kickoff)
    .filter(Boolean)
    .filter(k => new Date(k) > now)
    .sort();
  return upcoming[0] ?? null;
}
 
/**
 * Where a tournament sits in its own life.
 *
 * Returns `{ state, label, countdownTo }`:
 *   state       'upcoming' | 'soon' | 'live' | 'completed'
 *   label       display string for the status ribbon and selector chip
 *   countdownTo ISO kickoff to tick against, or null when a countdown would
 *               be noise (too far out, or nothing left to start)
 *
 * `null` is returned when the tournament has no dates at all — the caller
 * renders no ribbon rather than inventing a state. A tournament with dates we
 * cannot parse is not "upcoming"; it is unknown, and unknown renders as absent.
 */
export function tournamentStatus(tournament, matches = [], now = new Date()) {
  if (!tournament) return null;
 
  const start = parseDateOnly(tournament.start_date);
  const end = parseDateOnly(tournament.end_date) ?? start;
  if (!start) return null;
 
  // A match in progress beats any date arithmetic: the tournament is live
  // whatever the calendar says.
  const hasLiveMatch = matches.some(m =>
    LIVE_STATUSES.includes(m.match_status)
  );
 
  const today = startOfDay(now);
  const daysToStart = daysBetween(now, start);
  const daysAfterEnd = daysBetween(end, now);
 
  if (
    hasLiveMatch ||
    (today >= startOfDay(start) && today <= startOfDay(end))
  ) {
    return {
      state: 'live',
      label: hasLiveMatch ? 'Live now' : 'Happening today',
      countdownTo: nextKickoff(matches, now),
    };
  }
 
  if (daysAfterEnd > 0) {
    return { state: 'completed', label: 'Completed', countdownTo: null };
  }
 
  if (daysToStart <= 7) {
    let label;
    if (daysToStart <= 0) label = 'Starts today';
    else if (daysToStart === 1) label = 'Starts tomorrow';
    else label = `Starts in ${daysToStart} days`;
    return { state: 'soon', label, countdownTo: nextKickoff(matches, now) };
  }
 
  const weeks = Math.round(daysToStart / 7);
  return {
    state: 'upcoming',
    label: daysToStart <= 13 ? `In ${daysToStart} days` : `In ${weeks} weeks`,
    // No countdown this far out — a ticking clock on something three weeks
    // away is decoration, not information.
    countdownTo: null,
  };
}
 
const formatTournamentDate = value => {
  if (!value) return '';
  return new Date(`${String(value).slice(0, 10)}T00:00:00`).toLocaleDateString(
    'en-US',
    {
      month: 'short',
      day: 'numeric',
      year: 'numeric',
    }
  );
};
 
/**
 * "Aug 29, 2026 – Aug 31, 2026", or the start date alone for a one-day
 * tournament, or '' when there is no start date. Shared by the hero card and
 * the season index so the two never print a tournament's dates differently.
 */
export function formatTournamentDateRange(tournament) {
  const start = formatTournamentDate(tournament?.start_date);
  if (!start) return '';
  const end = formatTournamentDate(tournament?.end_date);
  return end && end !== start ? `${start} – ${end}` : start;
}
 
/**
 * Sort order for the tournament selector chips: live first, then what is about
 * to start, then the rest of the calendar, then what is finished.
 *
 * Within a state the tie-break follows what the reader is looking for: for
 * anything still ahead, soonest first; for anything finished, most recent
 * first, because the tournament someone wants to revisit is the one that just
 * ended, not the one from last autumn.
 */
const STATE_RANK = { live: 0, soon: 1, upcoming: 2, completed: 3 };
 
export function compareByStatus(a, b, now = new Date()) {
  const sa = tournamentStatus(a, a?.matches ?? [], now);
  const sb = tournamentStatus(b, b?.matches ?? [], now);
  const ra = STATE_RANK[sa?.state] ?? 2;
  const rb = STATE_RANK[sb?.state] ?? 2;
  if (ra !== rb) return ra - rb;
 
  const da = a?.start_date ?? '';
  const db = b?.start_date ?? '';
  if (da !== db) {
    const ascending = da < db ? -1 : 1;
    return ra === STATE_RANK.completed ? -ascending : ascending;
  }
  return (a?.id ?? 0) - (b?.id ?? 0);
}
 
/**
 * Group matches by their `match_date`, preserving the order they arrive in.
 *
 * Callers pass already-sorted matches, so day order and within-day order both
 * come from the caller's sort rather than being re-derived here. Matches with
 * no date collect under a `null` key so they still render somewhere instead of
 * silently vanishing.
 */
export function groupMatchesByDay(matches = []) {
  const days = new Map();
  for (const m of matches) {
    const key = m.match_date ? String(m.match_date).slice(0, 10) : null;
    if (!days.has(key)) days.set(key, []);
    days.get(key).push(m);
  }
  return [...days.entries()].map(([date, dayMatches]) => ({
    date,
    matches: dayMatches,
  }));
}
 
/**
 * Statuses that mean "no result has been recorded yet".
 *
 * `cancelled` is deliberately absent: that is a real, known outcome, not a
 * missing one, and it should keep saying so however old it gets.
 */
const UNRESOLVED_STATUSES = ['scheduled', 'tbd'];
 
/**
 * A match whose date has passed but which never received a result (SB-889).
 *
 * "Scheduled" is a claim about the future. On a fixture that kicked off in
 * June it is simply false — the match was played, and the score never reached
 * us. Callers render these as "Not reported": that states what we hold without
 * claiming the match was unplayed (we have no evidence of that) or promising a
 * result is still coming (the oldest of these dates from 2024).
 *
 * The boundary is the calendar day, not kickoff time. A match that started two
 * hours ago and has not been updated is normal mid-tournament; flipping it
 * during the day would be wrong, and would flicker while someone is
 * live-scoring. Only a date strictly before today counts.
 */
export function isMissingResult(match, now = new Date()) {
  if (!match) return false;
  if (!UNRESOLVED_STATUSES.includes(match.match_status)) return false;
  const date = parseDateOnly(match.match_date);
  if (!date) return false;
  return startOfDay(date) < startOfDay(now);
}
 
/**
 * Does this match involve the signed-in user's club or team?
 *
 * Club is checked first because a parent follows a club across age groups;
 * team is the fallback for accounts whose profile carries only `team_id`.
 * Signed out (both ids null) this is always false — the highlight is a silent
 * no-op rather than a state the page has to branch on.
 */
export function isMyMatch(match, { clubId = null, teamId = null } = {}) {
  if (!match) return false;
  if (clubId != null) {
    if (match.home_team_club?.id === clubId) return true;
    if (match.away_team_club?.id === clubId) return true;
  }
  if (teamId != null) {
    if (match.home_team?.id === teamId) return true;
    if (match.away_team?.id === teamId) return true;
  }
  return false;
}