From f5fb4bbea2aaae4597b9bcce031a3006a1d55b51 Mon Sep 17 00:00:00 2001 From: spark Date: Wed, 14 Oct 2026 14:07:32 +0100 Subject: [PATCH] Liga: referees from the round nominations; squads re-read when updating people "Update people from the squads" now reads the two Liga squads again on the spot (players and coaching staff) instead of waiting for the 6-hourly refresh, and for Liga matches takes the referee team on the pitch from the round's nominations (federation id, full name, the referee's official photo), kept as the Liga's referees so they are the same person from match to match. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW --- apps/api/src/sports/providers.ts | 35 ++++++++++++++++++++++++++++++++ apps/api/src/sports/service.ts | 35 ++++++++++++++++++++++++++++++-- apps/api/test/sports.test.ts | 19 +++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/apps/api/src/sports/providers.ts b/apps/api/src/sports/providers.ts index d599644..d02542d 100644 --- a/apps/api/src/sports/providers.ts +++ b/apps/api/src/sports/providers.ts @@ -62,6 +62,8 @@ export interface OfficialData { /** FPA: licence number (CIPA) and full name, from the match report. */ externalId?: string | null; fullName?: string | null; + /** Liga: the official photo (the referee's; assistants seldom have one). */ + photoUrl?: string | null; } const TIMEOUT_MS = 20_000; @@ -176,6 +178,39 @@ export class LigaProvider { .concat(staff); } + /** + * The officials Liga names for one match (the round's nominations, published days before): the referee team on + * the pitch and the VAR, with their federation id, full name and, for the referee, the official photo. + */ + async referees(competition: string, season: string, round: string, fixtureCode: string): Promise { + const list = await this.api(`competition/round/${encodeURIComponent(round)}/nominations`, { competition, season }); + if (!Array.isArray(list)) return []; + const n = (list as Record[]).find((x) => str(obj(x.fixture).fixtureCode) === fixtureCode); + if (!n) return []; + const roles: [string, string, string][] = [ + ["referee", "refereeName", "REFEREE"], + ["refereeAux1", "refereeAux1Name", "ASSISTANT_REFEREE_ONE"], + ["refereeAux2", "refereeAux2Name", "ASSISTANT_REFEREE_TWO"], + ["fourthReferee", "refereeFourthName", "FOURTH_OFFICIAL"], + ["var", "refereeVarName", "VIDEO_ASSISTANT_REFEREE"], + ["aVar", "refereeAVarName", "ASSISTANT_VIDEO_ASSISTANT_REFEREE"], + ]; + const out: OfficialData[] = []; + for (const [key, nameKey, role] of roles) { + const p = obj(n[key]); + const name = str(p.name) ?? str(n[nameKey]); + if (!name) continue; + out.push({ + name: name.replace(/\s+/g, " "), + role, + externalId: str(p.external_id), + fullName: str(p.fullName), + photoUrl: url(p.photos, p.photosFormats, role === "REFEREE" ? n.refereePhoto : undefined), + }); + } + return out; + } + /** Every Liga match of a month (all three competitions). */ async month(month: number, season: string): Promise { const days = await this.api(`calendar/matches/month/${month}`, { season }); diff --git a/apps/api/src/sports/service.ts b/apps/api/src/sports/service.ts index 5f5c038..b6980b4 100644 --- a/apps/api/src/sports/service.ts +++ b/apps/api/src/sports/service.ts @@ -737,7 +737,7 @@ export class SportsService { include: { homeTeam: true, awayTeam: true, - fixture: { select: { id: true, externalId: true, officials: true, competition: { select: { provider: true } } } }, + fixture: { select: { id: true, externalId: true, round: true, officials: true, competition: { select: { provider: true, externalId: true, season: true } } } }, organization: { select: { biometricsEnabled: true, codeGenerator: true } }, }, }); @@ -746,6 +746,16 @@ export class SportsService { const orgId = event.organizationId; const result = { created: 0, updated: 0, headshots: 0 }; const headshots: { personId: string; url: string }[] = []; + // Liga squads are read again right now (players and coaching staff), not only on the 6-hourly refresh. + for (const team of [event.homeTeam, event.awayTeam]) { + if (team?.provider !== "LIGA") continue; + try { + const season = event.fixture?.competition.provider === "LIGA" ? event.fixture.competition.season : seasonOf(new Date()); + await this.savePlayers(team.id, season, await this.liga.squad(team.externalId, season), "api"); + } catch (err) { + log.warn({ err, team: team.id }, "sports: liga squad re-read failed"); + } + } for (const team of [event.homeTeam, event.awayTeam]) { if (!team) continue; const club = settings.teamNames[team.id] ?? team.name; @@ -764,8 +774,29 @@ export class SportsService { log.warn({ err, fixture: event.fixture.id }, "sports: fpa referees failed"); } } + // Liga: the round's nominations name the referee team (with the referee's official photo). + const fixture = event.fixture; + if (!named.length && fixture?.competition.provider === "LIGA" && fixture.round) { + try { + named = await this.liga.referees(fixture.competition.externalId, fixture.competition.season, fixture.round, fixture.externalId); + if (named.length) await db.sportsFixture.update({ where: { id: fixture.id }, data: { officials: named as unknown as object } }); + } catch (err) { + log.warn({ err, fixture: fixture.id }, "sports: liga referees failed"); + } + } const officials = named.filter((o) => PEOPLE_OFFICIALS.includes(o.role)); - const refTeam = event.fixture?.competition.provider === "FPA" ? await db.sportsTeam.findUnique({ where: { provider_externalId: { provider: "FPA", externalId: FPA_REFEREES_TEAM.externalId } } }) : null; + let refTeam = fixture?.competition.provider === "FPA" ? await db.sportsTeam.findUnique({ where: { provider_externalId: { provider: "FPA", externalId: FPA_REFEREES_TEAM.externalId } } }) : null; + if (fixture?.competition.provider === "LIGA" && officials.some((o) => o.externalId)) { + // Kept as the Liga's referees "team", so each one is the same person (id, full name, photo) from match to match. + const teamId = await this.upsertTeam("LIGA", FPA_REFEREES_TEAM, "futebol"); + await this.savePlayers( + teamId, + fixture.competition.season, + officials.filter((o) => o.externalId).map((o) => ({ externalId: o.externalId ?? null, name: o.name, fullName: o.fullName ?? null, number: null, position: o.role === "REFEREE" ? "Árbitro" : o.role === "FOURTH_OFFICIAL" ? "4.º árbitro" : "Árbitro assistente", photoUrl: o.photoUrl ?? null })), + "lineup", + ); + refTeam = await db.sportsTeam.findUnique({ where: { id: teamId } }); + } for (const o of officials) { // Handball: the referee as seen in earlier match reports (licence, full name, photo); by licence or by name. if (refTeam) { diff --git a/apps/api/test/sports.test.ts b/apps/api/test/sports.test.ts index 6b2ae12..a5b047f 100644 --- a/apps/api/test/sports.test.ts +++ b/apps/api/test/sports.test.ts @@ -279,6 +279,20 @@ const fakeSites = (state: { benfica: { shirtNumber: number; name: string; photos ], }, ); + if (u.pathname.endsWith("/competition/round/8/nominations")) + return json([ + { fixture: { fixtureCode: "10802" }, refereeName: "Outro Árbitro" }, + { + fixture: { fixtureCode: "10801" }, + refereeName: "João Pinheiro", + refereePhoto: "https://www.ligaportugal.pt/backoffice/assets/Joao_Pinheiro.png", + referee: { external_id: "67456", name: "João Pinheiro", fullName: "João Pedro Pinheiro", photos: "https://www.ligaportugal.pt/backoffice/assets/Joao_Pinheiro.png" }, + refereeAux1: { external_id: "67479", name: "Luciano Maia", fullName: "Luciano Maia", photos: "" }, + refereeAux2Name: "Hugo Marques", + var: { external_id: "59631", name: "Manuel Mota" }, + fieldDelegate: { external_id: "77318", name: "Rodrigo Viana" }, + }, + ]); if (u.pathname.includes("/calendar/matches/month/10")) return json([ { @@ -425,8 +439,13 @@ describe("sports data service", () => { ["Anatoliy Trubin", "SL Benfica", "1", true], ["Diogo Costa", "FC Porto", "99", true], ["Francesco Farioli", "FC Porto", null, true], // the head coach, from the club's staff (not the B team's) + ["Hugo Marques", "Arbitragem", null, false], // the round's nominations: the referee team on the pitch + ["João Pinheiro", "Arbitragem", null, true], + ["Luciano Maia", "Arbitragem", null, true], ["Vangelis Pavlidis", "SL Benfica", "14", true], ]); + // The referee comes with his official photo; the VAR and the delegates are not on the pitch. + expect(people.find((p) => p.name === "João Pinheiro")).toMatchObject({ referenceStatus: "PENDING" }); const pavlidis = people.find((p) => p.name === "Vangelis Pavlidis")!; expect(pavlidis).toMatchObject({ referenceStatus: "PENDING" }); expect(pavlidis.referenceKey).toBeTruthy();