plus voting history initial

This commit is contained in:
Kalle 2021-02-14 11:25:18 +02:00
parent 08303cc8a4
commit 463ebeee39
4 changed files with 111 additions and 0 deletions

View File

@ -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<PlusVotingHistoryPageProps> = 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;

View File

@ -0,0 +1,14 @@
import { GetVotingSummariesByMonthAndTierData } from "./plusService";
export interface PlusVotingHistoryPageProps {
summaries: GetVotingSummariesByMonthAndTierData;
}
const PlusVotingHistoryPage: React.FC<PlusVotingHistoryPageProps> = ({
summaries,
}) => {
console.log({ summaries });
return <>hello</>;
};
export default PlusVotingHistoryPage;

View File

@ -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,
};

6
utils/prisma.ts Normal file
View File

@ -0,0 +1,6 @@
export const userBasicSelection = {
username: true,
discordAvatar: true,
discriminator: true,
discordId: true,
};