From 463ebeee396ba6428babf7614cc56de43e04eee9 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 14 Feb 2021 11:25:18 +0200 Subject: [PATCH] plus voting history initial --- pages/plus/history/[slug].tsx | 44 +++++++++++++++++++++++++ scenes/plus/PlusVotingHistoryPage.tsx | 14 ++++++++ scenes/plus/plusService.ts | 47 +++++++++++++++++++++++++++ utils/prisma.ts | 6 ++++ 4 files changed, 111 insertions(+) create mode 100644 pages/plus/history/[slug].tsx create mode 100644 scenes/plus/PlusVotingHistoryPage.tsx create mode 100644 scenes/plus/plusService.ts create mode 100644 utils/prisma.ts diff --git a/pages/plus/history/[slug].tsx b/pages/plus/history/[slug].tsx new file mode 100644 index 000000000..c251dd5b5 --- /dev/null +++ b/pages/plus/history/[slug].tsx @@ -0,0 +1,44 @@ +import { GetStaticPaths, GetStaticProps } from "next"; +import PlusVotingHistoryPage, { + PlusVotingHistoryPageProps, +} from "scenes/plus/PlusVotingHistoryPage"; +import plusService from "scenes/plus/plusService"; + +export const getStaticPaths: GetStaticPaths = async () => { + return { + paths: [], + fallback: "blocking", + }; +}; + +export const getStaticProps: GetStaticProps = async ({ + params, +}) => { + const getSlug = async () => { + const slug = Array.isArray(params!.slug) ? params!.slug : []; + if (slug.length === 3) { + return slug; + } + + const mostRecent = await plusService.getMostRecentVotingWithResultsMonth(); + + return ["1", mostRecent.year, mostRecent.month]; + }; + + const [tier, year, month] = (await getSlug()).map((param) => Number(param)); + const summaries = await plusService.getVotingSummariesByMonthAndTier({ + tier: tier as any, + year, + month, + }); + + console.log({ summaries }); + + if (!summaries.length) return { notFound: true }; + + return { + props: { summaries }, + }; +}; + +export default PlusVotingHistoryPage; diff --git a/scenes/plus/PlusVotingHistoryPage.tsx b/scenes/plus/PlusVotingHistoryPage.tsx new file mode 100644 index 000000000..9b1fa22cd --- /dev/null +++ b/scenes/plus/PlusVotingHistoryPage.tsx @@ -0,0 +1,14 @@ +import { GetVotingSummariesByMonthAndTierData } from "./plusService"; + +export interface PlusVotingHistoryPageProps { + summaries: GetVotingSummariesByMonthAndTierData; +} + +const PlusVotingHistoryPage: React.FC = ({ + summaries, +}) => { + console.log({ summaries }); + return <>hello; +}; + +export default PlusVotingHistoryPage; diff --git a/scenes/plus/plusService.ts b/scenes/plus/plusService.ts new file mode 100644 index 000000000..817c26af5 --- /dev/null +++ b/scenes/plus/plusService.ts @@ -0,0 +1,47 @@ +import { Prisma } from "@prisma/client"; +import prisma from "prisma/client"; +import { userBasicSelection } from "utils/prisma"; + +export type GetVotingSummariesByMonthAndTierData = Prisma.PromiseReturnType< + typeof getVotingSummariesByMonthAndTier +>; + +const getVotingSummariesByMonthAndTier = ({ + tier, + year, + month, +}: { + tier: 1 | 2; + year: number; + month: number; +}) => { + return prisma.plusVotingSummary.findMany({ + where: { tier, year, month }, + select: { + countsEU: true, + wasSuggested: true, + wasVouched: true, + countsNA: true, + user: { + select: userBasicSelection, + }, + }, + }); +}; + +const getMostRecentVotingWithResultsMonth = async () => { + const mostRecent = await prisma.plusVotingSummary.findFirst({ + orderBy: [{ year: "desc" }, { month: "desc" }], + }); + if (!mostRecent) + throw Error( + "unexpected null mostRecent in getMostRecentVotingWithResultsMonth" + ); + + return { year: mostRecent.year, month: mostRecent.month }; +}; + +export default { + getVotingSummariesByMonthAndTier, + getMostRecentVotingWithResultsMonth, +}; diff --git a/utils/prisma.ts b/utils/prisma.ts new file mode 100644 index 000000000..627682f94 --- /dev/null +++ b/utils/prisma.ts @@ -0,0 +1,6 @@ +export const userBasicSelection = { + username: true, + discordAvatar: true, + discriminator: true, + discordId: true, +};