From f14ac7572b363ccfafbfbfbca7970b448af6d5cf Mon Sep 17 00:00:00 2001 From: spark Date: Thu, 5 Nov 2026 20:02:50 +0000 Subject: [PATCH] fix(sports): find a matchup typed as Portugal x E MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The picker searched the whole box as one name, so tonight's game never appeared. The seleção is written before the long club refresh, so a restart shows it at once. --- apps/api/src/sports/fixture-search.test.ts | 35 +++++++++++++++++++ apps/api/src/sports/fixture-search.ts | 39 ++++++++++++++++++++++ apps/api/src/sports/routes.ts | 4 +-- apps/api/src/sports/service.ts | 10 +++--- 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 apps/api/src/sports/fixture-search.test.ts create mode 100644 apps/api/src/sports/fixture-search.ts diff --git a/apps/api/src/sports/fixture-search.test.ts b/apps/api/src/sports/fixture-search.test.ts new file mode 100644 index 0000000..84063f5 --- /dev/null +++ b/apps/api/src/sports/fixture-search.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest"; +import { fixtureTextFilter } from "./fixture-search.js"; + +describe("fixture search", () => { + it("reads a matchup, and a short side matches the start of the name", () => { + expect(fixtureTextFilter("Portugal x E")).toEqual({ + OR: [ + { + AND: [ + { homeTeam: { OR: [{ name: { contains: "Portugal", mode: "insensitive" } }, { abbreviation: { contains: "Portugal", mode: "insensitive" } }] } }, + { awayTeam: { OR: [{ name: { startsWith: "E", mode: "insensitive" } }, { abbreviation: { startsWith: "E", mode: "insensitive" } }] } }, + ], + }, + { + AND: [ + { homeTeam: { OR: [{ name: { startsWith: "E", mode: "insensitive" } }, { abbreviation: { startsWith: "E", mode: "insensitive" } }] } }, + { awayTeam: { OR: [{ name: { contains: "Portugal", mode: "insensitive" } }, { abbreviation: { contains: "Portugal", mode: "insensitive" } }] } }, + ], + }, + ], + }); + }); + + it("keeps a single word as a team, competition or venue", () => { + const text = { contains: "Caminha", mode: "insensitive" }; + expect(fixtureTextFilter("Caminha")).toEqual({ + OR: [ + { homeTeam: { OR: [{ name: text }, { abbreviation: text }] } }, + { awayTeam: { OR: [{ name: text }, { abbreviation: text }] } }, + { competition: { name: text } }, + { venue: text }, + ], + }); + }); +}); diff --git a/apps/api/src/sports/fixture-search.ts b/apps/api/src/sports/fixture-search.ts new file mode 100644 index 0000000..a09b885 --- /dev/null +++ b/apps/api/src/sports/fixture-search.ts @@ -0,0 +1,39 @@ +/** + * The picker sends the whole box ("Portugal x E"). A team is never named that, so a matchup is two sides. + * One or two letters ("E", "ES") match the start of the name; a longer word matches anywhere in it. + */ + +type Text = { contains: string; mode: "insensitive" } | { startsWith: string; mode: "insensitive" }; + +function textOf(token: string): Text { + if (token.length <= 2) return { startsWith: token, mode: "insensitive" }; + return { contains: token, mode: "insensitive" }; +} + +function team(token: string) { + const text = textOf(token); + return { OR: [{ name: text }, { abbreviation: text }] }; +} + +export function fixtureTextFilter(q: string) { + const sides = q.split(/\s+x\s+/i).map((s) => s.trim()).filter(Boolean); + if (sides.length >= 2) { + const home = team(sides[0]!); + const away = team(sides.slice(1).join(" ")); + return { + OR: [ + { AND: [{ homeTeam: home }, { awayTeam: away }] }, + { AND: [{ homeTeam: away }, { awayTeam: home }] }, + ], + }; + } + const text = { contains: q, mode: "insensitive" as const }; + return { + OR: [ + { homeTeam: { OR: [{ name: text }, { abbreviation: text }] } }, + { awayTeam: { OR: [{ name: text }, { abbreviation: text }] } }, + { competition: { name: text } }, + { venue: text }, + ], + }; +} diff --git a/apps/api/src/sports/routes.ts b/apps/api/src/sports/routes.ts index 88c6e94..d780546 100644 --- a/apps/api/src/sports/routes.ts +++ b/apps/api/src/sports/routes.ts @@ -6,6 +6,7 @@ import type { SessionService } from "../auth/sessions.js"; import type { Db } from "../db.js"; import { forbidden, notFound } from "../lib/errors.js"; import { toCsv, toTsv } from "./codegen.js"; +import { fixtureTextFilter } from "./fixture-search.js"; import type { SportsService } from "./service.js"; const OrgParams = z.object({ orgId: z.uuid() }); @@ -107,13 +108,12 @@ export async function registerSportsRoutes(app: FastifyInstance, deps: { db: Db; await anyMembership(currentUserId(req), null); const from = q.from ?? new Date(Date.now() - 2 * 86400_000); const to = q.to ?? new Date(from.getTime() + 14 * 86400_000); - const text = q.q ? { contains: q.q, mode: "insensitive" as const } : undefined; const fixtures = await db.sportsFixture.findMany({ where: { startsAt: { gte: from, lte: to }, ...(q.competitionId ? { competitionId: q.competitionId } : {}), ...(q.sport ? { competition: { sport: q.sport } } : {}), - ...(text ? { OR: [{ homeTeam: { name: text } }, { awayTeam: { name: text } }, { competition: { name: text } }, { venue: text }] } : {}), + ...(q.q ? fixtureTextFilter(q.q) : {}), }, orderBy: { startsAt: "asc" }, take: 200, diff --git a/apps/api/src/sports/service.ts b/apps/api/src/sports/service.ts index 7e05e38..d349e3f 100644 --- a/apps/api/src/sports/service.ts +++ b/apps/api/src/sports/service.ts @@ -2057,6 +2057,11 @@ export class SportsWorker { const tick = async () => { this.ticking = true; try { + // Before the long club refresh, so a restart shows tonight's seleção without waiting for every league. + if (Date.now() - this.lastNational >= 60 * 60_000) { + this.lastNational = Date.now(); + await this.service.syncNationalCallUps(); + } if (Date.now() - this.lastRefresh >= (this.opts.refreshCheckMs ?? 10 * 60_000)) { this.lastRefresh = Date.now(); await this.service.refreshDue(); @@ -2066,11 +2071,6 @@ export class SportsWorker { await this.service.mergeSamePeople(30); await this.service.attachArchiveGames(12); } - // National fixtures (Euro Cup) land in the database on their own, so the picker already has them. - if (Date.now() - this.lastNational >= 60 * 60_000) { - this.lastNational = Date.now(); - await this.service.syncNationalCallUps(); - } await this.service.pollLineups(); } catch (err) { this.opts.log.warn({ err }, "sports: tick failed");