From 8f6af48055ab3af8b5d30c8eb1f873368f5df0b0 Mon Sep 17 00:00:00 2001 From: spark Date: Wed, 21 Oct 2026 20:52:54 +0100 Subject: [PATCH] Keep national teams and sessions off the club squads. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A FPA seleção is its own list for the season: the women's team, the men's team and an age group do not share a roster or a shirt number. A session of that team has no match number. A gala still has no list. --- apps/api/src/faces/numbers.ts | 96 ++++++++++++++++++++++++++-- apps/api/src/faces/selection.test.ts | 25 ++++++++ apps/api/src/faces/selection.ts | 26 ++++++++ apps/api/src/faces/service.ts | 6 +- docs/ARCHITECTURE.md | 12 ++-- 5 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 apps/api/src/faces/selection.test.ts create mode 100644 apps/api/src/faces/selection.ts diff --git a/apps/api/src/faces/numbers.ts b/apps/api/src/faces/numbers.ts index ac29aa8..2ac4393 100644 --- a/apps/api/src/faces/numbers.ts +++ b/apps/api/src/faces/numbers.ts @@ -1,6 +1,7 @@ import { isExhibition, isNationalSide, seasonOf, teamsNamed } from "../archive/match.js"; import type { Db } from "../db.js"; import { normLabel } from "../jersey/service.js"; +import { selectionKey, selectionNeedles } from "./selection.js"; import { eventSport } from "./sport.js"; import { JERSEY_MIN_SCORE, majorityNumber } from "./votes.js"; @@ -18,6 +19,11 @@ export interface NumberContext { competition: string; /** A national team's game or session: club numbers never apply. */ national: boolean; + /** + * The national team when the folder names one ("snaf", "snam", "m18"). + * Null when the event only says "Portugal": the country is not an age group. + */ + side: string | null; /** The season now: only then does the number typed on the person (with no season of its own) apply. */ current: boolean; /** Linked to an official fixture: that competition's squad numbers come before what other shirts read. */ @@ -51,12 +57,19 @@ export async function numberContext(db: Db, eventId: string, now = new Date()): // An event that is not a game (a gala, a session, a presentation): nobody is known by a shirt there, only by face. if (!e || !e.away?.trim()) return null; const season = e.fixture?.season ?? seasonOf(day(e.startsAt)); + const national = isNationalEvent(e); + let side: string | null = null; + if (national) { + const folders = await db.cloudFolder.findMany({ where: { eventId }, select: { path: true }, take: 8 }); + side = selectionKey([e.home, e.away ?? "", e.competition ?? "", ...folders.map((f) => f.path)]); + } return { organizationId: e.organizationId, eventId, season, competition: competitionKey(e), - national: isNationalEvent(e), + national, + side, current: season === seasonOf(day(now)), official: !!e.fixture, teamIds: [e.homeTeamId, e.awayTeamId].filter((t): t is string => !!t), @@ -153,7 +166,10 @@ export async function numbersIn(db: Db, ctx: NumberContext, personIds: string[], where: { organizationId: ctx.organizationId, startsAt: { gte: from, lt: to } }, select: { id: true, home: true, away: true, competition: true, competitionCode: true }, }); - const same = events.filter((e) => competitionKey(e) === ctx.competition && isNationalEvent(e) === ctx.national).map((e) => e.id); + const onSide = ctx.national && ctx.side ? new Set(await eventsOfSide(db, ctx.organizationId, ctx.season, ctx.side)) : null; + const same = events + .filter((e) => (onSide ? onSide.has(e.id) : competitionKey(e) === ctx.competition && isNationalEvent(e) === ctx.national)) + .map((e) => e.id); if (same.length) { const faces = await db.face.findMany({ where: { photo: { eventId: { in: same } }, group: { personId: { in: rest } }, jerseyNumber: { not: null }, jerseyScore: { gte: JERSEY_MIN_SCORE } }, @@ -197,6 +213,51 @@ interface FixtureOfficial { externalId?: string | null; } +/** Events of one national team in a season (the women's senior team is not the men's, and not an age group). */ +async function eventsOfSide(db: Db, organizationId: string, season: string, side: string): Promise { + const { from, to } = seasonRange(season); + const needles = selectionNeedles(side); + const events = await db.event.findMany({ + where: { + organizationId, + startsAt: { gte: from, lt: to }, + OR: needles.flatMap((needle) => [ + { home: { contains: needle, mode: "insensitive" } }, + { away: { contains: needle, mode: "insensitive" } }, + { competition: { contains: needle, mode: "insensitive" } }, + { cloudFolders: { some: { path: { contains: needle, mode: "insensitive" } } } }, + ]), + }, + select: { id: true, home: true, away: true, competition: true, cloudFolders: { select: { path: true }, take: 4 } }, + }); + return events + .filter((e) => selectionKey([e.home, e.away ?? "", e.competition ?? "", ...e.cloudFolders.map((f) => f.path)]) === side) + .map((e) => e.id); +} + +/** People already confirmed, with a face, on that national team this season. */ +async function peopleConfirmedOnSide(db: Db, organizationId: string, season: string, side: string): Promise { + const eventIds = await eventsOfSide(db, organizationId, season, side); + if (!eventIds.length) return []; + const rows = await db.faceGroup.findMany({ + where: { eventId: { in: eventIds }, personId: { not: null }, ignoredAt: null }, + select: { personId: true, person: { select: { centroid: true } } }, + }); + return [...new Set(rows.filter((r) => r.personId && r.person && r.person.centroid.length > 0).map((r) => r.personId!))]; +} + +/** People a session's folders name ("7 - Bebiana Sabino"), when that person already has a face. */ +async function peopleNamedHere(db: Db, eventId: string, organizationId: string): Promise { + const photos = await db.photo.findMany({ where: { eventId }, select: { players: true } }); + const names = [...new Set(photos.flatMap((p) => p.players).map((n) => n.trim()).filter(Boolean))]; + if (!names.length) return []; + const people = await db.person.findMany({ + where: { organizationId, name: { in: names } }, + select: { id: true, centroid: true }, + }); + return people.filter((p) => p.centroid.length > 0).map((p) => p.id); +} + /** The people a suggestion may name. Null: this event has no roster, so the caller uses the whole list. An empty list is a known squad that has no face on file yet. */ export function peopleOnRoster(people: T[], roster: string[] | null): T[] | null { if (!roster) return null; @@ -212,6 +273,31 @@ export function peopleOnRoster(people: T[], roster: st */ export async function rosterOf(db: Db, eventId: string): Promise { await bindEventTeams(db, eventId); + const event = await db.event.findUnique({ + where: { id: eventId }, + select: { + organizationId: true, + home: true, + away: true, + competition: true, + startsAt: true, + cloudFolders: { select: { path: true }, take: 8 }, + }, + }); + if (!event) return null; + const side = selectionKey([event.home, event.away ?? "", event.competition ?? "", ...event.cloudFolders.map((f) => f.path)]); + const game = !!event.away?.trim(); + // A session, a training, a studio day, or a national team: never a club's squad. + // A gala or a press day names no team, so the caller compares the face on its own. + if (!game && !side) return null; + if (!game || side || isNationalEvent(event)) { + const ids = new Set(); + const season = seasonOf(day(event.startsAt)); + if (side) for (const id of await peopleConfirmedOnSide(db, event.organizationId, season, side)) ids.add(id); + if (!game) for (const id of await peopleNamedHere(db, eventId, event.organizationId)) ids.add(id); + if (game) for (const id of await officialsOf(db, eventId, event.organizationId)) ids.add(id); + return [...ids]; + } const ctx = await numberContext(db, eventId); if (!ctx) return null; const ids = new Set(); @@ -247,9 +333,9 @@ export async function rosterOf(db: Db, eventId: string): Promise 0) return []; // No official squad: people typed against these clubs, and only when that set is small enough to be the game. - const event = await db.event.findUnique({ where: { id: eventId }, select: { home: true, away: true, homeShort: true, awayShort: true } }); - if (!event) return null; - const teams = [event.home, event.away, event.homeShort, event.awayShort].filter((t): t is string => !!t).map(fold); + const named = await db.event.findUnique({ where: { id: eventId }, select: { home: true, away: true, homeShort: true, awayShort: true } }); + if (!named) return null; + const teams = [named.home, named.away, named.homeShort, named.awayShort].filter((t): t is string => !!t).map(fold); const people = await db.person.findMany({ where: { organizationId: ctx.organizationId, club: { not: null }, NOT: { centroid: { isEmpty: true } } }, select: { id: true, club: true }, diff --git a/apps/api/src/faces/selection.test.ts b/apps/api/src/faces/selection.test.ts new file mode 100644 index 0000000..6b6b93f --- /dev/null +++ b/apps/api/src/faces/selection.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from "vitest"; +import { selectionKey } from "./selection.js"; + +describe("selectionKey", () => { + it("treats the women's and men's national teams as different teams, whatever the folder calls them", () => { + const women = selectionKey(["/FPA/Fotos/2025_2026/Seleções Nacionais/SNAF/20260409 - POR x MNE"]); + const womenTitle = selectionKey(["Seleção Nacional A Feminina · Sessão"]); + const men = selectionKey(["/FPA/Fotos/2024_2025/Seleções Nacionais/Seleção Nacional A Masculina/20250122 - SWE x POR"]); + expect(women).toBe("snaf"); + expect(womenTitle).toBe(women); + expect(men).toBe("snam"); + expect(men).not.toBe(women); + }); + + it("keeps an age group apart from the senior teams", () => { + expect(selectionKey(["/FPA/Fotos/2025_2026/Seleções Nacionais/M18/Torneio Tiby/POR_FRA"])).toBe("m18"); + }); + + it("names no team for a club game, a gala, or a gathering", () => { + expect(selectionKey(["Sporting CP", "ABC de Braga", "Andebol 1"])).toBeNull(); + expect(selectionKey(["Portugal", "França"])).toBeNull(); + expect(selectionKey(["Gala 20250614"])).toBeNull(); + expect(selectionKey(["Encontro Nacional sub-14 · Dia 2"])).toBeNull(); + }); +}); diff --git a/apps/api/src/faces/selection.ts b/apps/api/src/faces/selection.ts new file mode 100644 index 0000000..016c6cd --- /dev/null +++ b/apps/api/src/faces/selection.ts @@ -0,0 +1,26 @@ +const fold = (s: string) => s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase(); + +/** + * Which national team a folder or a title is, when it names one. + * "SNAF" and "Seleção Nacional A Feminina" are the same team. A country name alone is not: + * Portugal against France can be the senior women, the senior men, or an age group. + * A gala, a press day, or "Encontro Nacional sub-14" names no team. + */ +export function selectionKey(texts: string[]): string | null { + const f = fold(texts.filter(Boolean).join("\n")); + if (!f) return null; + if (/\bsnaf\b/.test(f) || /selecao nacional a feminina/.test(f) || /selecao feminina/.test(f)) return "snaf"; + if (/\bsnam\b/.test(f) || /selecao nacional a masculina/.test(f) || /selecao masculina/.test(f)) return "snam"; + if (/\bsna\b/.test(f)) return "snam"; + const coded = /\b([mw])(\d{2})\b/.exec(f); + if (coded && /selec/.test(f)) return `${coded[1]}${coded[2]}`; + return null; +} + +/** Strings a stored path or title may contain for this team. */ +export function selectionNeedles(key: string): string[] { + if (key === "snaf") return ["SNAF", "Seleção Nacional A Feminina", "Seleção Feminina"]; + if (key === "snam") return ["SNAM", "SNA", "Seleção Nacional A Masculina", "Seleção Masculina"]; + if (/^[mw]\d{2}$/.test(key)) return [key.toUpperCase()]; + return [key]; +} diff --git a/apps/api/src/faces/service.ts b/apps/api/src/faces/service.ts index 0b0f0f5..95fe674 100644 --- a/apps/api/src/faces/service.ts +++ b/apps/api/src/faces/service.ts @@ -552,8 +552,8 @@ export class FaceService { const sport = await eventSport(db, eventId); const sports = sport ? await peopleSports(db, everyone) : new Map(); const people = everyone.filter((p) => !sport || !sports.has(p.id) || sports.get(p.id) === sport); - // A game whose clubs are known is named only from that squad. A session (no roster) is compared with - // everyone of the sport, and only a clearly stronger face: a weak likeness is not a name. + // A club game is named only from that season's squad. A national team or a session of one is named only + // from that team. A gala has no list: the face alone, and only a clearly stronger match. const sheet = await rosterOf(db, eventId); const bounded = sheet !== null; const pool = peopleOnRoster(people, sheet) ?? people; @@ -636,6 +636,8 @@ export class FaceService { */ private async suggestByNumber(groupId: string, ctx: NumberContext, read: NumberVote): Promise<{ personId: string; score: number } | null> { const { db } = this.deps; + // A national team wears its own numbers. The club who has that number this season is someone else. + if (ctx.national) return null; const eventId = ctx.eventId; const event = await db.event.findUnique({ where: { id: eventId }, diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index d0ea82f..ac075bb 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -240,16 +240,20 @@ number for that event's context: already have a face. A player who has since moved is included when that season's list has them (the same federation id, or the same birth date and full name), with the shirt number of that season. A face none of them explains stays unnamed. It is never someone who was not at either club that season. - A session (no away side), an exhibition, or a name that matches no team has no sheet: the face alone, against - everyone of that sport, and only a clearly stronger match. The shirt, when it is read, stays with those two clubs. + A session, a training day or a studio day of a national team (FPA: SNAF, SNAM, M18, and the same team under + its long name) uses that team only: people already confirmed there this season, and the athletes the session + folders name. There is no match number. A gala or a press day names no team: the face alone, against everyone + of that sport, and only a clearly stronger match. The shirt, when it is read, stays with those two clubs. Then what the shirts clearly read (three or more clear readings) in the person's confirmed photos of the same season and competition. - An editor of the folder can give a face another name. The list shows the official face, the club and the shirt number. - **Not linked (a cup, a friendly, a tournament):** those readings in that competition first, then the season's squad. -- **National team** (a country on either side, or a "Seleção" folder): club squads and club numbers never apply, - only what was read in national games of that season. +- **National team** (a country on either side, or a seleção folder): club squads and club numbers never apply. + The women's senior team, the men's, and an age group are different lists. A photo is named from people already + confirmed on that same team this season. The shirt number is the one read on that team's games, and an older + Portugal game with no team in the folder still shares what was read for Portugal that season. - **The number typed on the person** has no season: used only in the current season and never for a national team. Training samples never take the model's own readings as labels. -- 2.47.3