Fix people with already having access causing 400 error in voting

This commit is contained in:
Kalle 2024-02-09 23:40:43 +02:00
parent 2c89ca5bfa
commit b4704c1c4a
3 changed files with 28 additions and 6 deletions

View File

@ -26,13 +26,14 @@ type FindAllByMonthRow = {
discordId: string;
discordAvatar: string | null;
bio: string | null;
plusTier: number | null;
};
};
// TODO: naming is a bit weird here (suggestion inside suggestions)
export type FindAllByMonthItem = Unwrapped<typeof findAllByMonth>;
export async function findAllByMonth(args: MonthYear) {
const rows = (await db
const allRows = (await db
.selectFrom("PlusSuggestion")
.select(({ eb }) => [
"PlusSuggestion.id",
@ -48,7 +49,12 @@ export async function findAllByMonth(args: MonthYear) {
jsonObjectFrom(
eb
.selectFrom("User")
.select([...COMMON_USER_FIELDS, "User.bio"])
.leftJoin("PlusTier", "PlusSuggestion.suggestedId", "PlusTier.userId")
.select([
...COMMON_USER_FIELDS,
"User.bio",
"PlusTier.tier as plusTier",
])
.whereRef("PlusSuggestion.suggestedId", "=", "User.id"),
).as("suggested"),
])
@ -57,6 +63,12 @@ export async function findAllByMonth(args: MonthYear) {
.orderBy("PlusSuggestion.createdAt", "asc")
.execute()) as FindAllByMonthRow[];
// filter out suggestions that were made in the time period
// between voting ending and people gaining access from the leaderboard
const rows = allRows.filter(
(r) => !r.suggested.plusTier || r.suggested.plusTier > r.tier,
);
const result: Array<{
suggested: FindAllByMonthRow["suggested"];
tier: FindAllByMonthRow["tier"];

View File

@ -10,7 +10,6 @@ import {
import { COMMON_USER_FIELDS } from "~/utils/kysely.server";
import type { Unwrapped } from "~/utils/types";
import * as PlusSuggestionRepository from "~/features/plus-suggestions/PlusSuggestionRepository.server";
import invariant from "tiny-invariant";
const resultsByMonthYearQuery = (args: MonthYear) =>
db
@ -89,7 +88,6 @@ export async function usersForVoting(loggedInUser: {
rangeToMonthYear(nextNonCompletedVoting(new Date())),
)
).filter((suggestion) => suggestion.tier === loggedInUser.plusTier);
invariant(suggestedUsers);
const result: UsersForVoting = [];

View File

@ -62,10 +62,22 @@ export const action: ActionFunction = async ({ request }) => {
id: user.id,
plusTier: user.plusTier,
});
validateVotes({ votes: data.votes, usersForVoting });
// this should not be needed but makes the voting a bit more resilient
// if there is a bug that causes some user to show up twice, or some user to show up who should not be included
const seen = new Set<number>();
const filteredVotes = data.votes.filter((vote) => {
if (seen.has(vote.votedId)) {
return false;
}
seen.add(vote.votedId);
return usersForVoting.some((u) => u.user.id === vote.votedId);
});
validateVotes({ votes: filteredVotes, usersForVoting });
// freebie +1 for yourself if you vote
const votesForDb = [...data.votes].concat({
const votesForDb = [...filteredVotes].concat({
votedId: user.id,
score: PLUS_UPVOTE,
});