From ec77a7c1b6bf6f51739e20dd39fef584cb9fd53f Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sat, 20 Feb 2021 23:39:58 +0200 Subject: [PATCH] add suggestion to plus services --- services/plusService.ts | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/services/plusService.ts b/services/plusService.ts index 9ab17257c..cae3f9694 100644 --- a/services/plusService.ts +++ b/services/plusService.ts @@ -1,7 +1,7 @@ -import { PlusRegion, Prisma } from "@prisma/client"; -import prisma from "prisma/client"; +import { Prisma } from "@prisma/client"; import { getPercentageFromCounts } from "lib/plus"; import { userBasicSelection } from "lib/prisma"; +import prisma from "prisma/client"; export type VotingSummariesByMonthAndTier = Prisma.PromiseReturnType< typeof getVotingSummariesByMonthAndTier @@ -98,8 +98,35 @@ const getDistinctSummaryMonths = () => { }); }; +const addSuggestion = async ({ + data, + userId, +}: { + data: Prisma.PlusSuggestionUncheckedCreateInput; + userId: number; +}) => { + const existingSuggestion = await prisma.plusSuggestion.findUnique({ + where: { tier_suggestedId_suggesterId: data }, + }); + + // every user can only send one new suggestion per month + if (!existingSuggestion) { + const usersSuggestion = await prisma.plusSuggestion.findFirst({ + where: { isResuggestion: false, suggesterId: userId }, + }); + if (usersSuggestion) { + throw Error("Already made a new suggestion"); + } + } + + return prisma.plusSuggestion.create({ + data: { ...data, isResuggestion: !!existingSuggestion }, + }); +}; + export default { getVotingSummariesByMonthAndTier, getMostRecentVotingWithResultsMonth, getDistinctSummaryMonths, + addSuggestion, };