diff --git a/apps/api/src/sports/service.ts b/apps/api/src/sports/service.ts index 8108b55..0ff41ad 100644 --- a/apps/api/src/sports/service.ts +++ b/apps/api/src/sports/service.ts @@ -691,6 +691,20 @@ export class SportsService { this.deps.log.info({ side: camp.side, players: camp.players.length, source: camp.sourceUrl }, "sports: youth call-up"); } for (const game of camp.games) { + if (camp.side === EHF_W17_EURO.side) { + const homeTeamId = await this.upsertTeam("FPA", { + externalId: game.homeExternalId, + name: game.homeCountry, + abbreviation: youthAbbrev(game.homeExternalId, camp.side), + }); + const awayTeamId = await this.upsertTeam("FPA", { + externalId: game.awayExternalId, + name: game.awayCountry, + abbreviation: youthAbbrev(game.awayExternalId, camp.side), + }); + const listed = await this.hasOfficialYouthFixture(homeTeamId, awayTeamId, game.startsAt, EHF_W17_EURO.externalId); + if (listed) continue; + } const comp = await this.youthCompetition(season, camp.side); result.fixtures += await this.saveFixture(comp, "FPA", { externalId: game.externalId, @@ -810,6 +824,69 @@ export class SportsService { } } + /** The FPA camp and the EHF page can disagree by an hour and still be the same match. */ + private sameKickoff(startsAt: Date) { + const slackMs = 2 * 60 * 60 * 1000; + return { + gte: new Date(startsAt.getTime() - slackMs), + lte: new Date(startsAt.getTime() + slackMs), + }; + } + + /** The official under-17 EURO already has this kick-off, so the camp must not add it again as a friendly. */ + private async hasOfficialYouthFixture(homeTeamId: string, awayTeamId: string, startsAt: Date, competitionExternalId: string): Promise { + const row = await this.deps.db.sportsFixture.findFirst({ + where: { + homeTeamId, + awayTeamId, + startsAt: this.sameKickoff(startsAt), + competition: { externalId: competitionExternalId }, + }, + select: { id: true }, + }); + return row !== null; + } + + /** + * The FPA post lists the same under-17 game as a friendly ("Sub-17 feminina"). One match, one row. + * An event already opened on the friendly keeps that row when it can become the official one. + */ + private async foldYouthFriendly( + officialCompetitionId: string, + officialExternalId: string, + homeTeamId: string, + awayTeamId: string, + startsAt: Date, + ): Promise { + const friendly = await this.deps.db.sportsFixture.findFirst({ + where: { + homeTeamId, + awayTeamId, + startsAt: this.sameKickoff(startsAt), + competition: { provider: "FPA", externalId: { endsWith: "-AMIG" } }, + }, + select: { id: true }, + }); + if (!friendly) return; + const official = await this.deps.db.sportsFixture.findUnique({ + where: { competitionId_externalId: { competitionId: officialCompetitionId, externalId: officialExternalId } }, + select: { id: true }, + }); + if (!official) { + await this.deps.db.sportsFixture.update({ + where: { id: friendly.id }, + data: { competitionId: officialCompetitionId, externalId: officialExternalId }, + }); + return; + } + if (official.id === friendly.id) return; + await this.deps.db.event.updateMany({ + where: { fixtureId: friendly.id }, + data: { fixtureId: official.id }, + }); + await this.deps.db.sportsFixture.delete({ where: { id: friendly.id } }); + } + /** * Women's under-17 EHF EURO qualification on eurohandball.com. The whole tournament is stored so the * picker can open Portugal–Italy (and the rest of the weekend) before the nominated sheet is published. @@ -844,6 +921,9 @@ export class SportsService { update: { name: EHF_W17_EURO.name, gender: EHF_W17_EURO.gender, season }, }); for (const row of listed) { + const homeTeamId = await this.upsertTeam("FPA", row.home); + const awayTeamId = await this.upsertTeam("FPA", row.away); + await this.foldYouthFriendly(comp.id, row.match.matchId, homeTeamId, awayTeamId, row.match.startsAt); result.fixtures += await this.saveFixture(comp, "FPA", { externalId: row.match.matchId, competition: EHF_W17_EURO.externalId, diff --git a/apps/api/test/sports.test.ts b/apps/api/test/sports.test.ts index 11f62ba..0785d3f 100644 --- a/apps/api/test/sports.test.ts +++ b/apps/api/test/sports.test.ts @@ -930,6 +930,64 @@ describe("sports data service", () => { number: "9", externalId: "ehf-w17-rossi", }); + + const { org } = await seedUser(ctx.db, "w17-dup@photoreport.pt", { org: "w17dup", role: "OWNER" }); + const friendlyComp = await ctx.db.sportsCompetition.create({ + data: { provider: "FPA", externalId: "W17-AMIG", sport: "andebol", name: "Sub-17 feminina", gender: "F", season: w17!.season }, + }); + const duplicate = await ctx.db.sportsFixture.create({ + data: { + competitionId: friendlyComp.id, + externalId: "w17-por-ita-20261127", + season: w17!.season, + homeTeamId: w17!.homeTeam.id, + awayTeamId: w17!.awayTeam.id, + startsAt: w17!.startsAt, + timeKnown: true, + round: "Particular", + }, + }); + const opened = await ctx.db.event.create({ + data: { + organizationId: org!.id, + home: "Portugal", + away: "Itália", + startsAt: w17!.startsAt, + ingestFrom: w17!.startsAt, + ingestUntil: w17!.startsAt, + iptcTemplate: {}, + fixtureId: duplicate.id, + }, + }); + await sports.syncNationalCallUps(kick); + const paired = await ctx.db.sportsFixture.findMany({ + where: { homeTeamId: w17!.homeTeam.id, awayTeamId: w17!.awayTeam.id, startsAt: w17!.startsAt }, + }); + expect(paired.map((row) => row.externalId)).toEqual(["202712010502014"]); + expect((await ctx.db.event.findUniqueOrThrow({ where: { id: opened.id } })).fixtureId).toBe(w17!.id); + + await ctx.db.sportsFixture.delete({ where: { id: w17!.id } }); + const campOnly = await ctx.db.sportsFixture.create({ + data: { + competitionId: friendlyComp.id, + externalId: "w17-por-ita-20261127", + season: w17!.season, + homeTeamId: w17!.homeTeam.id, + awayTeamId: w17!.awayTeam.id, + startsAt: w17!.startsAt, + timeKnown: true, + round: "Particular", + }, + }); + await ctx.db.event.update({ where: { id: opened.id }, data: { fixtureId: campOnly.id } }); + await sports.syncNationalCallUps(kick); + const adopted = await ctx.db.sportsFixture.findMany({ + where: { homeTeamId: w17!.homeTeam.id, awayTeamId: w17!.awayTeam.id, startsAt: w17!.startsAt }, + include: { competition: true }, + }); + expect(adopted).toHaveLength(1); + expect(adopted[0]).toMatchObject({ id: campOnly.id, externalId: "202712010502014", competition: { name: "W17 EHF EURO" } }); + expect((await ctx.db.event.findUniqueOrThrow({ where: { id: opened.id } })).fixtureId).toBe(campOnly.id); }); }); diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 68eed22..718b155 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -333,7 +333,9 @@ every 6 hours (`SPORTS_SYNC=off` turns it off). It is shared by every organisati matches; photo/number come from the sheet. The same pass reads the Women's U17 EHF EURO qualification (`W17-EURO`, content `139009`: Portugal–Italy `202712010502014`, 27 Nov 2026 19:00 in Águas Santas, and the rest of that tournament). Portugal is team `W17`; every other country is `-W17` (Italy is `ITA-W17`). The nominated sheet - fills numbers and headshots once the EHF publishes it. Youth camps (sub-17, sub-19, sub-20, men or women) come from + fills numbers and headshots once the EHF publishes it. The FPA camp lists that same match as a friendly + (`Sub-17 feminina`): it is folded into `W17-EURO`, and an event already opened on it stays on the one fixture. + Youth camps (sub-17, sub-19, sub-20, men or women) come from recent posts on `portal.fpa.pt`: the call-up PDF (CIPA, no shirt number from the list order) and the "Jogos Particulares" calendar. A sheet with a real squad replaces that age group; a post the site does not return leaves the plantel we already have. The November 2026 M19 list and the Spain name list are only the