sendou.ink/scenes/plus/plusService.ts
2021-02-14 11:25:18 +02:00

48 lines
1.1 KiB
TypeScript

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