From de19fb7e1af623d0a929944d62c0593e9b252fda Mon Sep 17 00:00:00 2001 From: Tiago Neves Date: Sat, 10 Oct 2026 17:47:03 +0100 Subject: [PATCH] =?UTF-8?q?feat(andebol):=20add=20Classifica=C3=A7=C3=A3o?= =?UTF-8?q?=20link=20on=20FPA=20agenda=20game=20cards=20(BIT-31056)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Map PROVA competition codes to public standings URLs so every "Próximos jogos" card links to the competition table (Flashscore for Betclic leagues; FPA portal hubs otherwise). Paperclip-Issue: BIT-31056 Paperclip-Run: 5901136e-753a-494f-bd36-250d1b822690 --- bridge/package.json | 4 +- bridge/src/routes/homepage.ts | 4 +- bridge/src/services/fpa-standings-url.test.ts | 48 ++++++++++++ bridge/src/services/fpa-standings-url.ts | 74 +++++++++++++++++++ bridge/src/types.ts | 5 ++ frontend/src/lib/ui-i18n.ts | 3 + .../institutional/FpaAgendaSection.astro | 22 +++++- frontend/src/types/index.ts | 5 ++ 8 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 bridge/src/services/fpa-standings-url.test.ts create mode 100644 bridge/src/services/fpa-standings-url.ts diff --git a/bridge/package.json b/bridge/package.json index 3328ee7..3f0cc02 100644 --- a/bridge/package.json +++ b/bridge/package.json @@ -9,8 +9,8 @@ "build": "tsc", "start": "node dist/server.js", "typecheck": "tsc --noEmit", - "test": "npm run build && node --test dist/lib/sanitize-html.test.js dist/services/ranking.test.js dist/services/aggregator-mapper.test.js dist/services/andeboltv.test.js dist/services/fpa-stream-match.test.js", - "test:coverage": "npm run build && node --experimental-test-coverage --test dist/lib/sanitize-html.test.js dist/services/ranking.test.js dist/services/aggregator-mapper.test.js dist/services/andeboltv.test.js dist/services/fpa-stream-match.test.js" + "test": "npm run build && node --test dist/lib/sanitize-html.test.js dist/services/ranking.test.js dist/services/aggregator-mapper.test.js dist/services/andeboltv.test.js dist/services/fpa-stream-match.test.js dist/services/fpa-standings-url.test.js", + "test:coverage": "npm run build && node --experimental-test-coverage --test dist/lib/sanitize-html.test.js dist/services/ranking.test.js dist/services/aggregator-mapper.test.js dist/services/andeboltv.test.js dist/services/fpa-stream-match.test.js dist/services/fpa-standings-url.test.js" }, "dependencies": { "@fastify/cors": "^9.0.1", diff --git a/bridge/src/routes/homepage.ts b/bridge/src/routes/homepage.ts index 51a5c03..ae06684 100644 --- a/bridge/src/routes/homepage.ts +++ b/bridge/src/routes/homepage.ts @@ -8,6 +8,7 @@ import type { AndebolTvStreamEvent } from '../services/andeboltv.js'; import { fetch_andeboltv_stream_events } from '../services/andeboltv.js'; import { get_fpa_agenda_from_store } from '../services/fpa-agenda.js'; import { enrich_fpa_agenda_with_streams } from '../services/fpa-stream-match.js'; +import { enrich_fpa_agenda_with_standings_urls } from '../services/fpa-standings-url.js'; // Number of articles per section const HERO_COUNT = 3; @@ -151,7 +152,8 @@ export async function homepage_routes(app: FastifyInstance): Promise { } }), ]); - return enrich_fpa_agenda_with_streams(games, streams); + const with_streams = enrich_fpa_agenda_with_streams(games, streams); + return enrich_fpa_agenda_with_standings_urls(with_streams); } catch (err) { app.log.warn({ err, host }, 'FPA agenda fetch failed'); return []; diff --git a/bridge/src/services/fpa-standings-url.test.ts b/bridge/src/services/fpa-standings-url.test.ts new file mode 100644 index 0000000..c9fc8b1 --- /dev/null +++ b/bridge/src/services/fpa-standings-url.test.ts @@ -0,0 +1,48 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { + enrich_fpa_agenda_with_standings_urls, + extract_fpa_competition_code, + FPA_STANDINGS_FALLBACK_URL, + resolve_fpa_standings_url, +} from './fpa-standings-url.js'; + +test('extract_fpa_competition_code reads leading PO codes', () => { + assert.equal(extract_fpa_competition_code('PO01 - Campeonato Betclic Masculino'), 'PO01'); + assert.equal(extract_fpa_competition_code('PO01A - Divisao de Honra Masculina'), 'PO01A'); + assert.equal(extract_fpa_competition_code('PO09 - Campeonato Betclic Feminino'), 'PO09'); + assert.equal(extract_fpa_competition_code('PARTICULARES SELECOES NACIONAIS'), null); +}); + +test('resolve_fpa_standings_url maps Betclic leagues to Flashscore tables', () => { + assert.match( + resolve_fpa_standings_url('PO01 - Campeonato Betclic Masculino'), + /campeonato-betclic-masculino\/classificacao\/$/, + ); + assert.match( + resolve_fpa_standings_url('PO09 - Campeonato Betclic Feminino'), + /campeonato-betclic-feminino\/classificacao\/$/, + ); +}); + +test('resolve_fpa_standings_url falls back by gender then national hub', () => { + assert.match( + resolve_fpa_standings_url('PO02 - Campeonato Nacional 2a Divisao Masculina'), + /competicoes-nacionais-andebol-masculino\/$/, + ); + assert.match( + resolve_fpa_standings_url('PO10 - Campeonato Nacional 2a Divisao Feminina'), + /competicoes-nacionais-andebol-feminino\/$/, + ); + assert.equal(resolve_fpa_standings_url('PARTICULARES SELECOES NACIONAIS'), FPA_STANDINGS_FALLBACK_URL); +}); + +test('enrich_fpa_agenda_with_standings_urls always sets standings_url', () => { + const games: Array<{ competition: string; standings_url?: string | null }> = [ + { competition: 'PO01 - Campeonato Betclic Masculino' }, + { competition: 'Unknown cup', standings_url: null }, + ]; + const enriched = enrich_fpa_agenda_with_standings_urls(games); + assert.ok(enriched[0]?.standings_url.startsWith('https://')); + assert.equal(enriched[1]?.standings_url, FPA_STANDINGS_FALLBACK_URL); +}); diff --git a/bridge/src/services/fpa-standings-url.ts b/bridge/src/services/fpa-standings-url.ts new file mode 100644 index 0000000..cc2bda1 --- /dev/null +++ b/bridge/src/services/fpa-standings-url.ts @@ -0,0 +1,74 @@ +/** + * Resolve a public standings (classificação) URL for an FPA agenda competition name. + * + * Agenda rows only expose `PROVA` as a free-text label (e.g. "PO01 - Campeonato Betclic + * Masculino"). There is no competition id in the feed. We map known competition codes to + * public standings pages (Flashscore for top leagues that publish tables; FPA portal hubs + * otherwise), with a national competitions fallback. + */ + +const FLASHSCORE_PT = 'https://www.flashscore.pt/andebol/portugal'; +const PORTAL_FPA = 'https://portal.fpa.pt'; + +/** General FPA national competitions hub (final fallback). */ +export const FPA_STANDINGS_FALLBACK_URL = `${PORTAL_FPA}/competicoes-nacionais/`; + +const PORTAL_MASCULINO = `${PORTAL_FPA}/competicoes-nacionais-andebol-masculino/`; +const PORTAL_FEMININO = `${PORTAL_FPA}/competicoes-nacionais-andebol-feminino/`; + +/** + * Competition code (from PROVA prefix) → public standings page. + * Prefer pages that actually render a league table when available. + */ +const STANDINGS_BY_CODE: Record = { + PO01: `${FLASHSCORE_PT}/campeonato-betclic-masculino/classificacao/`, + PO01A: PORTAL_MASCULINO, + PO02: PORTAL_MASCULINO, + PO03: PORTAL_MASCULINO, + PO06: PORTAL_MASCULINO, + PO09: `${FLASHSCORE_PT}/campeonato-betclic-feminino/classificacao/`, + PO09A: PORTAL_FEMININO, + PO10: PORTAL_FEMININO, + PO12: PORTAL_FEMININO, + PO20: `${FLASHSCORE_PT}/taca-de-portugal/`, + PO23: PORTAL_FEMININO, +}; + +/** Extract leading competition code such as PO01 / PO01A / PO09A from a PROVA label. */ +export function extract_fpa_competition_code(competition: string): string | null { + const m = competition.trim().match(/^([A-Z]{1,3}\d{1,3}[A-Z]?)\b/i); + return m?.[1] ? m[1].toUpperCase() : null; +} + +function gender_hub_url(competition: string): string | null { + const lower = competition.toLowerCase(); + if (/\bfemin/i.test(lower)) { + return PORTAL_FEMININO; + } + if (/\bmascul/i.test(lower)) { + return PORTAL_MASCULINO; + } + return null; +} + +/** + * Always returns a URL suitable for an external "Classificação" link. + */ +export function resolve_fpa_standings_url(competition: string): string { + const code = extract_fpa_competition_code(competition); + if (code && STANDINGS_BY_CODE[code]) { + return STANDINGS_BY_CODE[code]!; + } + + return gender_hub_url(competition) ?? FPA_STANDINGS_FALLBACK_URL; +} + +/** Attach `standings_url` to each agenda game (idempotent). */ +export function enrich_fpa_agenda_with_standings_urls( + games: T[], +): Array { + return games.map((game) => ({ + ...game, + standings_url: game.standings_url || resolve_fpa_standings_url(game.competition), + })); +} diff --git a/bridge/src/types.ts b/bridge/src/types.ts index 2b14101..e0a89f2 100644 --- a/bridge/src/types.ts +++ b/bridge/src/types.ts @@ -40,6 +40,11 @@ export interface FpaAgendaGame { stream_status?: 'live' | 'upcoming' | null; /** Convenience flag when `stream_status === 'live'`. */ is_live?: boolean; + /** + * Public standings (classificação) page for this competition. + * Resolved from the PROVA label (code map + gender hub + FPA national fallback). + */ + standings_url?: string | null; } export interface CategoryConfig { diff --git a/frontend/src/lib/ui-i18n.ts b/frontend/src/lib/ui-i18n.ts index cca65bc..75f8cd3 100644 --- a/frontend/src/lib/ui-i18n.ts +++ b/frontend/src/lib/ui-i18n.ts @@ -60,6 +60,7 @@ export type UiStrings = { fpa_status_live: string; fpa_watch_live: string; fpa_watch_stream: string; + fpa_standings_link: string; fpa_games_on_date: (count: number) => string; /** News archive page title */ news_archive_title: string; @@ -172,6 +173,7 @@ const en: UiStrings = { fpa_status_live: 'Live', fpa_watch_live: 'Watch live', fpa_watch_stream: 'Watch on AndebolTV', + fpa_standings_link: 'Standings', fpa_games_on_date: count => (count === 1 ? '1 match' : `${count} matches`), news_archive_title: 'News', news_all_filter: 'All', @@ -268,6 +270,7 @@ const pt: UiStrings = { fpa_status_live: 'Em direto', fpa_watch_live: 'Ver em direto', fpa_watch_stream: 'Ver na AndebolTV', + fpa_standings_link: 'Classificação', fpa_games_on_date: count => (count === 1 ? '1 jogo' : `${count} jogos`), news_archive_title: 'Notícias', news_all_filter: 'Todas', diff --git a/frontend/src/themes/institutional/FpaAgendaSection.astro b/frontend/src/themes/institutional/FpaAgendaSection.astro index 357a043..05c3b9e 100644 --- a/frontend/src/themes/institutional/FpaAgendaSection.astro +++ b/frontend/src/themes/institutional/FpaAgendaSection.astro @@ -70,6 +70,7 @@ const date_groups = [...by_date.entries()]; const finished = is_finished(g); const score = format_score(g); const has_stream = Boolean(g.stream_url); + const standings_url = g.standings_url || 'https://portal.fpa.pt/competicoes-nacionais/'; const is_live = Boolean(g.is_live); const status_label = finished ? ui.fpa_status_finished @@ -173,6 +174,14 @@ const date_groups = [...by_date.entries()]; {g.venue} )} + + {ui.fpa_standings_link} + {has_stream && (