sendou.ink/app/features/sendouq/queries/findCurrentGroupByUserId.server.ts
Kalle c0ec15b7de
Some checks failed
Tests and checks on push / run-checks-and-tests (push) Has been cancelled
Updates translation progress / update-translation-progress-issue (push) Has been cancelled
Unify db type files
2025-03-21 21:47:08 +02:00

40 lines
1002 B
TypeScript

import { sql } from "~/db/sql";
import type { Tables } from "~/db/tables";
import invariant from "~/utils/invariant";
const stm = sql.prepare(/* sql */ `
select
"Group"."id",
"Group"."status",
"Group"."latestActionAt",
"Group"."chatCode",
"GroupMatch"."id" as "matchId",
"GroupMember"."role"
from
"Group"
left join "GroupMember" on "GroupMember"."groupId" = "Group"."id"
left join "GroupMatch" on "GroupMatch"."alphaGroupId" = "Group"."id"
or "GroupMatch"."bravoGroupId" = "Group"."id"
where
"Group"."status" != 'INACTIVE'
and "GroupMember"."userId" = @userId
`);
type ActiveGroup = Pick<
Tables["Group"],
"id" | "status" | "latestActionAt" | "chatCode"
> & {
matchId?: number;
role: Tables["GroupMember"]["role"];
};
export function findCurrentGroupByUserId(
userId: number,
): ActiveGroup | undefined {
const groups = stm.all({ userId }) as any;
invariant(groups.length <= 1, "User can't be in more than one group");
return groups[0];
}