diff --git a/bridge/src/routes/homepage.ts b/bridge/src/routes/homepage.ts index 333dc88..f660d74 100644 --- a/bridge/src/routes/homepage.ts +++ b/bridge/src/routes/homepage.ts @@ -3,7 +3,8 @@ import { get_domain_config } from '../config/domain.js'; import { resolve_site_host } from '../lib/request-host.js'; import { WpClient } from '../services/wp-client.js'; import { swr_fetch } from '../services/cache.js'; -import type { ArticleListItem, HomepageData } from '../types.js'; +import type { ArticleListItem, FpaAgendaGame, HomepageData } from '../types.js'; +import { fetch_fpa_agenda } from '../services/fpa-agenda.js'; // Number of articles per section const HERO_COUNT = 3; @@ -12,7 +13,7 @@ const FEATURED_COUNT = 4; const LATEST_COUNT = 12; const BY_CATEGORY_COUNT = 4; -const empty_homepage = (): HomepageData => ({ +const empty_homepage = (): Omit => ({ hero: [], trending: [], featured: [], @@ -26,7 +27,7 @@ export async function homepage_routes(app: FastifyInstance): Promise { const config = get_domain_config(host); const client = new WpClient(config.wp_endpoint); - const data = await swr_fetch( + const base = await swr_fetch>( `${host}:homepage`, async () => { try { @@ -82,6 +83,18 @@ export async function homepage_routes(app: FastifyInstance): Promise { }, ); + const fpa_agenda: FpaAgendaGame[] | null = config.fpa_agenda_enabled + ? await swr_fetch(`${host}:fpa:agenda`, async () => { + try { + return await fetch_fpa_agenda(); + } catch (err) { + app.log.warn({ err, host }, 'FPA agenda fetch failed'); + return []; + } + }) + : null; + + const data: HomepageData = { ...base, fpa_agenda }; return reply.send(data); }); diff --git a/bridge/src/services/fpa-agenda.ts b/bridge/src/services/fpa-agenda.ts new file mode 100644 index 0000000..2ed601e --- /dev/null +++ b/bridge/src/services/fpa-agenda.ts @@ -0,0 +1,78 @@ +import type { FpaAgendaGame } from '../types.js'; + +/** Public FPA JSON feed used by portal.fpa.pt for national competition fixtures. */ +const FPA_AGENDA_URL = + process.env['FPA_AGENDA_URL'] ?? 'https://ws.fpa.pt/index.php/getAgendaJogosFAP/'; + +const FETCH_TIMEOUT_MS = parseInt(process.env['FPA_FETCH_TIMEOUT_MS'] ?? '20000', 10); +const MAX_GAMES = Math.min(parseInt(process.env['FPA_AGENDA_MAX'] ?? '48', 10), 200); + +function as_trimmed_string(value: unknown): string { + if (value === null || value === undefined) { + return ''; + } + return String(value).trim(); +} + +function parse_goals(value: unknown): number | null { + if (value === null || value === undefined || value === '') { + return null; + } + const n = Number(value); + return Number.isFinite(n) ? n : null; +} + +function sort_key(date: string, time: string): string { + return `${date} ${time}`.padEnd(20, '0'); +} + +/** + * Fetch and normalise FPA agenda JSON. Caller should wrap in cache (e.g. swr_fetch). + * Throws on network/HTTP errors or invalid payload shape. + */ +export async function fetch_fpa_agenda(): Promise { + const res = await fetch(FPA_AGENDA_URL, { + headers: { Accept: 'application/json' }, + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), + }); + + if (!res.ok) { + throw new Error(`FPA agenda HTTP ${res.status}`); + } + + const raw: unknown = await res.json(); + if (!Array.isArray(raw)) { + throw new Error('FPA agenda: expected JSON array'); + } + + const games: FpaAgendaGame[] = []; + + for (const row of raw) { + if (row === null || typeof row !== 'object') { + continue; + } + const o = row as Record; + const match_id = as_trimmed_string(o['ID_PROVA_JOGO']); + const date = as_trimmed_string(o['JOG_DATA']); + const time = as_trimmed_string(o['JOG_HORA']); + if (!match_id || !date) { + continue; + } + + games.push({ + match_id, + date, + time: time || '—', + home_team: as_trimmed_string(o['EQUIPA_CASA']) || '—', + away_team: as_trimmed_string(o['EQUIPA_FORA']) || '—', + home_goals: parse_goals(o['GOLOS_CASA']), + away_goals: parse_goals(o['GOLOS_FORA']), + venue: as_trimmed_string(o['LOCAL']) || '—', + competition: as_trimmed_string(o['PROVA']) || '—', + phase: as_trimmed_string(o['FASE']) || '', + }); + } + + games.sort((a, b) => sort_key(a.date, a.time).localeCompare(sort_key(b.date, b.time))); + return games.slice(0, MAX_GAMES); +} diff --git a/bridge/src/types.ts b/bridge/src/types.ts index 4a28607..7a923d0 100644 --- a/bridge/src/types.ts +++ b/bridge/src/types.ts @@ -15,6 +15,22 @@ export interface DomainConfig { ads_enabled: boolean; analytics_id: string | null; auth_links?: boolean; + /** When true, homepage includes `fpa_agenda` from ws.fpa.pt (cached server-side). */ + fpa_agenda_enabled?: boolean; +} + +/** Normalised row from FPA `getAgendaJogosFAP` JSON. */ +export interface FpaAgendaGame { + match_id: string; + date: string; + time: string; + home_team: string; + away_team: string; + home_goals: number | null; + away_goals: number | null; + venue: string; + competition: string; + phase: string; } export interface CategoryConfig { @@ -78,6 +94,8 @@ export interface HomepageData { featured: ArticleListItem[]; latest: ArticleListItem[]; by_category: Record; + /** Present when `fpa_agenda_enabled`; otherwise null. */ + fpa_agenda: FpaAgendaGame[] | null; } // Raw WordPress REST API types (minimal subset we use) diff --git a/config/domains/andebol.net.json b/config/domains/andebol.net.json index 48c6551..f7dbbdf 100644 --- a/config/domains/andebol.net.json +++ b/config/domains/andebol.net.json @@ -25,5 +25,6 @@ "timezone": "Europe/Lisbon", "ads_enabled": false, "analytics_id": null, - "auth_links": true + "auth_links": true, + "fpa_agenda_enabled": true } diff --git a/frontend/src/lib/bridge.ts b/frontend/src/lib/bridge.ts index 63c20ba..21ddac3 100644 --- a/frontend/src/lib/bridge.ts +++ b/frontend/src/lib/bridge.ts @@ -131,6 +131,7 @@ export class MockBridgeClient extends BridgeClient { basketball: this.mockArticles(4), formula1: this.mockArticles(4), }, + fpa_agenda: null, }; } diff --git a/frontend/src/lib/ui-i18n.ts b/frontend/src/lib/ui-i18n.ts index 36c8c0b..58695eb 100644 --- a/frontend/src/lib/ui-i18n.ts +++ b/frontend/src/lib/ui-i18n.ts @@ -42,6 +42,15 @@ export type UiStrings = { search_results_count: (n: number) => string; explore_sections: string; discover_lead: string; + fpa_agenda_title: string; + fpa_agenda_lead: string; + fpa_col_when: string; + fpa_col_match: string; + fpa_col_score: string; + fpa_col_venue: string; + fpa_col_competition: string; + fpa_agenda_empty: string; + fpa_source_credit: string; }; const en: UiStrings = { @@ -83,6 +92,15 @@ const en: UiStrings = { search_results_count: n => `${n} results`, explore_sections: 'Explore', discover_lead: 'Browse by topic or find an article by keyword.', + fpa_agenda_title: 'Upcoming matches', + fpa_agenda_lead: 'National competition fixtures from the Portuguese Handball Federation feed.', + fpa_col_when: 'When', + fpa_col_match: 'Match', + fpa_col_score: 'Score', + fpa_col_venue: 'Venue', + fpa_col_competition: 'Competition', + fpa_agenda_empty: 'Fixture list is temporarily unavailable.', + fpa_source_credit: 'Data: Portuguese Handball Federation (ws.fpa.pt).', }; const pt: UiStrings = { @@ -124,6 +142,15 @@ const pt: UiStrings = { search_results_count: n => (n === 1 ? '1 resultado' : `${n} resultados`), explore_sections: 'Explorar', discover_lead: 'Navegue por tema ou encontre um artigo por palavra-chave.', + fpa_agenda_title: 'Próximos jogos', + fpa_agenda_lead: 'Agenda de competições nacionais (feed público da Federação Portuguesa de Andebol).', + fpa_col_when: 'Quando', + fpa_col_match: 'Jogo', + fpa_col_score: 'Resultado', + fpa_col_venue: 'Local', + fpa_col_competition: 'Competição', + fpa_agenda_empty: 'A agenda não está disponível de momento.', + fpa_source_credit: 'Dados: Federação Portuguesa de Andebol (ws.fpa.pt).', }; export function getUiStrings(locale: string): UiStrings { diff --git a/frontend/src/pages/index.astro b/frontend/src/pages/index.astro index d60d19e..a699f64 100644 --- a/frontend/src/pages/index.astro +++ b/frontend/src/pages/index.astro @@ -2,6 +2,7 @@ import Base from '../layouts/Base.astro'; import LatestGrid from '../components/home/LatestGrid.astro'; import DiscoverSection from '../themes/institutional/DiscoverSection.astro'; +import FpaAgendaSection from '../themes/institutional/FpaAgendaSection.astro'; import { getDomainConfig } from '../lib/domain.js'; import { getThemeComponents } from '../lib/theme.js'; import { BridgeClient, MockBridgeClient, bridgeRequestHeaders } from '../lib/bridge.js'; @@ -54,6 +55,10 @@ const website_schema = { + {config.fpa_agenda_enabled && homepage.fpa_agenda != null && ( + + )} + {config.ads_enabled && (
Advertisement
diff --git a/frontend/src/themes/institutional/FpaAgendaSection.astro b/frontend/src/themes/institutional/FpaAgendaSection.astro new file mode 100644 index 0000000..860f21d --- /dev/null +++ b/frontend/src/themes/institutional/FpaAgendaSection.astro @@ -0,0 +1,208 @@ +--- +import type { FpaAgendaGame } from '../../types/index.js'; +import { getUiStrings } from '../../lib/ui-i18n.js'; + +interface Props { + games: FpaAgendaGame[]; + locale: string; +} + +const { games, locale } = Astro.props; +const ui = getUiStrings(locale); + +function format_score(game: FpaAgendaGame): string { + if (game.home_goals !== null && game.away_goals !== null) { + return `${game.home_goals} – ${game.away_goals}`; + } + return '—'; +} +--- +
+
+
+

