mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-07-19 09:02:43 -05:00
Fix people with already having access causing 400 error in voting
This commit is contained in:
parent
2c89ca5bfa
commit
b4704c1c4a
|
|
@ -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"];
|
||||
|
|
|
|||
|
|
@ -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 = [];
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user