sendou.ink/app/features/friends/FriendRepository.server.ts
2026-01-16 21:30:03 +02:00

52 lines
1.2 KiB
TypeScript

import { db } from "~/db/sql";
export async function findByUserIdWithActivity(userId: number) {
const rows = await db
.selectFrom("Friendship")
.innerJoin("User", (join) =>
join.on((eb) =>
eb.or([
eb.and([
eb("Friendship.userOneId", "=", userId),
eb("User.id", "=", eb.ref("Friendship.userTwoId")),
]),
eb.and([
eb("Friendship.userTwoId", "=", userId),
eb("User.id", "=", eb.ref("Friendship.userOneId")),
]),
]),
),
)
.leftJoin("TournamentSub", "TournamentSub.userId", "User.id")
.leftJoin(
"CalendarEvent",
"CalendarEvent.tournamentId",
"TournamentSub.tournamentId",
)
.leftJoin(
"CalendarEventDate",
"CalendarEventDate.eventId",
"CalendarEvent.id",
)
.select([
"User.id",
"User.username",
"User.discordId",
"User.discordAvatar",
"User.customUrl",
"CalendarEvent.name as tournamentName",
"TournamentSub.tournamentId",
"CalendarEventDate.startTime as tournamentStartTime",
])
.where((eb) =>
eb.or([
eb("Friendship.userOneId", "=", userId),
eb("Friendship.userTwoId", "=", userId),
]),
)
.orderBy("Friendship.createdAt", "desc")
.execute();
return rows;
}