{ui.fpa_agenda_title}

+

{ui.fpa_agenda_lead}

+
+ + {games.length === 0 ? ( +

{ui.fpa_agenda_empty}

+ ) : ( +
+ + + + + + + + + + + + {games.map((g) => ( + + + + + + + + ))} + +
{ui.fpa_col_when}{ui.fpa_col_match}{ui.fpa_col_score}{ui.fpa_col_venue}{ui.fpa_col_competition}
+ {g.date} + {g.time} + + {g.home_team} + + {g.away_team} + {g.phase && {g.phase}} + {format_score(g)}{g.venue}{g.competition}
+
+ )} + +

{ui.fpa_source_credit}

+
+
+ + diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts index 528f7d4..29f6dc5 100644 --- a/frontend/src/types/index.ts +++ b/frontend/src/types/index.ts @@ -30,6 +30,22 @@ export interface DomainConfig { analytics_id: string | null; /** When true, show login/register in chrome and expose /entrar + /registar → WordPress. */ auth_links?: boolean; + /** Homepage includes FPA fixture feed (bridge proxies ws.fpa.pt). */ + fpa_agenda_enabled?: boolean; +} + +/** Normalised FPA agenda row (bridge → homepage JSON). */ +export interface FpaAgendaGame { + match_id: string; + date: string; + time: string; + home_team: string; + away_team: string; + home_goals: number | null; + away_goals: number | null; + venue: string; + competition: string; + phase: string; } export interface Article { @@ -100,4 +116,6 @@ export interface HomepageData { featured: ArticleListItem[]; latest: ArticleListItem[]; by_category: Record; + /** Omitted by older bridge versions; treat as null if missing. */ + fpa_agenda?: FpaAgendaGame[] | null; }