diff --git a/app/features/plus-suggestions/PlusSuggestionRepository.server.ts b/app/features/plus-suggestions/PlusSuggestionRepository.server.ts index 030efcf2d..cac3451a2 100644 --- a/app/features/plus-suggestions/PlusSuggestionRepository.server.ts +++ b/app/features/plus-suggestions/PlusSuggestionRepository.server.ts @@ -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; 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"]; diff --git a/app/features/plus-voting/PlusVotingRepository.server.ts b/app/features/plus-voting/PlusVotingRepository.server.ts index f6fc69875..52e8a6119 100644 --- a/app/features/plus-voting/PlusVotingRepository.server.ts +++ b/app/features/plus-voting/PlusVotingRepository.server.ts @@ -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 = []; diff --git a/app/features/plus-voting/routes/plus.voting.tsx b/app/features/plus-voting/routes/plus.voting.tsx index 54abc1dae..638f8bd51 100644 --- a/app/features/plus-voting/routes/plus.voting.tsx +++ b/app/features/plus-voting/routes/plus.voting.tsx @@ -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(); + 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, });