sendou.ink/app/features/sendouq-match/GroupMatchContinueVoteRepository.server.ts
Kalle 2b5b1b1948
Some checks are pending
E2E Tests / e2e (push) Waiting to run
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
New match page (#3032)
2026-05-04 18:15:10 +03:00

68 lines
1.6 KiB
TypeScript

import type { Transaction } from "kysely";
import { db } from "~/db/sql";
import type { DB, DBBoolean } from "~/db/tables";
export async function findForGroups(groupIds: number[], trx?: Transaction<DB>) {
if (groupIds.length === 0) return [];
const executor = trx ?? db;
const rows = await executor
.selectFrom("GroupMatchContinueVote")
.select([
"GroupMatchContinueVote.groupId",
"GroupMatchContinueVote.userId",
"GroupMatchContinueVote.isContinuing",
"GroupMatchContinueVote.votedAt",
])
.where("GroupMatchContinueVote.groupId", "in", groupIds)
.execute();
return rows.map((row) => ({
...row,
isContinuing: Boolean(row.isContinuing),
}));
}
export async function cast(
{
groupId,
userId,
isContinuing,
}: {
groupId: number;
userId: number;
isContinuing: DBBoolean;
},
trx?: Transaction<DB>,
) {
const executor = trx ?? db;
const runner = async (t: Transaction<DB>) => {
if (isContinuing === 0) {
// every vote is only valid for a specific continuing size
// e.g. if i want to keep going with a full group, i might not
// want to continue with just 3 people -> revote required from all
await t
.deleteFrom("GroupMatchContinueVote")
.where("GroupMatchContinueVote.groupId", "=", groupId)
.where("GroupMatchContinueVote.isContinuing", "=", 1)
.execute();
}
await t
.insertInto("GroupMatchContinueVote")
.values({ groupId, userId, isContinuing })
.onConflict((oc) =>
oc.columns(["groupId", "userId"]).doUpdateSet({ isContinuing }),
)
.execute();
};
if (trx) {
await runner(trx);
return;
}
await executor.transaction().execute(runner);
}