diff --git a/app/features/friends/FriendRepository.server.test.ts b/app/features/friends/FriendRepository.server.test.ts index f0b0c06a9..51cf86073 100644 --- a/app/features/friends/FriendRepository.server.test.ts +++ b/app/features/friends/FriendRepository.server.test.ts @@ -1,7 +1,11 @@ +import { addDays, subDays } from "date-fns"; import { beforeEach, describe, expect, test } from "vitest"; import * as FriendRequestFactory from "~/db/seed/factories/FriendRequestFactory"; import * as FriendshipFactory from "~/db/seed/factories/FriendshipFactory"; +import * as TournamentFactory from "~/db/seed/factories/TournamentFactory"; +import * as TournamentLFGTeamFactory from "~/db/seed/factories/TournamentLFGTeamFactory"; import * as UserFactory from "~/db/seed/factories/UserFactory"; +import { dateToDatabaseTimestamp } from "~/utils/dates"; import { withUserId } from "~/utils/Test"; import * as FriendRepository from "./FriendRepository.server"; @@ -419,6 +423,56 @@ describe("findByUserIdWithActivity", () => { expect(result).toHaveLength(0); }); + + const lookingTournamentNameOfFriend = async ({ + startsAt, + regClosesAt, + }: { + startsAt: Date; + regClosesAt?: Date; + }) => { + await FriendshipFactory.create({ + userOneId: users.id(1), + userTwoId: users.id(2), + }); + const tournament = await TournamentFactory.create({ + authorId: users.id(3), + name: "Looking Cup", + startTimes: [dateToDatabaseTimestamp(startsAt)], + regClosesAt: regClosesAt + ? dateToDatabaseTimestamp(regClosesAt) + : undefined, + }); + await TournamentLFGTeamFactory.create({ + tournamentId: tournament.id, + userId: users.id(2), + }); + + const result = await FriendRepository.findByUserIdWithActivity(users.id(1)); + + return result.find((row) => row.id === users.id(2))?.tournamentName; + }; + + test("shows a friend looking for a team in an upcoming tournament", async () => { + expect( + await lookingTournamentNameOfFriend({ startsAt: addDays(new Date(), 1) }), + ).toBe("Looking Cup"); + }); + + test("ignores looking for a team in a tournament that has started", async () => { + expect( + await lookingTournamentNameOfFriend({ startsAt: subDays(new Date(), 7) }), + ).toBeNull(); + }); + + test("ignores looking for a team after registration has closed", async () => { + expect( + await lookingTournamentNameOfFriend({ + startsAt: addDays(new Date(), 1), + regClosesAt: subDays(new Date(), 1), + }), + ).toBeNull(); + }); }); describe("updateOwnFriendshipPinned", () => { diff --git a/app/features/friends/FriendRepository.server.ts b/app/features/friends/FriendRepository.server.ts index 66fd05ae2..c6acfe587 100644 --- a/app/features/friends/FriendRepository.server.ts +++ b/app/features/friends/FriendRepository.server.ts @@ -93,19 +93,18 @@ function withLfgJoins>(qb: QB) { "=", "TournamentTeamMember.tournamentTeamId", ) - .where((innerEb) => - innerEb.or([ - innerEb( - sql`json_extract("LookingTournament"."settings", '$.regClosesAt')`, - "is", - null, - ), - innerEb( - sql`json_extract("LookingTournament"."settings", '$.regClosesAt')`, - ">", - nowTimestamp, - ), - ]), + .where( + sql`coalesce( + "LookingTournament"."settings" ->> 'regClosesAt', + ( + select min("CalendarEventDate"."startsAt") + from "CalendarEventDate" + inner join "CalendarEvent" on "CalendarEvent"."id" = "CalendarEventDate"."eventId" + where "CalendarEvent"."tournamentId" = "LookingTournament"."id" + ) + )`, + ">", + nowTimestamp, ), ), ), diff --git a/changelog/2026-11-05-sidebar-friend-past-tournament.md b/changelog/2026-11-05-sidebar-friend-past-tournament.md new file mode 100644 index 000000000..76d855cf0 --- /dev/null +++ b/changelog/2026-11-05-sidebar-friend-past-tournament.md @@ -0,0 +1,5 @@ +--- +navItem: medal +type: bug +--- +Friends who signed up as a sub no longer show in the sidebar as looking for a team in a tournament that already happened