diff --git a/apps/api/src/archive/service.ts b/apps/api/src/archive/service.ts index 256e23a..f23d86f 100644 --- a/apps/api/src/archive/service.ts +++ b/apps/api/src/archive/service.ts @@ -23,6 +23,8 @@ const LOGIN_TTL_MS = 20 * 60_000; export const RESCAN_EVERY_MS = 6 * 3600_000; const MAX_ATTEMPTS = 3; const DAY_MS = 86_400_000; +/** A finished Liga Portugal league (18 clubs, 34 rounds). A past season with fewer games was only partly read. */ +const LIGA_LEAGUE_FULL = 280; /** Played FPA games of a season above which it counts as read (a national season has well over a thousand). */ const FPA_SEASON_KNOWN = 300; /** The season ("20252026", July to June) a game's day falls in. */ @@ -612,13 +614,16 @@ export class ArchiveService { const liga = (game.competition && /^liga|^allianz/i.test(game.competition)) || /liga/i.test(source?.baseUrl ?? ""); // The season: the folder's, else the one its day falls in. const season = game.season ?? (day ? seasonOfDay(day) : null); - if (season && liga && this.deps.ligaSeason && !this.seasonsAsked.has(`LIGA|${season}|${game.competition ?? ""}`)) { - this.seasonsAsked.add(`LIGA|${season}|${game.competition ?? ""}`); - // A cup already saved must not hide the league of the same season (2022/23 has the Allianz Cup and not Liga bwin). - const known = await this.deps.db.sportsFixture.count({ - where: { season, competition: { provider: "LIGA", ...(game.competition && { externalId: { equals: game.competition, mode: "insensitive" as const } }) } }, + if (season && liga && this.deps.ligaSeason && !this.seasonsAsked.has(`LIGA|${season}`)) { + this.seasonsAsked.add(`LIGA|${season}`); + // One read brings the league and the cup. A cup already stored, or a league only partly read, must not hide + // the rest of that year: a finished league is 306 games, and a past season under that is read again. + const league = await this.deps.db.sportsFixture.count({ + where: { season, competition: { provider: "LIGA", externalId: "ligaportugalbetclic" } }, }); - if (!known) await this.deps.ligaSeason(season).catch((err) => this.deps.log.warn({ err, season }, "archive: liga season not read")); + const past = season.replace(/\D/g, "") < seasonOf(lisbonDay(new Date())); + if ((past && league < LIGA_LEAGUE_FULL) || (!past && league === 0)) + await this.deps.ligaSeason(season).catch((err) => this.deps.log.warn({ err, season }, "archive: liga season not read")); } if (season && !liga && this.deps.fpaSeason && !this.seasonsAsked.has(`FPA|${season}`)) { this.seasonsAsked.add(`FPA|${season}`); diff --git a/apps/api/src/sports/service.ts b/apps/api/src/sports/service.ts index 0823f72..8be50b7 100644 --- a/apps/api/src/sports/service.ts +++ b/apps/api/src/sports/service.ts @@ -165,10 +165,8 @@ export class SportsService { let saved = 0; for (const month of [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]) { for (const f of await this.liga.month(month, season)) { - // A month can answer with another season's games. Those keep the season Liga named, never this one: - // the same fixture id updated in place would move a game out of the season it belongs to. - // Only a season Liga itself named. Guessing the season we asked for would move another year's game - // onto this one, because the fixture id is updated in place. + // Only a season Liga itself named. A fixture id is updated in place, so guessing the season we asked + // for would move another year's game onto this one. if (!f.seasonNamed) continue; const theirs = f.season.replace(/\D/g, ""); if (theirs !== asked) continue;